-1

我有一个 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

4

3 回答 3

0

太长的问题,但是如果您通过帖子页面中的更新状态表示它回显了更新,如果我是对的,它将回显更新,因为您正在检查它而条件为真。但是,您是否在帖子页面中回显了发布的值,检查您是否以这种方式获得该值,以在此处询问长问题之前找到您在哪里遇到问题。

于 2012-12-08T11:22:36.550 回答
0

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

echo "<td><input type='text' size='40' name='mname[]' value='name' /></td>";
于 2012-12-08T11:52:00.587 回答
0

如果您希望执行所有查询,则可以在 mysql 中使用事务处理,否则不执行。

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

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

于 2012-12-08T11:52:45.997 回答