Alisha Lamichhane
Alisha Lamichhane

Reputation: 514

Update query not working if added the code to update image and pdf

Update query not working if added the code to upload image and pdf. I have the same code for insert and update except prepare and execute, insert query works very fine, update query doesn't. I have also included enctype="multipart/form-data" in the form so that I can get data from $_FILES as well. Moreover, I have used $_FILES['photo']['tmp_name'] and $_FILES['pdf']['tmp_name'] in order to move them using function move_uploaded_file move_uploaded_file($_FILES['photo']['tmp_name'], 'destination'); and move_uploaded_file($_FILES['pdf']['tmp_name'], 'destination');

function edit_profile($pid)
{
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        echo "<pre>";
        print_r($_POST);
        print_r($_FILES);
        echo "</pre>";
        $fullname = $_POST['fullname'];
        $email = $_POST['email'];
        $contact = $_POST['contact'];
        $sel_post = $_POST['sel_post'];
        $txt_post = $_POST['post'];
        $post = "";
        if(empty($txt_post))
        {
            $post = $sel_post;
        }
        else
        {
            $post = $this->add_new_post($txt_post);
        }

        if(empty($fullname) || empty($contact))
        {
            array_push($this->errors, MEND_FIELD_ERROR);
            return;
        }

        if(!empty($_FILES['photo']['name']))
            {
                $photo = $_FILES['photo'];
                $allowed_ext = array('png','jpg', 'pdf','jpeg', 'bmp', 'gif');
                $allowed_size = 20000000;
                $tmp_photo = $photo['tmp_name'];
                $photo_size = $photo['size'];
                $photo_error = $photo['error'];
                $photo_ext = explode('.',$photo['name']);
                $photo_ext = strtolower(end($photo_ext));

                if(in_array($photo_ext,$allowed_ext))
                {
                    if($photo_size <= $allowed_size)
                    {
                        $photo_new_name = time()."_".uniqid('',true).'.'.$photo_ext;
                        $upload_destination = './cdn/uploads/profile/'.$photo_new_name;
                        if(move_uploaded_file($tmp_photo,$upload_destination))
                        {
                            $photo_to_db = $photo_new_name;
                        }
                        else
                        {
                            array_push($this->errors, STORAGE_ERROR);
                            return;
                        }

                    }
                    else
                    {
                        array_push($this->errors, $document_name.' : '.FILE_SIZE_ERROR);
                        return;
                    }
                }
                else
                {
                    array_push($this->errors, $photo_ext.' : '.FILE_EXT_ERROR);
                    return;
                }
            }

            if(!empty($_FILES['pdf']['name']))
            {
                $pdf = $_FILES['pdf'];
                $allowed_pdf_ext = array('pdf');
                $allowed_pdf_size = 20000000;
                $tmp_pdf = $pdf['tmp_name'];
                $pdf_size = $pdf['size'];
                $pdf_error = $pdf['error'];
                $pdf_ext = explode('.',$pdf['name']);
                $pdf_ext = strtolower(end($pdf_ext));

                if(in_array($pdf_ext,$allowed_pdf_ext))
                {
                    if($photo_size <= $allowed_pdf_size)
                    {
                        $pdf_new_name = time()."_".uniqid('',true).'.'.$pdf_ext;
                        $upload_pdf_destination = './cdn/uploads/profile_pdf/'.$pdf_new_name;
                        if(move_uploaded_file($tmp_pdf,$upload_pdf_destination))
                        {
                            $pdf_to_db = $pdf_new_name;
                        }
                        else
                        {
                            array_push($this->errors, STORAGE_ERROR);
                            return;
                        }

                    }
                    else
                    {
                        array_push($this->errors, $document_name.' : '.FILE_SIZE_ERROR);
                        return;
                    }
                }
                else
                {
                    array_push($this->errors, $photo_ext.' : '.FILE_EXT_ERROR);
                    return;
                }
            }

        $statement = $this->db->prepare("UPDATE `profiles` SET `fullname`=?,`email`=?,`contact`=?,`post`=?, `photo`=?, `pdf`=? WHERE `pid`=?");
        if($statement->execute([$fullname,$email,$contact,$post,$pid, $pdf_to_db, $photo_to_db]))
        {
            ExitThis::send_to(URL.'profile/view_profile?id='.$pid);
        }
        else
        {
            array_push($this->errors, DATABASE_ERROR);
            return;
        }
    }

}

The above code will return me to the view_profile page as if the update query worked properly, however, the data remains the same before the update -- no change.

Edit: After debugging $statement before executing it get this:

PDOStatement Object
(
    [queryString] => UPDATE `profiles` SET `fullname`=?,`email`=?,`contact`=?,`post`=?, `photo`=?, `pdf`=? WHERE `pid`=?
)

Upvotes: 0

Views: 57

Answers (3)

Bryan Nivarez
Bryan Nivarez

Reputation: 34

    $statement = $this->db->prepare("UPDATE `profiles` SET 
    `fullname`=?,`email`=?,`contact`=?,`post`=?, `photo`=?, `pdf`=? WHERE 
    `pid`=?");
     if($statement->execute([$fullname,$email,$contact,$post,$photo_to_db
     $pdf_to_db,$pid]))
     {
          ExitThis::send_to(URL.'profile/view_profile?id='.$pid);
       }
     else
    {
       array_push($this->errors, DATABASE_ERROR);
        return;
      }

remove $pid on your update statement and also check your parameters please try this code

Upvotes: 0

Rohit Ghotkar
Rohit Ghotkar

Reputation: 803

This issue is might be the wrong sequence of parameters passed, try by following sequence:

if($statement->execute([$fullname,$email,$contact,$post,$photo_to_db,$pdf_to_db,$pid]))

Upvotes: 1

Sergey Poltaranin
Sergey Poltaranin

Reputation: 544

You pass variables to execute method using wrong order.

...db->prepare("UPDATE `profiles` SET `fullname`=?,`email`=?,`contact`=?,`post`=?, `photo`=?, `pdf`=? WHERE `pid`=?");

And next you calls execute with these variables:

...->execute([$fullname,$email,$contact,$post,$pid, $pdf_to_db, $photo_to_db]))

Last 3 should be like $photo_to_db, $pdf_to_db, $pid. You pass wrong pid, so that's why you don't see update result.

Also you can use named parameters: http://php.net/manual/en/pdostatement.execute.php#example-1072

Upvotes: 0

Related Questions