繁体   English   中英

编辑 php / mysql 表但不更新表

[英]Editing php / mysql table but does not update table

我正在尝试编辑一个 mysql 表,但是当我提交表单时,该表没有更新,并且以前的值保持不变。 我也没有收到任何错误...

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

下面是我的代码:

编辑.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>

结果.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>

请点击此链接查看我的数据库

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

//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的内容,因为我认为您的字段名称有误(认为它们是“日期”和“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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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