简体   繁体   中英

PDO INSERT INTO - not inserting data into table?

I am struggling with an INSERT query using PDO.

The exact same code works using msql_query function, but I am struggling when trying to convert to PDO.

Can anyone advise why this code isnt inserting anything into the table?

$weaponinvite = $_POST['weaponbtn'];    

if ($weaponinvite){

    $weaponinviteperson = $_POST['weaponinvitename'];

    if(!$weaponinviteperson){
        echo "You must enter a playername";
    }else{
        $invitequery = "SELECT count(*) FROM `users` WHERE username=:ocinvited";
        $checkinvite = $db->prepare($invitequery);
        $checkinvite->execute(array(':ocinvited'=>$weaponinviteperson));
        $checkrows =  $checkinvite->fetchColumn(); 

          if ($checkrows == 0){                   

          echo "No such user. Please check and try again";

          }else{
             $ocpositioninv = "Weapon Master";
             $message = "Congratulations, You have been invited to join an organised crime in $oclocation as a $ocpositioninv . Click here to accept";           
             $ocinvitestatement ="INSERT INTO `inbox`(`id`,`to`,`from`,`message`,`date`,`read`,`saved`,`event_id`,`subject`) VALUES ('', ':ocinvited', ':ocinviter', ':message', ':date', '0', '0', '0', 'Organised Crime Invitation - :ocpositioninvsub')";
             $wpinvstate = $db->prepare($ocinvitestatement); 
             $wpinvstate->execute(array(':ocinvited'=>$weaponinviteperson,
                                        ':ocinviter'=>$username,
                                        ':message'=>$message,
                                        ':date'=>$date,
                                        ':ocpositioninvsub'=>$ocpositioninv));



             echo" invitation sent";

          }
    }
}

It all works Ok, giving all the correct echos at the correct stages, even at the last ELSE, it echos: Invitation Sent, but it just wont insert the data into the Table :(

Ive looked through a few posts on here / the internet, and it appears my code is how it is meant to be?

Can anybody help please?

Replace this code:

$ocinvitestatement ="INSERT INTO `inbox`(`id`,`to`,`from`,`message`,`date`,`read`,`saved`,`event_id`,`subject`) VALUES (NULL, :ocinvited, :ocinviter, :message, :date, '0', '0', '0',:ocpositioninvsub)";

And:

   $wpinvstate->execute(array(':ocinvited'=>$weaponinviteperson,
                              ':ocinviter'=>$username,
                              ':message'=>$message,
                              ':date'=>$date,
                              ':ocpositioninvsub'=>'Organised Crime Invitation '.$ocpositioninv));

Look, you have two queries.

In the first query you are using placeholders all right.

But in the second one - all of sudden - you did it wrong. How it can be?

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