0

このコードは本当に私を混乱させました。
1回目と2回目は完全に機能しましたが、その後は機能しなくなりました。

説明させてください:

私は2つのテーブルで作業します。
最初に挿入するテーブルには、現在の日付、現在の時刻、およびセッションから取得したユーザーのIDが含まれます。私はそれがうまくいくと信じています。

私の問題は2番目の表にあります。私が得るエラーは、2番目の挿入後に「print」と入力したエラーです。

これは私のコードです:

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:: prepareおよびmysqli::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' の 2 つの値を入力しようとしているように見えますが、それがうまくいかないところです。

DESCRIBE `shipmentquanitity`
于 2012-04-14T17:33:26.953 に答える