Jump to content

PHP Update Prepared Statement Script Help


Mickeyatty

Recommended Posts

Hi, I am just getting a blank page and I am unable to figure out why such a simple concept is not working. Any help would be greatly appreciated. 

<?php
  //Error Check
     ini_set('display_errors', 1); 
     ini_set('display_startup_errors', 1); 
     error_reporting(E_ALL);

   try {
   $conn = new PDO("mysqli:host=$servername;dbname=$dbname", $username,
    $password);
  // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

     $statement = $conn->prepare("UPDATE test SET title= ? WHERE id= ?");
     $statement->bind_param('si', $title,$id);
     $statement->execute();
     if ($statement->affected_rows >0) {
      echo "Record updated successfully";
       } else {
        echo "Error updating record: " . $conn->error;
        }
       $statement->close();
         ?>
Link to comment
Share on other sites

you have a php syntax error. the try/catch syntax is incomplete. you are not seeing any errors since your code never runs when there is a php syntax error. you should set the error_reporting/display_errors setting in the php.ini on your system so that php will report and display ALL the errors it detects.

in most cases, you should not catch exceptions in your code (only when detecting duplicate data being inserted/updated.) you should normally let php catch and handle any exception.

next, the variables being used in the connection don't appear to exist, the PDO extension doesn't have a bind_param() method (it does have a bindParam() method), the two variables you are binding don't appear to exist, and instead of binding input data,you should just supply it as an array in the execute() method call.

also, if there are no affected rows, that's not a query error and using $conn->error, which isn't a PDO property anyway, won't contain any useful information. if there is a query error, an exception will be thrown and the exception handler will be executed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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