php mysql UPDATE命令不会更新

时间:2014-05-16 19:46:21

标签: php html mysql

将所有数据加载到表中,每行作为按钮。按下按钮后,必须更新行。但是当我点击按钮时,没有任何反应。行上的文本框返回其原始值...它真的开始关闭我了

<!DOCTYPE html> 

    <head>
        <title>Edit Students</title>
    </head>

<?php

            $user = 'root';     //Database username ("Root for xampp")
            $pass = '';             //Database password ("empty for exampp")
            $db = 'dragondrivingschooldb';      //Name of database

            $con = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");  //Create new data connection ('name of host/server', user, password, database name)

            if (isset($_POST['btnUpdate'])) {   //Once Update button pressed perform this code

                $updatequery = mysqli_query($con, "UPDATE booking SET FirstName='$_POST[txtfirstname]' WHERE BookingID='$_POST[txtid]' "); //excute UpDate Query

            };  

            $sql = mysqli_query($con, "SELECT * FROM booking"); //Select All from Booking

        //Create Headers for table

    echo "<table border='1'>                
    <tr>
        <th></th>
        <th>Booking ID</th>
        <th>First Name</th>

    </tr>";

    //Show Edit Form///////////////////////////////////////////////////////////////////////////////////////////////////
    while($row = mysqli_fetch_array($sql)) {    //Run sql code till there are no more rows to import 

    echo "<form action=EditStudent.php method=post>";    //Run update code at top of this page

    //Populate table with query (sql)

    echo "<tr>";
        echo "<td> <input name=update type=submit value=update /> </td>";           //once press update row this button is apart of
        echo "<td> <input type=text value=" . $row['BookingID'] . " name=txtid /> </td>";
        echo "<td> <input type=text value=" . $row['FirstName'] . " name=txtfirstname /> </td>";

    echo "</tr>";
    }


    echo "</table>";

    echo "</form>";

    mysqli_close($con);     //Close connection

    ?>


</html>

3 个答案:

答案 0 :(得分:1)

我认为BookingID是一个整数,所以你的更新行需要是:

$updatequery = mysqli_query($con, "UPDATE booking SET FirstName='" . $_POST['txtfirstname'] . "' WHERE BookingID=" . $_POST['txtid'] . ""); //excute UpDate Query

修改: 我测试了你的脚本,问题是你在while循环之外关闭了表单。现在它的工作

<!DOCTYPE html> 

<head>
    <title>Edit Students</title>
</head>

<?php

        $user = 'root';     //Database username ("Root for xampp")
        $pass = '';             //Database password ("empty for exampp")
        $db = 'all_tests';      //Name of database

        $con = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");         //Create new data connection ('name of host/server', user, password, database name)

        if (isset($_POST['btnUpdate'])) {   //Once Update button pressed perform this  code

            $updatequery = mysqli_query($con, "UPDATE test_1 SET FirstName='" . $_POST['txtfirstname'] . "' WHERE BookingID='" . $_POST['txtid'] . "'"); //excute UpDate Query

        };  

        $sql = mysqli_query($con, "SELECT *FROM test_1"); //Select All from Booking

    //Create Headers for table

echo "<table border='1'>                
<tr>
    <th></th>
    <th>Booking ID</th>
    <th>First Name</th>

</tr>";

//Show Edit Form///////////////////////////////////////////////////////////////////////////////////////////////////
while($row = mysqli_fetch_array($sql)) {    //Run sql code till there are no more rows to import 

echo "<form method=post>";    //Run update code at top of this page

//Populate table with query (sql)

echo "<tr>";
    echo "<td> <input name='btnUpdate' type='submit' value='update' /> </td>";           //once press update row this button is apart of
    echo "<td> <input type='text' value=" . $row['BookingID'] . " name='txtid' /> </td>";
    echo "<td> <input type='text' value=" . $row['FirstName'] . " name='txtfirstname' /> </td>";

echo "</tr>";
echo "</form>";
}


echo "</table>";



mysqli_close($con);     //Close connection

?>

答案 1 :(得分:-1)

首先检查$ _POST [txtid]是否有值。并确保字段名称正确或不正确。

答案 2 :(得分:-1)

html中没有名称为btnUpdate的输入字段。我能看到它的名字是更新。因此你的行:

if (isset($_POST['btnUpdate'])) {

永远不会是真的