简体   繁体   English

PDO INSERT INTO-不将数据插入表中吗?

[英]PDO INSERT INTO - not inserting data into table?

I am struggling with an INSERT query using PDO. 我正在努力使用PDO进行INSERT查询。

The exact same code works using msql_query function, but I am struggling when trying to convert to PDO. 使用msql_query函数可以使用完全相同的代码,但是在尝试转换为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 :( 一切正常,即使在最后一个ELSE,也可以在正确的阶段提供所有正确的回显,但它会回显:邀请已发送,但不会将数据插入表中:(

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? 怎么可能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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