简体   繁体   中英

Editing php / mysql table but does not update table

I am trying to edit a mysql table, however when i submit the form, the table does not get updated, and the previous value remains the same. I am not getting any errors at all either...

i have tried running the update query directly in the database, and it works...can someone have a look at my code and see if they can help?

below is my code:

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

Updating using prepared statements (similar to the way your doing it in the select in the second listing)...

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

Also check the contents of $_POST as I think you have the field names wrong (think they were 'date' and '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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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