这段代码真的让我感到困惑。
第一和第二次我跑了它,它的工作完美,但之后,它停止工作将数据库插入到两个表中
让我来解释一下:
我有2代表工作。
我给它插入的第一个表格是当前日期,当前时间和用户id,我从会话中获得的id。 我认为这很好。
我的问题是在第二个表中,我得到的错误是我在第二次插入后在“打印”中键入的错误。
这是我的代码:
session_start();
//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['con_id'])) {
header("location: login.html");
exit();
}
$DB_USER ='root';
$DB_PASSWORD='';
$DB_DATABASE='';
$con= mysql_connect($DB_HOST ,$DB_USER , $DB_PASSWORD);
if (!$con) {
die('Failed to connect to server :'.mysql_error());
}
$db=mysql_select_db($DB_DATABASE);
if (!$db) {
die("unable to select database");
}
//first table
$qry="insert into shipment values('',NOW(),CURTIME(),'".$_SESSION['con_id']."');";
$resultop=mysql_query($qry);
//to take the id frome last insert because i need it in the second insert
$SNo=mysql_insert_id();
if ($resultop) {
$options=$_POST['op'];//this is the name of the check boxe's
if (empty($options)) {
header("location: manage_itemsE.php");}
// this is the second table .. my reaaal problem
$qun=$_POST['Quantit'];
$size =count($options);
for ($i =0; $i<$size; $i++) {
$qqry="insert into shipmentquantity values('".$options[$i]."','".$SNo."','".$qun[$i]."');"; // $options is array of the id's which i took from the checkbox's in the html ... $qun is array of the values i took form html ... i sure this is right ;)
$resultqun=mysql_query($qqry);
}
if ($resultqun) {
header("location: shipment_order.php");
}
else print "error in the Quantity";
}
else print "error in the shipmet";
谢谢你...我做到了这一点,我得到了这个...重复'3'的关键'PRIMARY'这意味着数据库中的错误,对吧? – proG 2012-04-14 17:25:45
这意味着你正试图将3插入到主键中,但是该值已经存在。 – nnichols 2012-04-14 17:43:37
非常感谢你真的很棒......!我会检查并阅读有关PDO – proG 2012-04-14 18:22:03