简体   繁体   English

我的 PHP PDO 不会对数据库 PHPMYADMIN 运行更新查询

[英]My PHP PDO wont run the update query to database PHPMYADMIN

I have a manage-user.php page that list out all users.我有一个列出所有用户的manage-user.php页面。 When admin select to edit a certain user, the admin is directed to edit-user.php where admin can update all user data in the form.当管理员选择编辑某个用户时,管理员被定向到edit-user.php ,管理员可以在其中更新表单中的所有用户数据。 I can direct the page to the edit-user.php and it will load the user data, however when i want to update user's data but the sql wont let me.我可以将页面定向到edit-user.php ,它会加载用户数据,但是当我想更新用户数据但 sql 不允许我时。

Here's my tbluser from PHPMYADMIN:这是我来自 PHPMYADMIN 的tbluser

CREATE TABLE `tbluser` (
  `U_ID` int(11) NOT NULL,
  `U_USERNAME` varchar(250) NOT NULL,
  `U_PASSWORD` varchar(255) NOT NULL,
  `U_FULLNAME` varchar(255) DEFAULT NULL,
  `U_DOB` date DEFAULT NULL,
  `U_ADDRESS` varchar(255) DEFAULT NULL,
  `U_GENDER` varchar(255) DEFAULT NULL
);

Here's my table in manage-user.php that loads all user details:这是我在 manage-user.php 中加载所有用户详细信息的表:

<table>
  <thead>
    <tr>
      <th>#</th>
      <th>Full Name</th>
      <th>Login ID</th>
      <th>DOB</th>
      <th>Address</th>
      <th>Gender</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <?php 
$sql = "SELECT U_ID, U_FULLNAME, U_USERNAME, U_DOB, U_ADDRESS, U_GENDER from tbluser";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $results)
{ ?>
    <tr>
      <td>
        <?php echo htmlentities($cnt);?>
      </td>
      <td>
        <?php echo htmlentities($results->U_FULLNAME);?>
      </td>
      <td>
        <?php echo htmlentities($results->U_USERNAME);?>
      </td>
      <td>
        <?php echo htmlentities($results->U_DOB);?>
      </td>
      <td>
        <?php echo htmlentities($results->U_ADDRESS);?>
      </td>
      <td>
        <?php echo htmlentities($results->U_GENDER);?>
      </td>

      <td>
        <a href="edit-user.php?U_ID=<?php echo htmlentities($results->U_ID);?>">
          <button class="btn btn-primary">
          <i class="fa fa-edit "></i> Edit
          </button>
      </td>
    </tr>
    <?php $cnt=$cnt+1;}} ?>
  </tbody>
</table>

Here's my update query in the edit-user.php.这是我在 edit-user.php 中的更新查询。 I placed this query on top of the edit-user.php page:我将此查询放在edit-user.php页面的顶部:

if(strlen($_SESSION['slogin'])==0)
{ 
  header('location:login.php');
} else
{
  if(isset($_POST['return']))
    {
        $userid=$_POST['userid'];
        $username=$_POST['username'];
        $password=$_POST['password'];
        $fname=$_POST['fname'];
        $dob=$_POST['dob'];
        $address=$_POST['address'];
        $gender=$_POST['gender'];

        $sql="UPDATE tbluser SET 
                U_ID=:userid,
                U_FULLNAME=:fname, 
                U_USERNAME=:username, 
                U_DOB=:dob, 
                U_ADDRESS=:address, 
                U_GENDER=:gender 
                WHERE U_ID=:userid";

        $query = $dbh->prepare($sql);
        $query->bindParam(':userid',$userid,PDO::PARAM_STR);
        $query->bindParam(':username',$username,PDO::PARAM_STR);
        $query->bindParam(':password',$password,PDO::PARAM_STR);
        $query->bindParam(':fname',$fname,PDO::PARAM_STR);
        $query->bindParam(':dob',$dob,PDO::PARAM_STR);
        $query->bindParam(':address',$address,PDO::PARAM_STR);
        $query->bindParam(':gender',$gender,PDO::PARAM_STR);
        $query->execute();

        $lastInsertId = $dbh->lastInsertId();
        if($lastInsertId)
          {
            $_SESSION['msg']="User Updated successfully";
            header('location:manage-user.php');
          }
          else 
          {
            $_SESSION['error']="Something went wrong. Please try again";
            header('edit-user.php?U_ID=<?php echo htmlentities($results->U_ID);?>');
          }
    }

And here's the edit-user.php HTML codes that display all user details.这是显示所有用户详细信息的edit-user.php HTML 代码。

<form name="update" method="post">
  <?php 
                      $U_ID=$_GET['U_ID'];
                      $sql = "SELECT U_ID, U_FULLNAME, U_USERNAME, U_PASSWORD, U_DOB, U_ADDRESS, U_GENDER from tbluser where U_ID=:U_ID";
                      $query = $dbh -> prepare($sql);
                      $query->bindParam(':U_ID',$U_ID,PDO::PARAM_STR);
                      $query->execute();
                      $results=$query->fetchAll(PDO::FETCH_OBJ);
                      $cnt=1;
                      if($query->rowCount() > 0)
                      {
                        foreach($results as $results)
                          {   ?>
  <div>
    <label>USER ID</label>
    <input class="form-control" type="text" name="userid" autocomplete="off" value="<?php echo htmlentities($results->U_ID);?>" disabled />
  </div>

  <div>
    <label>Full Name</label>
    <input type="text" name="fname" value="<?php echo htmlentities($results->U_FULLNAME);?>" autocomplete="off" required />
  </div>

  <div>
    <label>Login ID</label>
    <input type="text" name="username" value="<?php echo htmlentities($results->U_USERNAME);?>" autocomplete="off" required />
  </div>

  <div>
    <label>Password</label>
    <input type="password" name="password" value="<?php echo htmlentities($results->U_PASSWORD);?>" autocomplete="off" required />
  </div>

  <div>
    <label>DOB</label>
    <input type="date" name="dob" value="<?php echo htmlentities($results->U_DOB);?>" autocomplete="off" />
  </div>

  <div>
    <label>Address</label>
    <input type="password" name="address" value="<?php echo htmlentities($results->U_ADDRESS);?>" autocomplete="off" />
  </div>

  <div>
    <label>Gender: </label>
    <select name="gender" autocomplete="off" required>
      <option selected hidden value="<?php echo htmlentities($results->U_GENDER);?>">
        <?php echo htmlentities($results->U_GENDER);?>
      </option>
      <option value="Male"> Male</option>
      <option value="Female"> Female</option>
    </select>
  </div>

  <button type="submit" name="return">Update</button>
  <?php $cnt=$cnt+1;}} ?>
</form>

Since your U_ID should not change and it has been made an request with this it, he should exist.既然你的U_ID不应该改变,并且它已经提出了这个请求,他应该存在。 Try to update like this:尝试像这样更新:

edit-user.php编辑-user.php

$sql="UPDATE tbluser SET 
                U_FULLNAME=:fname, 
                U_USERNAME=:username, 
                U_DOB=:dob, 
                U_ADDRESS=:address, 
                U_GENDER=:gender 
                WHERE U_ID=:userid";

Delete this line:删除这一行:

...
$query->bindParam(':password',$password,PDO::PARAM_STR);
...

To get error while executing do this:要在执行时出错,请执行以下操作:

if (!$query->execute()) {
    print_r($query->errorInfo());
}
$lastInsertId = $userid;

Also since you are updating and not inserting a new record, you should not check to get LAST Instert ID IT was not Inserted it is just Update.此外,由于您正在更新而不是插入新记录,因此您不应检查以获取LAST Instert ID IT was not Inserted它只是更新。

Okay I've fixed the HTML code in my edit-user.php .好的,我已经修复了我的edit-user.php的 HTML 代码。 After I changed this, the UPDATE query works.更改此设置后,UPDATE 查询有效。 I've learnt that there are 2 ways to do this.我了解到有两种方法可以做到这一点。

First thing first, I had to remove the variable disabled in the HTML code below:首先,我必须删除以下 HTML 代码中disabled的变量:

<input class="form-control" type="text" name="userid" autocomplete="off" value="<?php echo htmlentities($results->U_ID);?>" disabled />

Then, I can either choose to hide the input textbox by using the hidden attribute as below:然后,我可以选择使用hidden属性隐藏输入文本框,如下所示:

<label hidden>USER ID</label>
<input class="form-control" type="hidden" name="userid" autocomplete="off" value="<?php echo htmlentities($results->U_ID);?>" />

Or, I make my input textbox read-only by using the readonly attribute as below:或者,我通过使用readonly属性使我的输入文本框readonly ,如下所示:

<label>USER ID</label>
<input readonly class="form-control" type="text" name="userid" autocomplete="off" value="<?php echo htmlentities($results->U_ID);?>" />

If I use the hidden attribute, the user will not see the textbox and its data.如果我使用hidden属性,用户将看不到文本框及其数据。

If I use the readonly attribute, the user will be able to see the textbox and its data but they cant edit it.如果我使用readonly属性,用户将能够看到文本框及其数据,但他们无法编辑它。

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

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