编辑php / mysql表但不更新表

时间:2018-02-07 12:18:11

标签: php mysql database sql-update

我正在尝试编辑mysql表,但是当我提交表单时,表格不会更新,之前的值保持不变。我根本没有得到任何错误......

我已经尝试直接在数据库中运行更新查询,它可以工作......有人可以查看我的代码,看看他们是否可以提供帮助?

下面是我的代码:

edit.php

<?php include('server.php') ?> 
<?php

if(isset($_POST['update']))
{    
    $responseid = $_POST['responseid'];

    $response=$_POST['response'];  


{    

        //updating the table
        $result = $conn->prepare ("UPDATE response SET response= '$response' WHERE responseid=$responseid");


        header("Location: results.php");
    }
}
?>
<?php
//getting id from url
$responseid = $_GET['id'];

//selecting data associated with this particular id
$result = $conn->prepare("SELECT * FROM response WHERE responseid=$responseid");


while ($response = $result->fetch())
{ 
    $response = $res['response'];
    $student_id = $res['student_id'];
}
?>
<html>
<head>    
    <title>Edit Data</title>
</head>

<body>


    <form name="form1" method="post" action="edit.php">
        <table border="0">
            <tr> 
                <td>response</td>
                <td><input type='text' name='date' value="<?php echo $response;?>"</td>
            </tr>
            <tr>
                <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>
</body>
</html>

results.php

<div id="table1" class="table1">    
<?php 
if(isset($_POST["submit"]))
{
$searchTerm=$_POST['search']; 

$stmt = $conn->prepare(" SELECT question.description AS question, answer.description AS answer, discipline.name AS name, response.responseid AS responseid, response.response AS response, response.student_id AS student_id, response.Date_Time AS Date
        FROM response
        INNER JOIN answer ON response.question_id = answer.answerid
        INNER JOIN question ON response.question_id = question.qid
        INNER JOIN discipline ON response.discipline_id = discipline.disciplineid WHERE Date_Time LIKE :searchTerm");
$stmt->bindValue(':searchTerm','%'.$searchTerm.'%');
$stmt->execute();
$result=0;




    /*
The above code is a query which selects attributes according to the search term
*/



echo "<table> <tr><th>Discipline</th><th>Question</th><th>Student ID</th><th>Response</th><th>Date & Time</th><th>Answer</th><th>Final Marks</th></tr>";
while ($response = $stmt->fetch())    /* This is a While loop which iterates each row */
{

echo " <tr><td>".$response["name"]."</td><td>".$response["question"]."</td><td>".$response["student_id"]."</td><td>".$response["response"]."</td><td>".$response["Date"]."</td><td><input type='text' name='date' value=". $response["answer"]."></td><td><a href=\"edit.php?id=$response[responseid]\">Edit</a></td></tr> "; 
    $result++;


}



}  /* This bit of code closes the connection with the database */
?>


    </div>

please click this link to see my database

1 个答案:

答案 0 :(得分:1)

使用预准备语句进行更新(类似于您在第二个列表中的选择中执行此操作的方式)...

//updating the table
$result = $conn->prepare ("UPDATE response 
                            SET response= :response 
                            WHERE responseid=:responseid");
$result->bindValue(':response',$response);
$result->bindValue(':responseid', $responseid);
$result->execute();

同时检查$_POST的内容,因为我认为您的字段名称错误(认为它们是'date'和'id')...

<form name="form1" method="post" action="edit.php">
    <table border="0">
        <tr> 
            <td>response</td>
            <td><input type='text' name='response' value="<?php echo $response;?>"</td>
        </tr>
        <tr>
            <td><input type="hidden" name="responseid" value=<?php echo $_GET['id'];?>></td>
            <td><input type="submit" name="update" value="Update"></td>
        </tr>
    </table>
</form>