2012-04-14 54 views
0

这段代码真的让我感到困惑。
第一和第二次我跑了它,它的工作完美,但之后,它停止工作将数据库插入到两个表中

让我来解释一下:

我有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"; 

回答

0

只需添加一些调试语句,找出什么错误。喜欢的东西 -

$resultqun = mysql_query($qqry) or print mysql_error(); 

你需要做一些阅读SQL injection因为这个脚本是脆弱的。使用PDO与你的数据库交互下面是一个例子 - -

<?php 
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=''; 

$db = new PDO("mysql:dbname=$DB_DATABASE;host=127.0.0.1", $DB_USER, $DB_PASSWORD); 

//first table 
$qry = "INSERT INTO shipment VALUES(NULL, CURRENT_DATE, CURRENT_TIME, ?)"; 
$stmt = $db->prepare($qry); 
$resultop = $stmt->execute(array($_SESSION['con_id'])); 

if(!$resultop){ 
    print $stmt->errorInfo(); 
} else { 

    $SNo = $db->lastInsertId(); 

    $options = $_POST['op'];//this is the name of the check boxe's 
    if (empty($options)) { 
     header("location: manage_itemsE.php"); 
     exit; 
    } 

    // this is the second table .. my reaaal problem 
    $qun = $_POST['Quantit']; 
    $size = count($options); 

    $stmt = $db->prepare("INSERT INTO shipmentquantity VALUES(?, ?, ?)"); 
    for($i = 0; $i < $size; $i++) { 
     $resultqun = $stmt->execute(array($options[$i], $SNo, $qun[$i])); 
    } 

    if($resultqun) { 
     header("location: shipment_order.php"); 
    } else { 
     print $stmt->errorInfo(); 
    } 

} 
+0

谢谢你...我做到了这一点,我得到了这个...重复'3'的关键'PRIMARY'这意味着数据库中的错误,对吧? – proG 2012-04-14 17:25:45

+0

这意味着你正试图将3插入到主键中,但是该值已经存在。 – nnichols 2012-04-14 17:43:37

+0

非常感谢你真的很棒......!我会检查并阅读有关PDO – proG 2012-04-14 18:22:03

0

什么是你的“shipmentquantity”主键结帐上使用预处理语句的这些网页 - PDO::preparemysqli::prepare

UPDATE表?它看起来像你正试图为主键输入两个'3'值,这就是它出错的地方。

DESCRIBE `shipmentquanitity` 
+0

yup这是我的错误的原因..谢谢你..我会改变主键 – proG 2012-04-14 17:42:13