[英]While Loop using fgetcsv only updating MySQL Database on first instance
[英]Updating mysql database using While loop php
这已经困扰了我三天了。.我是这个新手,正在设法弄清楚我的想法。 我有一个涉及3个领域的表格。 名字,姓氏,商标。 我使用了while循环从mysql表生成表。 我使用了一个文本框,并使用循环在“ ID”之后调用该文本框,因此每个文本框都具有唯一的名称。 然后,我使用post方法将值发送到第二个页面,该页面将使用用户刚刚输入的值更新“标记”列。这是我发现问题的地方!
这是初始页面。
<html>
<head><title>Please Enter Your Surname</title></head>
<body>
<center>
<h2><font color=blue>Please Enter Your Surname</font></h2><p>
<form action="insert.php" method="POST">
<?php
$db = mysql_connect("localhost","root","");
if (!$db)
{
do_error("Could not connect to the server");
}
mysql_select_db("session6",$db)or do_error("Could not connect to the database");
$result = mysql_query("SELECT * FROM members ORDER BY id",$db);
$rows=mysql_num_rows($result);
if(!$rows)
{
do_error("No results found");
}
else
{
echo "<table border=3 cellspacing=1 cellpadding=1
align=center bgcolor=lightblue>\n";
echo "<caption><h2><font color=blue> Members Details
</font></h2></caption>\n";
echo "<tr><th>Member Id</th><th>Firstname</th><th>Mark</th></tr>\n";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td strong>" . $row['Id'] . "</td>";
echo "<td strong>" . $row['Firstname'] . "</td>";?>
<td strong><input type="text" name="<?php echo $row['Id']; ?>" size="20"></td>
<tr>
<?php
}
?><input type="hidden" name="no_of_rows" value="<?php echo $rows; ?>">
<?php
echo "</table>\n";
}
mysql_close($db) or do_error("Could not close connection");
function do_error($error)
{
echo $error;
die();
}
?>
<input type="submit" value="Search">
<input type="reset" value="Reset">
</form>
</body></html>
`
然后更新在这里完成,这似乎是我遇到的问题:
<html>
<body>
<?php
$db = mysql_connect("localhost","root","");
if (!$db)
{
do_error("Could not connect to the server");
}
mysql_select_db("marks",$db)or do_error("Could not connect to the database");
$i=1;
while ($i <= $_POST["no_of_rows"])// or $_POST["No_of_Rows"] from form
{
$insertsql = "UPDATE members SET mark = " . $_POST[$i] . " WHERE Id = " . $row['Id'] . ";";
echo $_POST['$i'];
$i++;
}
?>
</body></html>
当我回显$ _POST [$ i']时,它会显示正确的值,但不会更新数据库,因此我不准备将笔记本电脑扔进垃圾箱! 哈! 我知道这很可能是愚蠢的,但我什至看不到,所以我们将不胜感激。
您在更新查询中缺少单引号。 这将有助于:
$insertsql = "UPDATE `members` SET `mark` = '" . $_POST[$i] . "' WHERE `Id` = '" . $row['Id'] . "' ;";
您也没有运行mysql_query
查询命令进行更新
最后,您正在使用不推荐使用的mysql php命令。 改用mysqli或pdo。 并且不要忘记在查询中转义数据以防止sql注入
问题是这里的单引号,强制使用文字'$ i',这可能不是$ _POST中的键
echo $_POST["$i"];
使用变量时,无需使用引号:$ _POST [$ id];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.