skip to Main Content

I have a manage-user.php page that list out all users. 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. 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.

Here’s my tbluser from PHPMYADMIN:

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:

<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. I placed this query on top of the edit-user.php page:

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.

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

2

Answers


  1. Since your U_ID should not change and it has been made an request with this it, he should exist. Try to update like this:

    edit-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.

    Login or Signup to reply.
  2. Okay I’ve fixed the HTML code in my edit-user.php. After I changed this, the UPDATE query works. 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:

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

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

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

    If I use the readonly attribute, the user will be able to see the textbox and its data but they cant edit it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search