简体   繁体   English

使用While循环php更新mysql数据库

[英]Updating mysql database using While loop php

This has been bugging me for 3 days now.. I'm new to this and trying to get my head round something. 这已经困扰了我三天了。.我是这个新手,正在设法弄清楚我的想法。 I have a form which involves 3 fields. 我有一个涉及3个领域的表格。 Firstname, Surname, Marks. 名字,姓氏,商标。 I have used a while loop to generate the table from a mysql table. 我使用了while循环从mysql表生成表。 I have used a text box and used the loop to call the text box after the 'ID' so each text box is named uniquely. 我使用了一个文本框,并使用循环在“ ID”之后调用该文本框,因此每个文本框都具有唯一的名称。 I am then using a post method to send values to a second page which will update the 'marks' column with the value the user has just put in.. this is where I am finding my problem! 然后,我使用post方法将值发送到第二个页面,该页面将使用用户刚刚输入的值更新“标记”列。这是我发现问题的地方!

This is the initial page. 这是初始页面。

<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>
`

Then the update is done here which is where I seem to be having a problem: 然后更新在这里完成,这似乎是我遇到的问题:

<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>

When I echo $_POST[$i'] it shows the correct values but does not update the DB, and I'm not about ready to throw my laptop in the bin! 当我回显$ _POST [$ i']时,它会显示正确的值,但不会更新数据库,因此我不准备将笔记本电脑扔进垃圾箱! ha! 哈! I know it is prob going to be something stupid I just can't see what, so any help would be appreciated. 我知道这很可能是愚蠢的,但我什至看不到,所以我们将不胜感激。

You're missing the single quotes in your update query. 您在更新查询中缺少单引号。 This would help: 这将有助于:

$insertsql = "UPDATE `members` SET `mark` = '" . $_POST[$i] . "' WHERE `Id` = '" . $row['Id'] . "' ;"; 

you are also not running the mysql_query query command for the update 您也没有运行mysql_query查询命令进行更新

lastly you are using the mysql php commands which are deprecated. 最后,您正在使用不推荐使用的mysql php命令。 Use mysqli or pdo instead. 改用mysqli或pdo。 and don't forget to escape data in your queries to prevent sql injections 并且不要忘记在查询中转义数据以防止sql注入

问题是这里的单引号,强制使用文字'$ i',这可能不是$ _POST中的键

echo $_POST["$i"];

使用变量时,无需使用引号:$ _POST [$ id];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM