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
  • 35,330
  • 24
  • 93
  • 158
wuno
  • 8,980
  • 17
  • 81
  • 173

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
  • 8,980
  • 17
  • 81
  • 173