通过PHP Web表单表更新mySQL表中的多条记录?
需要一点帮助...
我有一个基本的 html 表,最后一列有文本字段形式,网络表的每一行都有一个隐藏字段。该表中的数据是从数据库中的表中提取的。
我希望我的用户能够使用此 Web 表单(页面)更新数据库中的一个字段(分数)。
我在每一行上都有一个隐藏的 Web 表单组件,其中包含网页表中每一行的数据库中记录的唯一 ID。
我试图创建代码来更新网络表单上的整个条目列表,即使用户没有更新该特定字段。 (分数字段的值在创建表时填充到 Web 表单中。因此,如果您没有更新任何分数,但点击提交按钮,它将使用相同的值更新数据库表。)
这是我的代码:(缩写以节省字节......)
<?php
//Do all the database connection stuff up front
if (isset($_POST[‘score’]))
{
$student_id = $_POST[‘student_id’];
$score = $_POST['score'];
$n = count($score);
$i = 0;
echo "You have updated these student scores on this assignment. \r\n" .
"<ol>";
while ($i < $n)
{
echo "<hr><P>{$score[$i]} \r\n";
echo "<hr><P>{$student_id[$i]} \r\n";
$qry = "UPDATE assignments SET score = ".$score[$i]." WHERE student_id = " .$student_id[$i]. '"';
$result=@mysql_query($qry);
$i++;
}
}
if($result) {
header("location: member-index.php");
exit();
}else {
die("Query failed");
}
?>
我走在正确的道路上吗?有更好的方法来做我正在尝试的事情吗?欢迎所有建议和想法!
先感谢您!
Need a little help...
I have a basic html table with text field form in last column and a hidden field on each row of the web table. The data in this table is extracted out of a table in the database.
I want my users to be able to update one field in the database (a score) using this web form (page).
I have a hidden web form component on each row that contains the unique id of the record in the database for each row in the web page table.
I was attempting to create code that would update the entire list of entries on the web form, even if the user is not updating that particular field. (The values of the scores field are populated into the web form at the creation of the table. So if you did not update any scores, but hit the submit button, it would update the database table with the same values.)
Here’s my code: (abbreviated to save bytes…)
<?php
//Do all the database connection stuff up front
if (isset($_POST[‘score’]))
{
$student_id = $_POST[‘student_id’];
$score = $_POST['score'];
$n = count($score);
$i = 0;
echo "You have updated these student scores on this assignment. \r\n" .
"<ol>";
while ($i < $n)
{
echo "<hr><P>{$score[$i]} \r\n";
echo "<hr><P>{$student_id[$i]} \r\n";
$qry = "UPDATE assignments SET score = ".$score[$i]." WHERE student_id = " .$student_id[$i]. '"';
$result=@mysql_query($qry);
$i++;
}
}
if($result) {
header("location: member-index.php");
exit();
}else {
die("Query failed");
}
?>
Am I on the right track? Is there a better way to do what I’m attempting? All suggestions and ideas welcome!
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你正在使用
为什么不将唯一的 id 回显到输入名称中?
这意味着在循环中,$i 将是唯一的 id(少一个变量和更少的 HTML)。
并且在处理数据库事务时请使用 mysql_real_escape_string() 。我知道这是示例代码,但请不要忘记
除此之外,是的,您走在正确的轨道上
i'm guessing you are using
why not echo the unique id into the input name?
this means in the loop, the $i would be the unique id (one less variable and less HTML).
and please use mysql_real_escape_string() when working with DB transactions. I know it's an example code but please don't forget that
Besides that, yes, you are on the right track