我无法更新第一行表

时间:2012-11-19 06:48:56

标签: php mysql html-table

我的编码显示表用户的所有数据..文件名是admindisplay.php

<?php
//Establish Server Connection String

//Connect to database
include('server.php');
session_start();

//Query database and set result to variable
$result = mysql_query ("SELECT * FROM user");
//Generate Table
echo "<table border='1'><tr><th>No</th><th>Name</th><th>Department</th><th>Date</th><th>Issue</th><th>Details</th><th>Assign Person</th><th>Status</th></tr>";

while($row = mysql_fetch_array($result))
{
$No = $row ['No'];
echo "<tr>";
echo "<td>" . $row['No'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Department'] . "</td>";
echo "<td>" . $row ['Date'] . "</td>";
echo "<td>" . $row ['Issue'] . "</td>";
echo "<td>" . $row ['Details'] . "</td>";
echo "<td>" . $row ['Assignperson'] . "</td>";
echo "<td>" . $row ['Status'] . "</td>";
echo "<td><form action=\"adminassign.php\" method=\"POST\">
Note:   <input type=\"text\" size=\"40\" maxlength=\"100\" name=\"note\">

<label>Assign to:</label>
<select name=\"assign\">
<option value=\"\">-Choose-</option>
<option value=\"Adrian\">Adrian</option>
<option value=\"Trainee\">Trainee</option> 
</select>

<input name=\"Nos\" type=\"hidden\" value=\"$No\">

<input name=\"submit\" type=\"submit\" value=\"Assign\"</td>";
}
echo "</table>";

?>

这是我的编码更新了我想要的任何行。文件名是adminassign.php

<?php
//Establish Server Connection String
//Connect to database
include('server.php');

session_start();
$No = $_POST ['Nos'];
$Note = $_POST['note'];
$Assign  = mysql_real_escape_string(htmlspecialchars($_POST['assign']));

if($Assign=='Adrian')
{

mysql_query("UPDATE user SET Assignperson = 'Adrian'  WHERE No = '$No'");
mysql_query("UPDATE user SET Note = '$Note'  WHERE No = '$No'");
echo "assign successful";

}

else
{

if($Assign=='Trainee')

{
mysql_query("UPDATE user SET Assignperson = 'Trainee' WHERE No = '$No'");
mysql_query("UPDATE user SET Note = '$Note'  WHERE No = '$No'");
echo "assign successful";

}
}
?>

<form action="index.php" method="POST">
<p><input type="submit" value="Log Out"> 

</form>

<form action="admindisplay.php" method="POST">
<p><input type="submit" value="Back">

 </form>    

现在的问题是,我只能更新最后一行,前一行无法更新。任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

是的,这很简单,你必须使用这个

<input name=\"Nos\" type=\"hidden\" value=\"$No\">

每当您提交此内容时,他们都会提交最后一条记录数据,以便您可以使用该备选内容

<td><a href='adminassign.php?id=<?php echo $row['No']; ?>'>Edit</a></td>

这对你有帮助!!!!

答案 1 :(得分:0)

您错过了表格中每行的每个表单的结束标记:

</form>

然后,你只有一个带有很多Nos输入的表格,只考虑最后一个。

第一个代码示例中while循环的最后一行应该是:

<input name=\"submit\" type=\"submit\" value=\"Assign\" /></form></td>";

您的代码中还有其他语法问题(例如缺少右括号&gt;)。 html代码的验证器可以捕获这类问题:http://validator.w3.org/