插入数据错误代码23000 mysql pdo php [英] inserting data error code 23000 mysql pdo php

查看:378
本文介绍了插入数据错误代码23000 mysql pdo php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个表

我正在尝试在其中插入一些数据

i am trying to insert some data inside it

但是我一直收到pdo错误代码23000

but i keep getting pdo error code 23000

我手动尝试了该查询,该查询成功了,所以问题不在查询中

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

我的代码:

<?
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());
?>

和配置看起来像

<?
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'];
?>

推荐答案

您是否检查过MySQL文档?

Did you check MySQL docs?

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

好像您在数据库中有一个重复的密钥.我猜是用户名.

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

如果userid具有自动递增功能,则无需在创建时通过它.

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

查看您的代码:

  1. 从会话中获取用户
  2. 将此用户添加到数据库

因此,如果您多次运行脚本,则会出现23000错误.

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

是的,userid是如何加入会话的?

Yes and how did userid got in to session?

这篇关于插入数据错误代码23000 mysql pdo php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆