简体   繁体   English

更新mysql行无法正常工作(PDO)

[英]Updating mysql row not working properly (PDO)

What i'm trying to do is to update a mysql row ( in this case, only the username) just the way phpMyAdmin handles it ( click on input field, change value, press enter ). 我想做的就是按照phpMyAdmin处理它的方式更新mysql行(在这种情况下,仅用户名)(单击输入字段,更改值,按enter)。

As far as I can see, I have no errors in my code. 据我所知,我的代码没有错误。 I also don't see any php errors coming up after submitting it. 提交后,我也看不到任何php错误。

Here is the code for main page: 这是主页的代码:

 // 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> 

After user clicks submit, update.php tries to handle the update query. 用户单击提交后,update.php尝试处理更新查询。 Here is my update.php code: 这是我的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'); } }; ?> 

All seems to be working fine. 一切似乎都正常。 but when I try to update the query ( press enter , it does send me to update.php , but seems to be doing nothing as my redirect code to the main page doesn't work. 但是当我尝试更新查询时(按enter,它确实将我发送到update.php,但似乎无济于事,因为我到主页的重定向代码不起作用。

What am I doing wrong here ? 我在这里做错了什么?

Your issue is probably that you are binding ':username' but should be binding 'username' - you don't need the colon in the bind statement. 您的问题可能是您正在绑定':username',但应该绑定'username'-您在bind语句中不需要冒号。

You should define the date type your binding too at so: 您还应该这样定义绑定的日期类型:

bindValue('id', $id, PDO::PARAM_INT);

try adding an else after if (success) { bit that fires die(PDOStatement->errorInfo()); 尝试在if (success) { bit that fires die(PDOStatement->errorInfo());之后添加else if (success) { bit that fires die(PDOStatement->errorInfo()); and you'll see any errors in the SQL syntax. 并且您会看到SQL语法中的任何错误。 remove it or add to file logging before you go live though. 删除它或添加到文件日志,然后再上线。

Also

You have no 'where' statement in the update query so it needs to be: 您在更新查询中没有“ where”语句,因此它必须是:

UPDATE `login` SET `gebruikersnaam` = :username where id = :id;

This means you need to be capturing the ID of the login from the POST too. 这意味着您也需要从POST捕获登录ID。 Without it the query will update all the records in the table. 没有它,查询将更新表中的所有记录。

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

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