douju4278 2017-08-14 20:13
浏览 68
已采纳

INSERT私人消息系统PDO php

I am trying to build a little private message system in PHP, i am using PDO as connection to the database. i have got the most to work excpet the most important part.

I cannot get the insert message to write anything to the database so i would love some help.

I will first post my database so you all can see what we are working with here. starting with my users

id int(10) UN AI PK 
username varchar(30) 
password varchar(100) 
regdate datetime

This is my message table which im trying to write the message to

id int(11) AI PK 
from_user varchar(45) 
to_user varchar(45) 
subject varchar(400) 
message text 
date date 
read tinyint(4) 

I have tried a couple of diffrent code to get it to work i will now paste the code that according to my error check says that the message was succesfully sent...

$sql = "
INSERT INTO private_messages VALUES('',$user, 
$to_user','$subject',$message','$date','0')
";

    echo "Youre message was succesfully sent...";

But this does not work and i know that this is probably not the right way when using PDO.

I have also tried this

$query =$dbh->prepare("INSERT INTO private_messages(user, to_user, subject, 
message,
date);
VALUES('',$user, $to_user','$subject',$message','$date','0')
");

$query->bindParam(":user", $user);
$query->bindParam(":to_user", $to_user);
$query->bindParam(":subject", $subject);
$query->bindParam(":message", $message);

if ($query->execute()) {
    $success = true;
}

but then i get Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064

I will also post the form im using if that can be of any value

echo "
<form action='compose.inc.php' method='POST'>
<table>
<tr>
<td>To: </td>
<td><input type='text' name='to_user'/></td>
</tr>
<tr>
<td>Subject: </td>
<td><input type='text' name='subject'/></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name='message' rows='10' cols='50'></textarea></td>
</tr>
<tr>
<td></td>
<td colspan='2'><input type='submit' name='submit' value='Send Message'/>
</td>
</r>
</table>
</form>

  ";

When doing this message system ive been following this https://www.youtube.com/watch?v=BED_yMC7qv0 , its using mysqli and i have been trying to decode it to PDO, i think the problem lies therein

So short recap My error check says that my message are sent. Nothing gets written to my database. im trying to use PDO.

If a i cant find a solution i would love to get pointers to tutorial or books or anything that are about message system with PDO. Best regards /Robert

  • 写回答

1条回答 默认 最新

  • doudiaozhi6658 2017-08-14 20:23
    关注

    Instead of inserting the variables into the query you do this by binding them.

    $query =$dbh->prepare("INSERT INTO private_messages (user,to_user,subject,message,date)
    VALUES(:user,:to_user,:subject,:message,NOW())");
    
    $query->bindParam(":user", $user);
    $query->bindParam(":to_user", $to_user);
    $query->bindParam(":subject", $subject);
    $query->bindParam(":message", $message);
    
    if ($query->execute()) {
        $success = true;
    }
    

    Also you should match the amount of values to the amount of columns.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测