簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM