我有以下的PHP/SQL的例子,當編輯一行&保存更改時可以很好地工作。PHP/MYSQL/XHTML FORM。如何一次更新多行表?
它基於POST/GET方法工作 - 其中URL正在設置要編輯/保存的行ID。
form -> post id & live values
seperate php file -> get 'id' and change row with this 'id'.
<?php
session_name('users');
session_set_cookie_params(2*7*24*60*60);
session_start();
define('INCLUDE_CHECK',true);
require 'connect.php';
require 'functions.php';
if(!$_SESSION['id']) {
header ("Location: index.php");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$id = clean($_POST['id']);
$usr2 = $_SESSION['usr'];
$live = (isset($_POST['live']))?1:0;
$updated = date("F j, Y, g:i a",time()+60*60);
$title = clean($_POST['title']);
$content = clean($_POST['content']);
//Create INSERT query
$qry = "UPDATE table SET live = '$live' WHERE id='".mysql_real_escape_string($_POST['id']). "' ";
$result = mysql_query($qry);
echo mysql_error();
//Check whether the query was successful or not
if($result) {
header("location: notes.php");
exit();
}else {
die("Query failed");
}
?>
我的問題是如何更新多行表/格式如this one?
2件小事。 1)對於你的cookie,輸入這個等式的結果並且評論它正在做什麼。無需每次加載都要進行計算。 2)有沒有你不使用BEGIN - COMMIT/ROLLBACK的原因?你可能想這樣做,因爲它確實失敗。 – afuzzyllama
@afuzzyllama - 原因是我剛開始使用PHP/MYSQL。我不知道所有的選項。 – Iladarsda
@afuzzyllama - cookie被設置爲'remeber me'字段 - 「登錄」系統的一部分。不是SQL查詢的一部分。 – Iladarsda