Jump to content

php pdo execute array quotations


MDanz

Recommended Posts

The below prepared statement doesn't insert into the database.

 

 

  $sid =1;
    $sid2 = $GET['sid2']; //empty
    $position = 0;
    $name = "John";

$new = $connectdb->prepare("INSERT INTO `table1` VALUES ('',:sid,:sid2,:position,:name)");
				$new->execute(array(':sid'=>$sid,':sid2'=>$sid2,':position'=>$position,':name'=>$name));

 

 

When i add quotations to execute array values, then the insert works. 

 

 

 $new->execute(array(':sid'=>"$sid",':sid2'=>"$sid2",':position'=>"$position",':name'=>"$name"));

 

What i want to know is by adding quotations does this affect PDO's sanitization? 

Link to comment
Share on other sites

You shouldn't have to add quotes...

 

<?php

try {

$pdo = new PDO('mysql:host=localhost;dbname=db','root','');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$val1 = 'foo';
$val2 = 'bar';

$q = 'INSERT INTO test (column1, column2)
	VALUES (:param1, :param2)';

$stmt = $pdo->prepare($q);
$stmt->execute( array(':param1'=>$val1,':param2'=>$val2) );

} catch( PDOException $e ) {
echo 'Error: '.$e->getMessage().'<br>'.$pdo->errorInfo();
}
?>

 

Works fine for me.

Link to comment
Share on other sites

PDO treats null values in PHP as NULL values in MySQL. Apparently the third column in that table doesn't allow for NULLs. (Or maybe there's a uniqueness constraint.)

 

And yes, null. Because unless you defined a $GET array (coughunderscore) then $sid2 is null.

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.