PHP - 按 ID 更新记录


PHP- Updating a record by ID

所以基本上我有一张桌子,上面有讲师和他们的详细信息。有两个选项可用于删除或编辑记录。我已经设法让删除工作,但没有编辑记录。当我单击编辑时,我的编辑用户表单出现,但我无法让它将更改保存到我的数据库。它也不会输出我不成功的消息或任何东西,只是再次回到表中。

这是我的代码

编辑用户表单

    <form class="register" method="post" autocomplete="off" action="<?php echo $_SERVER['PHP_SELF'];?>">
        <div>
            <h2>Edit Lecturer Form </h2>
            <table class="form">
                <tr>
                    <td>
                        <label>
                            <span>First Name</span><input name="lectFirstName" type="text" required="true" title="First Name (up to 45 Characters)" pattern="[a-zA-Z0-9óáéí']{1,48}">
                            <span>Last Name</span><input name="lectLastName" type="text" required="true" title="Last Name (up to 45 Characters)" pattern="[a-zA-Z0-9óáéí']{1,48}">
                            <span>Password</span><input name="lectPass1" type="password" required="true" title="Password (min 5 to 8 Characters)" pattern="[A-Za-z-0-9_]{5,8}">
                            <span>Re-enter Password</span><input name="lectPass2" type="password" required="true" title="Password (5 to 8 Characters a-z,A-Z,0-9 and underscore)" pattern="[A-Za-z-0-9_]{5,8}">
                        </label>
                    </td>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td>
                        <label>
                            <span>Hit Enter to finish</span>
                            <input name="send" type="submit" id="sendButton" value="Enter">
                        </label>
                    </td>
                </tr>
            </table>
        </div>
    </form>

编辑代码

$table='lecturer';  
$PK="LectID";
    if(isset($_POST['editRecord'])) //edit button has a name of editRecord
    {
        if(isset($_POST['send'])) //send button in edit user form has name of send
        {
            $selectedID=$_POST['LectID'];

            $lectFirstName=$conn->real_escape_string($_POST['lectFirstName']);
            $lectLastName=$conn->real_escape_string($_POST['lectLastName']);
            $pass1=$conn->real_escape_string($_POST['lectPass1']);
            $pass2=$conn->real_escape_string($_POST['lectPass2']);
            if ($pass1===$pass2)
            {
                $sqlUpdate="UPDATE $table SET FirstName='$lectFirstName', LastName='$lectLastName', password='$pass1' where $PK='$selectedID'";
                if(queryEdit($conn,$sqlInsert)==1) 
                {
                    echo "<h3>New data inserted successfully</h3>";
                }
                else
                {
                    echo "<h3>Edit unsuccessful</h3>";
                }
            }
            else
            {
                echo "<p>Passwords dont match - data not entered";
            }
            $sqlData="SELECT * FROM $table WHERE LectID='$lectID'";  //get the data from the table
            $sqlTitles="SHOW COLUMNS FROM $table";  //get the table column descriptions
            //execute the 2 queries
            $rsData=getTableData($conn,$sqlData);
            $rsTitles=getTableData($conn,$sqlTitles);
            //check the results
            $arrayData=checkResultSet($rsData);
            $arrayTitles=checkResultSet($rsTitles);
            //use resultsets to generate HTML tables
            generateTable($table, $arrayTitles, $arrayData);
            //close the connection
            $conn->close();
        }
            include 'FORMS/editUserForm.html';
    }
    else //this is the first time the form is loaded
    {
        //display table with delete options
        //Query string
        $sqlData="SELECT * FROM $table";  //get the data from the table
        $sqlTitles="SHOW COLUMNS FROM $table";  //get the table column descriptions

        //execute the 2 queries
        $rsData=getTableData($conn,$sqlData);
        $rsTitles=getTableData($conn,$sqlTitles);
        //check the results
        $arrayData=checkResultSet($rsData);
        $arrayTitles=checkResultSet($rsTitles);
        //use resultsets to generate HTML tables with DELETE button
        generateDeleteEditTable($table, $PK, $arrayTitles, $arrayData);
        //close the connection
        $conn->close();
    }

"编辑记录"按钮

   <form class="small_button" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
        <button class="smallBtn" name="editRecord" type="submit" value="<?php echo $id;?>"><?php echo $buttonText; ?></button>
    </form>

查询编辑功能

函数查询编辑($connection,$sql)

{
    try {
        if ($connection->query($sql)===TRUE)  //execute the insert sql
        {
            return 1;  //if successful
        }
        else
        {
            return 0;  //if not successful
        }
    }
    //catch exception
    catch(Exception $e) {
        if (__DEBUG==1)
        {
            echo 'Message: ' .$e->getMessage();
            exit('<p class="warning">PHP script terminated');
        }
        else
        {
            header("Location:".__USER_ERROR_PAGE);
        }
    }
}
您需要将

要编辑的记录的 ID 存储在表单中(在 <input type="hidden" name="LectID" ... /> 字段中)。服务器需要知道当表单数据从客户端到达时要编辑的 ID。

<form class="small_button" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
        <button class="smallBtn" name="editRecord" type="submit" value="<?php echo $id;?>"><?php echo $buttonText; ?></button>
</form>

在您的表单中,您没有任何名为"send"的 HTML 元素。

只有在isset($_POST['send']