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";
4

2 回答 2

0

只需添加一些调试语句即可找出问题所在。就像是 -

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

你需要阅读一些关于SQL 注入的知识,因为这个脚本很容易受到攻击。查看有关使用准备好的语句的这些页面 - PDO::preparemysqli::prepare

更新- 这是一个使用 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();
    }

}
于 2012-04-14T17:22:35.820 回答
0

'shipmentquantity' 表的主键是什么?看起来您正在尝试为主键输入两个值“3”,这就是它出错的地方。

DESCRIBE `shipmentquanitity`
于 2012-04-14T17:33:26.953 回答