donglin1192 2014-09-12 19:44
浏览 302
已采纳

插入数据错误代码23000 mysql pdo php

i have a table in my databse

i am trying to insert some data inside it

but i keep getting pdo error code 23000

i tried the query manually and it worked so the proplem isn't with the query

my code :

<?
include ('config.php');
$text=$_POST['text'];
if (!isset ($text)) exit();
$query=$db->prepare("INSERT INTO posts (post,userid,votes) VALUES (?,?,0)");
$query->bindValue(1,$text,PDO::PARAM_STR);
$query->bindValue(2,$id,PDO::PARAM_INT);
$re=$query->execute();
if ($re)
echo 'Added';
else die($query->errorcode()." ".$query->errorinfo());
?>

and config look like

<?
session_start();
try{
$db=new PDO ('mysql:host=localhost;dbname=test;charset=utf8','root','');
}
catch (PDOException $error) {
    echo $error->getmessage();
}
$id=$_SESSION['id'];
$username=$_SESSION['username'];
$password=$_SESSION['password'];
?>
  • 写回答

1条回答 默认 最新

  • dqknycyt92288 2014-09-12 19:58
    关注

    Did you check MySQL docs?

    http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html#error_er_dup_key

    Looks like you have a duplicate key in DB. My guess userid.

    If you have an auto-increment on userid no need to pass it on creation.

    Looking at your code it:

    1. Gets user from session
    2. Adds this user to DB

    So if you run script more than once it'll give 23000 error.

    Yes and how did userid got in to session?

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?