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?

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀