[英]Updating mysql row not working properly (PDO)
我想做的就是按照phpMyAdmin处理它的方式更新mysql行(在这种情况下,仅用户名)(单击输入字段,更改值,按enter)。
据我所知,我的代码没有错误。 提交后,我也看不到任何php错误。
这是主页的代码:
// Here, I select the rows I need to be displayed first. <?php $q="SELECT gebruikersnaam,wachtwoord,id FROM login WHERE rollen !=1 AND rollen !=2" ; $stmt=$ conn->prepare( $q ); $stmt->execute(); ?> // Table to show the rows <div id="box"> <!-- Table --> <center> <table> <thead> <tr> <th style='color:#e20363;'>ID <br> </th> <th>Gebruikersnaam <br> </th> <th>Wachtwoord <br> </th> <th>Actie <br> </th> </tr> </thead> <tbody> </div> <?php while($row=$ stmt->fetch()){ $id=$row['id']; echo " <tr>"; echo " <td style='color:#e20363; text-align:center;'>{$row['id']}</td>"; // Here, I made the input field show the mysql row and made a form to submit when I press enter echo " <form action='update.php' method='post'>"; echo " <td> <input type='text' value='{$row[' gebruikersnaam ']}' name='gebruikersnaam'> </td>"; echo "</form>"; echo " <td style='text-align:center; padding:10px;'> <input type='text' value='{$row[' wachtwoord ']}' name='password'> </td>"; echo ' <center> <td> <a href="delete.php?id='.$row['id'].'"> <img id="remove_user" src="images/remove.png" width="60px" style="padding:13px;"> </a> </td> </center>'; echo "</tr>"; } ?> </tbody> </table>
用户单击提交后,update.php尝试处理更新查询。 这是我的update.php代码:
<?php if(isset($_POST['username'])){ $host = 'localhost'; $user = 'root'; $pass = 'root'; $database = 'users'; $pdo = new PDO("mysql:host=$host;dbname=$database", $user, $pass); $sql = "UPDATE `login` SET `gebruikersnaam` = :username"; //Prepare our UPDATE SQL statement. $statement = $pdo-> prepare($sql); //Bind our value to the parameter :id. $statement->bindValue(':username', $_POST['username']); //Execute our UPDATE statement. $update = $statement->execute(); if($update){ header('Location: account_verwijderen.php'); } }; ?>
一切似乎都正常。 但是当我尝试更新查询时(按enter,它确实将我发送到update.php,但似乎无济于事,因为我到主页的重定向代码不起作用。
我在这里做错了什么?
您的问题可能是您正在绑定':username',但应该绑定'username'-您在bind语句中不需要冒号。
您还应该这样定义绑定的日期类型:
bindValue('id', $id, PDO::PARAM_INT);
尝试在if (success) { bit that fires die(PDOStatement->errorInfo());
之后添加else if (success) { bit that fires die(PDOStatement->errorInfo());
并且您会看到SQL语法中的任何错误。 删除它或添加到文件日志,然后再上线。
也
您在更新查询中没有“ where”语句,因此它必须是:
UPDATE `login` SET `gebruikersnaam` = :username where id = :id;
这意味着您也需要从POST捕获登录ID。 没有它,查询将更新表中的所有记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.