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 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?