在此处遇到“ SQLSTATE [23000]:完整性约束违规:1062 Duplicate entry '0' for key 'id' ”的问题,
<? if (!defined("sKEY")) { exit("Houston, We've Got a Problem"); }
class PDOAct
{
public $db;
function __construct()
{
try {
$this->db = new PDO(DBdriver.':host='.DBhost.';dbname='.DBbase, DBuser, DBpass);
$this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch(PDOException $e) {
$this->err($e->getMessage());
}
}
//
//
function select($base, $row, $val, $type)
{
try {
if (isset($row[0]) && isset($val[0]) && isset($type[0]))
{
if (isset($base['select']) && $base['select'] != "*")
{
for ($i=0; $i<count($row); $i++)
{
if ($i < 1)
{
$q = "SELECT `".$base['select']."` FROM `".$base['table']."` WHERE `".$row[$i]."` = ':".$row[$i]."'";
} else {
$q = $q." AND `".$row[$i]."` = ':".$row[$i]."'";
}
}
} elseif (!isset($base[select]) || $base[select] == "*") {
for ($i=0; $i<count($row); $i++)
{
if ($i < 1)
{
$q = "SELECT * FROM `".$base['table']."` WHERE `".$row[$i]."` = ':".$row[$i]."'";
} else {
$q = $q." AND `".$row[$i]."` = ':".$row[$i]."'";
}
}
}
} elseif (!isset($row[0]) && !isset($val[0]) && !isset($type[0])) {
if ($base['select'] != "" && $base['select'] != "*")
{
$q = "SELECT `".$base['select']."` FROM `".$base['table']."`";
} elseif ($base['select'] == "" || $base['select'] == "*") {
$q = "SELECT * FROM `".$base['table']."`";
}
} else {
exit("Query error!");
}
$do = $this->db->prepare($q);
} catch(PDOException $e) {
$this->err($e->getMessage());
}
$arr = array('do' => $do, 'row' => $row, 'val' => $val, 'type' => $type);
return $arr;
}
//
//
function insert($base, $row, $val, $type)
{
try {
if (isset($row[0]) && isset($val[0]) && isset($type[0]))
{
for ($i=0; $i<=count($row); $i++)
{
if ($i < 1)
{
$q = "INSERT INTO `".$base['table']."` (`".$row[$i]."`";
} elseif ($i == count($row)) {
$q = $q.") VALUES (";
for ($j=0; $j<=count($row); $j++)
{
if ($j < 1)
{
$q = $q."':".$row[$j]."'";
} elseif ($j == count($row)) {
$q = $q.")";
} else {
$q = $q.", ':".$row[$j]."'";
}
}
} else {
$q = $q.", `".$row[$i]."`";
}
}
} else {
exit("Query error!");
}
$do = $this->db->prepare($q);
} catch(PDOException $e) {
$this->err($e->getMessage());
}
$arr = array('do' => $do, 'row' => $row, 'val' => $val, 'type' => $type);
return $arr;
}
//
//
function execute($arr)
{
$do = $arr['do'];
$row = $arr['row'];
$val = $arr['val'];
$type = $arr['type'];
try {
for ($i=0; $i<count($row); $i++)
{
$bindrow = ':'.$row[$i];
$bindtype = "PDO::PARAM_".strtoupper($type[$i]);
$do->bindValue($bindrow, $val[$i], $bindtype);
}
$do->execute();
} catch(PDOException $e) {
$this->err($e->getMessage());
}
}
//
//
function err($e)
{
file_put_contents('log'.DIR_SEP.'PDOerrors.txt', $e."\n", FILE_APPEND);
exit("Houston, We've Got a Problem");
}
}
?>
当我尝试像这样插入查询时:
$db = new PDOAct;
$base = array("table" => "users");
$row = array("id", "login", "pass", "level", "date", "name", "sex", "birth");
$val = array( 4, "Paul", 135246, 2, "2013-06-22", "Paulio", 1, "1996-06-25");
$type = array("int", "str", "int", "int", "str", "str", "int", "str");
$res = $db->execute($db->insert($base, $row, $val, $type));
print_r($res);
在表中,我有两个 id 为 0 和 1 的条目,所以它不能重复 id..
那么有什么问题呢?
谢谢!