简体   繁体   中英

PDO statement with insert

I'm getting an error saying:

[Sat Nov 28 08:07:29.066118 2015] [:error] PHP 2. PDOStatement->execute() /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();
}               

But I could not figure out where is the mistake

在此处输入图片说明

Try defining null in execute statement. Or try this

$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)");

And also try using it like this

 $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));

Going through PDO statement

Your code will be like :-

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM