1

I am trying to convert this to PDO:

echo 'sup 1';                                        
$sql = "INSERT INTO blogData(
        title,
        content,
        category) 
        VALUES ( 
        :title, 
        :content, 
        :category)";
     echo 'sup 2';                                 
$stmt = prepare($sql);
                    echo 'sup 3';                          
$stmt->bindParam(':title', $_POST['title'], PDO::PARAM_STR);       
$stmt->bindParam(':content', $_POST['content'], PDO::PARAM_STR); 
$stmt->bindParam(':category', 'City Secrets', PDO::PARAM_STR);
                               echo 'sup 4';       
$stmt->execute(); 
echo 'sup 5';
      header('location: http://www.backToThePageIPostedOn.com');

This is my current code but it is not entering to the DB:

$sql = "INSERT INTO blogData(
            title,
            content,
            category) 
            VALUES ( 
            :title, 
            :content, 
            :category)";

$stmt = $pdo->prepare($sql);

$stmt->bindParam(':title', $_POST['title'], PDO::PARAM_STR);       
$stmt->bindParam(':content', $_POST['content'], PDO::PARAM_STR); 
$stmt->bindParam(':category', 'City Secrets', PDO::PARAM_STR);

$stmt->execute(); 
 header('location: http://www.backToThePageIPostedOn.com');

Its stopping on the script page. This is my first time to use PDO so If someone could point out the error in my syntax I would appreciate it.

My code does not get past echo 'sup 2'; So I believe the error is in this line, $stmt = $pdo->prepare($sql);

I followed a tutorial to do this and I don't understand why they are adding the $pdo in. I was assuming thats supposed to be my connection but I have that set as $con When I change $pdo to $con I still get the same cut off at echo 'sup 2';

Anthony
  • 36,459
  • 25
  • 97
  • 163
wuno
  • 9,547
  • 19
  • 96
  • 180
  • This `$stmt->bindParam(':category', 'City Secrets', PDO::PARAM_STR);` should most likely need to be `$stmt->bindValue(':category', 'City Secrets', PDO::PARAM_STR);` – Funk Forty Niner Jun 06 '14 at 21:58
  • No sir that did not help. the error is on this line, $stmt = $pdo->prepare($sql); It must be the $pdo but I followed a tutorial and I am not sure what that is supposed to be – wuno Jun 06 '14 at 22:02
  • Then you'll need to show us what your PDO connection is, replacing credentials with `xxx` – Funk Forty Niner Jun 06 '14 at 22:05
  • Enable reporting of errors and/or check the error logs to provide **what error** is actually occurring. – hjpotter92 Jun 06 '14 at 22:06
  • I edited to show my connection – wuno Jun 06 '14 at 22:07
  • If you've `$stmt = prepare($sql);` then you're not calling `$pdo->prepare()`. – hjpotter92 Jun 06 '14 at 22:07
  • Add `$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` right after the connection is opened, see if it yields anything. – Funk Forty Niner Jun 06 '14 at 22:15
  • I found your error. You're doing `$con = new PDO` where it should be `$pdo = new PDO` or change `$stmt = $pdo->prepare($sql);` to `$stmt = $con->prepare($sql);` and you should be OK. – Funk Forty Niner Jun 06 '14 at 22:16
  • So then you'd do `$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` right after the connection is opened, should any other problems arise. – Funk Forty Niner Jun 06 '14 at 22:18
  • ya i was thinking maybe thats what the deal was. But it still stops at the same place.. GRR – wuno Jun 06 '14 at 22:19
  • 1
    AHHH ty its fixed. it was 3 issues. Thank you for your help! Answer so I can give you the cred. – wuno Jun 06 '14 at 22:21

2 Answers2

1

Statement bindParam method accepts second parameter by reference. Only variables can be passed by reference.

The solution is to assign to variables the params you are going to bind:

$stmt = $pdo->prepare($sql);

$title = $_POST['title'];
$content = $_POST['content'];
$category = 'City Secrets';

$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':content', $content, PDO::PARAM_STR);
$stmt->bindParam(':category', $category, PDO::PARAM_STR);

$stmt->execute();
  • The issue is at $stmt = $pdo->prepare($sql); You're right that needed to be changed but thats not what my question is about. – wuno Jun 06 '14 at 22:17
  • 1
    The correct answer is what Fred posted in the comments. – wuno Jun 06 '14 at 22:38
0

This is the correct working code for the question above.

$stmt->bindParam

changed to

 $stmt->bindValue 

And added the connection.php file for DB connection.

<?php                           
require_once( 'connection.php' );

$sql = "INSERT INTO blogData(
            title,
            content,
            category) 
            VALUES ( 
            :title, 
            :content, 
            :category)";

$stmt = $con->prepare($sql);

$stmt->bindParam(':title', $_POST['title'], PDO::PARAM_STR);       
$stmt->bindParam(':content', $_POST['content'], PDO::PARAM_STR); 
$stmt->bindValue(':category', 'City Secrets', PDO::PARAM_STR);

$stmt->execute(); 

 header('location: http://www.website.com');
?>           
wuno
  • 9,547
  • 19
  • 96
  • 180