PHP + MySQL - 无法一次更新多个表

时间:2012-12-08 11:15:45

标签: php mysql

我有一个php表单来更新MySQL表。获取工作完美,但更新不起作用。这是我的表单代码:

<?php

$sql= "SELECT client.resID AS resID, client.resName AS resName FROM client WHERE client.resID =".$_GET["resID"];
$rs = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());

$sqlM= "SELECT menu.id AS mid, menu.name AS mname FROM menu WHERE menu.resID =".$_GET["resID"];
$rsM = mysql_query($sqlM) or die($sqlM."<br/><br/>".mysql_error());

$sqlF= "SELECT facilities.id AS fid, facilities.name AS fname FROM facilities WHERE facilities.resID =".$_GET["resID"];
$rsF = mysql_query($sqlF) or die($sqlF."<br/><br/>".mysql_error());

$sqlS= "SELECT services.id AS sid, services.name AS sname FROM services WHERE services.resID =".$_GET["resID"];
$rsS = mysql_query($sqlS) or die($sqlS."<br/><br/>".mysql_error());

// $names array now contains all names

$i = 0;


echo '<table width="50%">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>Name</td>';
echo '<td>Edit</td>';
echo '</tr>';

echo "<form name='form_update' method='post' action='client_admin_post.php'>\n";

while ($fm = mysql_fetch_array($rsM)) { // loop as long as there are more results
    $mnames[] = $fm['mname'];
    $mid[] = $fm['mid'];  // push to the array


echo '<tr>';
echo "<td>Menu :</td>";
echo "<td><input type='text' size='40' name='mname' value='{$fm['mname']}' /></td>";
echo "<td>{$fm['id']}<input type='hidden' name='mid' value='{$fm['mid']}' /></td>";
echo '</tr>';
++$i;

print_r($mnames);
}

while ($ff = mysql_fetch_array($rsF)) { // loop as long as there are more results
    $fnames[] = $ff['fname'];
    $fid[] = $ff['fid'];  // push to the array


echo '<tr>';
echo "<td>Facilities :</td>";
echo "<td><input type='text' size='40' name='fname' value='{$ff['fname']}' /></td>";
echo "<td>{$ff['id']}<input type='hidden' name='fid' value='{$ff['fid']}' /></td>";
echo '</tr>';
++$i;


}

while ($fs = mysql_fetch_array($rsS)) { // loop as long as there are more results
    $snames[] = $fs['sname'];
    $sid[] = $fs['sid'];  // push to the array


echo '<tr>';
echo "<td>Services :</td>";
echo "<td><input type='text' size='40' name='sname' value='{$fs['sname']}' /></td>";
echo "<td>{$fs['id']}<input type='hidden' name='sid' value='{$fs['sid']}' /></td>";
echo '</tr>';
++$i;


}

echo'<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';



?>

这是我的邮政编码:

$size = count($_POST['mname']);

$i = 0;
while ($i < $size) {
$mname= $_POST['mname'][$i];
$mid = $_POST['mid'][$i];


$query = "UPDATE menu SET name = '$mname' WHERE id = '$mid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
echo "$mname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}


$size = count($_POST['fname']);

$i = 0;
while ($i < $size) {
$fname= $_POST['fname'][$i];
$fid = $_POST['fid'][$i];


$query1 = "UPDATE facilities SET name = '$fname' WHERE id = '$fid' LIMIT 1";
mysql_query($query1) or die ("Error in query: $query1");
echo "$fname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}


$size = count($_POST['sname']);

$i = 0;
while ($i < $size) {
$sname= $_POST['sname'][$i];
$sid = $_POST['sid'][$i];

$query3 = "UPDATE services SET name = '$sname' WHERE id = '$sid' LIMIT 1";
mysql_query($query3) or die ("Error in query: $query3");
echo "$sname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}

我在帖子页面中获得了'更新'状态,但MySQL表中没有更新任何内容。如何解决这个问题呢?真的很感谢你的帮助:D

3 个答案:

答案 0 :(得分:0)

太长的问题,但是如果你但是通过帖子页面中的更新状态你的意思是说它回应更新如果我是对的它会回显更新,因为你正在检查它而条件为真。但是你是否回应了帖子页面中发布的值,检查你是否正在以这种方式获取价值,以便在问这里很长的问题之前找到你遇到问题的地方。

答案 1 :(得分:0)

您正在创建具有相同名称的多个输入,例如“fname”,但您尝试访问它们就像它们是一个数组一样。重命名以下字段:

echo "<td><input type='text' size='40' name='mname[]' value='name' /></td>";

答案 2 :(得分:0)

如果您希望所有查询都执行,则可以在mysql中使用transacrions,否则无。

您的帖子页面已更新,因为在您的查询结束时您只是回显“已更新”,没有条件应该打印。

并且您的数据未更新,因为您没有为mysql_query指定任何连接变量(“query”,$ conn)