Jump to content

Updating Not Working


Moorcam
 Share

Go to solution Solved by Barand,

Recommended Posts

Howdy folks,

I have decided, after a discussion with Barand, to finally hang up the MySQLi shoes and move over to the dark side of PDO.

I am trying to Update a profile, for example, but it is not working. No errors or anything. New to PDO so would love some help on figuring out where I am going wrong. Probably everywhere knowing me lol.

Here is the dreaded code:

if(isset($_POST['submit'])){
    $id = trim($_SESSION['id']);
    //$trn_date = trim($db, date("Y-m-d H:i:s"));
    //$password = $db->real_escape_string(md5($_POST['password']));
    $image = trim($_FILES['image']['name']);
    $name = trim($_POST['name']);
    $phone = trim($_POST['phone']);
    $email = trim($_POST['email']);
    $address = trim($_POST['address']);
    $license_number = trim($_POST['license_number']);
    $position = trim($_POST['position']);
    $role = trim($_POST['role']);

    $submittedby = trim($_SESSION["username"]);
  	// image file directory
  	$target = "images/".basename($image);
  	    if(!empty($_FILES['image']['name'])) {
    $sql = "UPDATE users SET name = :name, email = :email, phone = :phone, address = :address, license_number = :license_number, position = :position, role = :role, submittedby = :submittedby, image = :image";
  	    }else{
    $sql = "UPDATE users SET name = :name, email = :email, phone = :phone, address = :address, license_number = :license_number, position = :position, role = :role, submittedby = :submittedby";
  	    }
     $stmt= $db->prepare($sql);
  	if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
  		$msg = "Image uploaded successfully";
  	}else{
  		$msg = "Failed to upload image";
  	}
     if(!$stmt){ 
        if ($stmt->execute()){
        $message = '  <i class="fa fa-check text-danger"> Something went wrong please contact the server admin.</i>';
     } else{ 
            
       $message = '  <i class="fa fa-check text-success"> Record Updated!</i>';

    } 

 }
}

Any help folks would be appreciated :)

Link to comment
Share on other sites

1. Organize your code ie, do the file upload and save before you get to the query stuff.  Very confusing read here

2. Once you can separate your two tasks and organize the different parts of code you may have a better view which will point out a glaring error in your logic.  Hint:  It is this line: if(!$stmt)

3.  Read up on your use of pdo, especially how you incorporate the arguments into your process.

 

Link to comment
Share on other sites

  • Solution

I'd suggest something like this

if($_SERVER['REQUEST_METHOD']=='POST') {
    
    $post = array_map('trim', $_POST);
    unset($post['submit']);                           // not wanted on journey
    
    $post['id'] = trim($_SESSION['id']);
    $post['submittedby'] = trim($_SESSION["username"]);

    if ($_FILES['image']['error'] = UPLOAD_ERR_OK)  {
        // image file directory
        $target = "images/".basename($_FILES['image']['name']);
        if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
          $msg = "Image uploaded successfully";
          $post['image'] = $target;
      }else{
          $msg = "Failed to upload image";
          $post['image'] = null;
      }
    } else {
        $post['image'] = null;
    }
    
    try {
        $sql = "UPDATE users SET name = :name, email = :email, phone = :phone, address = :address, license_number = :license_number, 
                position = :position, role = :role, submittedby = :submittedby, image = :image
                WHERE id = :id
                ";
        $stmt= $db->prepare($sql);
        $stmt->execute($post);
        $message = '  <i class="fa fa-check text-success"> Record Updated!</i>';
    }
    catch (PDOException $e) {
        $message = '  <i class="fa fa-check text-danger"> Something went wrong please contact the server admin.</i>';
    }
} 

 

Link to comment
Share on other sites

Ok, I am having a crack here so please be gentle.

include_once('includes/header.php');

if(isset($_POST['submit'])){
    $id = trim($_SESSION['id']);
    //$trn_date = trim($db, date("Y-m-d H:i:s"));
    //$password = $db->real_escape_string(md5($_POST['password']));
    $image = trim($_FILES['image']['name']);
    $name = trim($_POST['name']);
    $phone = trim($_POST['phone']);
    $email = trim($_POST['email']);
    $address = trim($_POST['address']);
    $license_number = trim($_POST['license_number']);
    $position = trim($_POST['position']);
    $role = trim($_POST['role']);

    $submittedby = trim($_SESSION["username"]);
  	// image file directory
  	$target = "images/".basename($image);
  	    if(!empty($_FILES['image']['name'])) {
    $stmt = $db->prepare("UPDATE users SET name = ?, email = ?, phone = ?, address = ?, license_number = ?, position = ?, role = ?, submittedby = ?, image = ? WHERE id = ?");

    if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
        $msg = "Image uploaded successfully";
    }else{
        $msg = "Failed to upload image";
    }
  	    }else{
    $stmt = $db->prepare("UPDATE users SET name = ?, email = ?, phone = ?, address = ?, license_number = ?, position = ?, role = ?, submittedby = ? WHERE id = ?");
  	    }

    $stmt = $db->execute(array($name, $email, $phone, $address, $license_number, $position, $role, $submittedby, $image));
 if($stmt->execute()){
    echo 'Done';
    
    }else{
        echo 'Not Done';
    }
}

The above gives an error:

Fatal error: Uncaught Error: Call to undefined method PDO::execute()

That's this line:

$stmt = $db->execute(array($name, $email, $phone, $address, $license_number, $position, $role, $submittedby, $image));

I am at a complete roadblock here.

 

Edited by Moorcam
Link to comment
Share on other sites

2 minutes ago, Barand said:

I'd suggest something like this

if($_SERVER['REQUEST_METHOD']=='POST') {
    
    $post = array_map('trim', $_POST);
    unset($post['submit']);                           // not wanted on journey
    
    $post['id'] = trim($_SESSION['id']);
    $post['submittedby'] = trim($_SESSION["username"]);

    if ($_FILES['image']['error'] = UPLOAD_ERR_OK)  {
        // image file directory
        $target = "images/".basename($_FILES['image']['name']);
        if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
          $msg = "Image uploaded successfully";
          $post['image'] = $target;
      }else{
          $msg = "Failed to upload image";
          $post['image'] = null;
      }
    } else {
        $post['image'] = null;
    }
    
    try {
        $sql = "UPDATE users SET name = :name, email = :email, phone = :phone, address = :address, license_number = :license_number, 
                position = :position, role = :role, submittedby = :submittedby, image = :image
                WHERE id = :id
                ";
        $stmt= $db->prepare($sql);
        $stmt->execute($post);
        $message = '  <i class="fa fa-check text-success"> Record Updated!</i>';
    }
    catch (PDOException $e) {
        $message = '  <i class="fa fa-check text-danger"> Something went wrong please contact the server admin.</i>';
    }
} 

 

Just saw this now. Thanks heaps mate. It works.

I will get there eventually :)

Link to comment
Share on other sites

Your first attempt at using PDO with named parms was better.  What you left out was the array of those parms being assigned to real values.

Instead of using the ? marks, get used to using the :name like you did the first time.  Then assign an array like this:

	$parms = array(
	     'name'=>$name,
	    'id'=>$id,
	    ...
	   ...
	);
	$stmt->execute($parms);
	

Much easier to read later on when you are doing maintenance as well as now when you are debugging.

Edited by ginerjm
  • Like 1
Link to comment
Share on other sites

12 hours ago, ginerjm said:

Your first attempt at using PDO with named parms was better.  What you left out was the array of those parms being assigned to real values.

Instead of using the ? marks, get used to using the :name like you did the first time.  Then assign an array like this:

	$parms = array(
	     'name'=>$name,
	    'id'=>$id,
	    ...
	   ...
	);
	$stmt->execute($parms);
	

Much easier to read later on when you are doing maintenance as well as now when you are debugging.

Thanks mate. Will do. Bit of a massive learning curve but no doubt I will get there. Thanks for your help guys.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.