带插入的PDO语句

时间:2015-11-28 05:12:32

标签: php pdo insert

我收到错误说:

  

[星期六11月28日08:07:29.066118 2015] [:错误] PHP 2. PDOStatement->执行()/home/www/Files/direct_download.php:213,referer:Listen-1.html

$db_host = 'localhost';
$db_user = 'dbuser';
$db_passwd = 'passwd';
$db_name = 'dbname';
$db_charset = 'utf8';

    $connexion = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_passwd
        , array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES '.$db_charset.'')); //SET NAMES utf8
$connexion->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);

// Insert into downloads table to track downloads
try {               
$addDownload = $connexion->prepare("INSERT INTO `downlads` (`idd`, 
    `rub`, 
    `srub`, 
    `cat`, 
    `scat`, 
    `menu`, 
    `type`, 
    `action`, 
    `num`, 
    `randkey`, 
    `person_ip`, 
    `when`) 
    VALUES (NULL, :rub, :srub, :cat, :scat, :menu, :type, :action, :num, :rand, :ip, :when)");

 $addDownload->execute(array(
            $rub, $srub, $cat, $scat, $menu, $type, $action, $num, $randKeys, $ip_visiteur, $DateTime));                
} catch(PDOException $e) {
    echo $e->getMessage();
}               

但我无法弄清楚错误在哪里

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试在null语句中定义execute。或试试这个

$addDownload = $connexion->prepare("INSERT INTO `downlads` (`idd`, 
`rub`, 
`srub`, 
`cat`, 
`scat`, 
`menu`, 
`type`, 
`action`, 
`num`, 
`randkey`, 
`person_ip`, 
`when`) 
VALUES (:null, :rub, :srub, :cat, :scat, :menu, :type, :action, :num, :rand, :ip, :when)");

并尝试像这样使用它

 $addDownload->execute(array(
        ':null'=>NULL,':rub'=>$rub, ':srub'=>$srub, ':cat'=>$cat, ':scat'=>$scat, ':menu'=>$menu, ':type'=>$type, ':action'=>$action, ':num'=>$num, ':rand'=>$randKeys, ':ip'=>$ip_visiteur, ':when'=>$DateTime));

答案 1 :(得分:1)

完成PDO statement

您的代码将如下: -

try {      
$addDownload = $connexion->prepare("INSERT INTO `downlads` ( 
    `rub`, 
    `srub`, 
    `scat`) 
    VALUES (:rub, :srub, :scat)");
            $addDownload->bindParam(':rub', $rub, PDO::PARAM_STR, 100);
            $addDownload->bindParam(':srub', $srub, PDO::PARAM_STR, 100);
            $addDownload->bindParam(':scat', $cat, PDO::PARAM_STR, 100);

     $addDownload->execute();                

} catch(PDOException $e) {
    echo $e->getMessage();
}