开发者

Why do I end up with my param names instead of their values in this PDO insert statement?

开发者 https://www.devze.com 2023-03-15 22:13 出处:网络
This is actually my first project where I use PDO. It worked very well so far but in this particular case I end up with the following content in my database:

This is actually my first project where I use PDO.

It worked very well so far but in this particular case I end up with the following content in my database:

Why do I end up with my param names instead of their values in this PDO insert statement?

The unmodified code I use is this:

<?php
$dbh=new PDO('mysql:host=localhost;dbname=domain-me;port=3306','domain-me','****',array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8"));
$sql="
INSERT INTO  payments_paypal (
tid ,
txn_id ,
item_number ,
item_name ,
mc_currency ,
mc_gross ,
payment_date ,
payment_status ,
custom ,
payer_email ,
raw_data
)
VALUES (
NULL ,  ':txn_id',  ':item_number',  ':item_name',  ':mc_currency',  ':mc_gross',  ':payment_date',  ':payment_status',  ':custom',  ':payer_email',  ':raw_data'
);
";
$sth=$dbh->prepare($sql);
$do=$sth->execute(array(':txn_id'=>@$_POST["txn_id"],':item_number'=>$_POST["item_number"],':item_name'=>$_POST["item_name"],':mc_currency'=>$_POST["mc_currency"],':mc_gross'=>$_POST["mc_gross"],':payment_date'=>$_POST["payment_date"],':payment_status'=>$_POST["payment_status"],':custom'=>$_POST["custom"],':payer_email'=>$_POST["payer_email"],':raw_data'=>$_POST["raw_data"],));
?>

edit:

I now did it using the old mysql_function and it works now. However I have this query that works just fine:

$sql = "INSERT INTO users ( puid,refcode,extuid, login,login_proxy, pass, email) 
VALUES (:puid,:refcode,:extuid,:login,:login_proxy,:pass,:email);";
        $sth = $dbh->prepare($sql);

        $do = $sth->execute(
                array(
                ':puid' => $refuser,
                ':refcode' => crc32(uniqid('')),
                ':extuid' => md5(uniqid('')),
                ':login' => $_POST['login'],
                ':login_proxy' => $_POST['login'],
                ':pass' => sha1($_POST['pass']),
                ':email' => $_POST['email'] ,
  开发者_运维百科              )
        );


I believe when doing an INSERT or UPDATE you need to specify the names in the insert values array without the colons. Try this:

<?php
$dbh=new PDO('mysql:host=localhost;dbname=domain-me;port=3306','domain-me','****',array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8"));
$sql="
INSERT INTO  payments_paypal (
tid ,
txn_id ,
item_number ,
item_name ,
mc_currency ,
mc_gross ,
payment_date ,
payment_status ,
custom ,
payer_email ,
raw_data
)
VALUES (
NULL ,  ':txn_id',  ':item_number',  ':item_name',  ':mc_currency',  ':mc_gross',  ':payment_date',  ':payment_status',  ':custom',  ':payer_email',  ':raw_data'
);
";
$sth=$dbh->prepare($sql);
$do=$sth->execute(array('txn_id'=>@$_POST["txn_id"],'item_number'=>$_POST["item_number"],'item_name'=>$_POST["item_name"],'mc_currency'=>$_POST["mc_currency"],'mc_gross'=>$_POST["mc_gross"],'payment_date'=>$_POST["payment_date"],'payment_status'=>$_POST["payment_status"],'custom'=>$_POST["custom"],'payer_email'=>$_POST["payer_email"],'raw_data'=>$_POST["raw_data"],));
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号