更新查询不起作用

时间:2019-03-21 20:07:09

标签: php mysql database sql-update crud

我正在尝试开发一个小的“待办事项”应用程序。该应用程序的数据存储在数据库中,并且需要执行所有CRUD操作。现在,选择,插入和删除就可以了。我仍然坚持更新。 index.php页面如下所示:

<?php
    session_start();
    require_once 'connect.php';

    if (isset($_POST['DeleteTask'])) {
        $sqldelete = "DELETE FROM Tasks WHERE dbTaskID = :bvTaskID";
        $stmtdelete = $db->prepare($sqldelete);
        $stmtdelete->bindValue(':bvTaskID', $_POST['taskID']);
        $stmtdelete->execute();
        echo "<div>Task successfully deleted</div>"; 
    }

    if (isset($_POST['theSubmit'])){
        echo '<p>New task added</p>';

        $formfield['ffTaskName'] = trim($_POST['taskName']);
        $formfield['ffTaskDue'] = trim($_POST['taskDue']);

        if(empty($formfield['ffTaskName'])){$errormsg .= "<p>Task field is empty.</p>";}
        if(empty($formfield['ffTaskDue'])){$errormsg .= "<p>Deadline field is empty.</p>";}

        if ($errormsg != "") {
                echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
                echo $errormsg;
                echo "</div>";
        } else {
            try {
                $sqlinsert = 'INSERT INTO Tasks (dbTaskName, dbTaskDue, dbTaskDone)
                VALUES (:bvTaskName, :bvTaskDue, :bvTaskDone)';
                $stmtinsert = $db->prepare($sqlinsert);
                $stmtinsert->bindValue(':bvTaskName', $formfield['ffTaskName']);
                $stmtinsert->bindValue(':bvTaskDue', $formfield['ffTaskDue']);
                $stmtinsert->bindValue(':bvTaskDone', 0);
                $stmtinsert->execute();
                echo "<div><p>There are no errors.  Thank you.</p></div>";
            } catch(PDOException $e){
                echo 'ERROR!!!' .$e->getMessage();
                exit();
            }
        }   
    }

    $sqlselect = "SELECT * from Tasks";
    $result = $db->prepare($sqlselect);
    $result->execute();
?>


<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>To Do Application</title>
    </head>
    <body> 

        <h1><u>To-Do List</u></h1>
        <table border>
            <tr>
                <th>Task</th>
                <th>Deadline</th>
                <th>Status</th>
                <th>Complete</th>   
                <th>Edit</th>
                <th>Delete</th>
            </tr>
            <?php           
                while ($row = $result->fetch()) {
                    if ($row['dbTaskDone'] == 0) {
                        $status = "Unfinished";
                    } else {
                        $status = "Finished";
                    }
                    echo '<tr><td>' . $row['dbTaskName'] 
                    . '</td><td>' . $row['dbTaskDue']
                    . '</td><td>' . $status;

                    /*if ($status == "Unfinished"){
                        echo '</td><td>';
                        echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
                        echo '<input type="hidden" name="taskID" value"' . $row['dbTaskID'] . '">';
                        echo '<input type="submit" name="CompleteTask" value="Complete Task">';
                        echo '</form>';
                    }*/

                    echo '</td><td>';
                    echo '<form action="updateTask.php" method="post">';
                    echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
                    echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';
                    echo '</form></td><td>';

                    echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
                    echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
                    echo '<input type="submit" name="DeleteTask" value="Delete Task">';
                    echo '</td></tr>';
                }
            ?>
        </table>   
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="toDoForm">
            <fieldset><legend>New Task</legend>
                <table>
                    <tr>
                        <th>Task</th>
                        <td><input type="text" name="taskName" id="taskName"
                        value="<?php echo $formfield['ffTaskName']; ?>"></td>
                    </tr><tr>
                        <th>Deadline</th>
                        <td><input type="text" name="taskDue" id="taskDue"
                        value="<?php echo $formfield['ffTaskDue']; ?>"></td>
                    </tr>
                </table>
                <input type="submit" name = "theSubmit" value="Add Task">
            </fieldset>
        </form>
    </body>
</html>

每条记录都显示一个“编辑”按钮,该按钮从“任务”表中获取PK,并将其发送到updateTask.php页面:

<?php 
    require_once 'connect.php';
    $errormsg = "";
    if (isset($_POST['EditTask']) ) {
        $formfield['ffTaskID'] = $_POST['taskID'];
        $sqlselect = "SELECT * FROM Tasks WHERE dbTaskId = :bvTaskID";
        $result = $db->prepare($sqlselect);
        $result->bindValue(':bvTaskID', $formfield['ffTaskID']);
        $result->execute();
        $row = $result->fetch(); 

        if( isset($_POST['theEdit']) )
        {   
            $formfield['ffTaskID'] = $_POST['taskID'];
            $formfield['ffTaskName'] = trim($_POST['taskName']);
            $formfield['ffTaskDue'] = trim($_POST['taskDue']);

            if(empty($formfield['ffTaskName'])){$errormsg .= "<p>Task field is empty.</p>";}
            if(empty($formfield['ffTaskDue'])){$errormsg .= "<p>Deadline field is empty.</p>";}

            if ($errormsg != "") {
                echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
                echo $errormsg;
                echo "</div>";              
            } else {
                try
                {
                    $sqlUpdate = "UPDATE Tasks SET dbTaskName = :bvTaskName,
                                  dbTaskDue = :bvTaskDue
                                  WHERE dbTaskID = :bvTaskID";
                    $stmtUpdate = $db->prepare($sqlUpdate);
                    $stmtUpdate->bindvalue(':bvTaskName', $formfield['ffTaskName']);
                    $stmtUpdate->bindvalue(':bvTaskDue', $formfield['ffTaskDue']);
                    $stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);
                    $stmtUpdate->execute();
                }
                catch(PDOException $e)
                {
                    echo 'ERROR!!!' .$e->getMessage();
                    exit();
                }
            }
        }
    }   
?>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>To Do Application</title>
    </head>
    <body> 
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="editForm">
            <fieldset><legend>Edit Task</legend>
                <table>
                    <tr>
                        <th>Task</th>
                        <td><input type="text" name="taskName" id="taskName"
                        value="<?php echo $row['dbTaskName'];?>" ></td>
                    </tr><tr>
                        <th>Deadline</th>
                        <td><input type="text" name="taskDue" id="taskDue"
                        value="<?php echo $row['dbTaskDue']; ?>"></td>
                    </tr>
                    <tr>
                        <th>Submit Changes</th>
                        <input type="hidden" name="taskID" value="<?php echo $_formfield['ffTaskID']; ?>">
                        <td><input type="submit" name="theEdit" value="Submit Changes">
                </table>

            </fieldset>
        </form> 
    </body>
</html>

“名称”和“截止日期”字段将根据从最后一页传递的PK值进行适当填充。但是,每当我按下“提交更改”按钮时,更新似乎都不会执行。页面刚刚刷新,我看到表数据在数据库中保持不变。

1 个答案:

答案 0 :(得分:0)

解决了问题!

我发现了几个问题。

1。)在updateTask.php中,我将第二个if语句嵌套在第一个if语句中。因此,它在页面加载时正在运行更新查询,而对数据没有任何更改。因此,“ theEdit”按钮什么也不做,因为它需要运行前面的if语句的条件。

2。)在updateTask.php上表单底部的要通过“ theEdit”按钮传递的表单字段“ ffTaskID”输入错误。

  

$ _ formfield

..应该是..

  

$ formfield

此时,更新查询正常运行。

3。)“编辑”按钮的问题已修复。虽然老实说我不能确定它是如何修复的。它可能与问题的第一部分有关。因此,当解决该问题时,也是如此。

无论哪种方式,所有功能似乎都可以正常运行。再次感谢所有发表评论和提供帮助的人。