2012-04-14 41 views
0

このコードは本当に私を混乱させました。
私はそれを実行した第1および第2の時間、それは完全に働いたが、その後、それはデータベースに2つのテーブルに挿入

を動作を停止し、私はそれを説明してみましょう:

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

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

これは私のコードです:

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

はちょうど間違っているものを見つけるためにいくつかのデバッグ文を追加します。以下のようなもの - あなたはこのスクリプトとしていくつかの読みについてSQL injectionを行う必要があり

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

は脆弱です。準備文の使用上のこれらのページをチェックアウト - PDO::preparemysqli::prepare

UPDATE - ここにあなたのデシベルと対話するためにPDOを使用した例である -

「shipmentquantity」のプライマリキーは何
<?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

+0

これは、プライマリキーに3を挿入しようとしていますが、その値はすでに存在しています。 – nnichols

+0

ありがとうございました。本当に素晴らしいです...!私はPDO – proG

0

表?主キーに「3」の2つの値を入力しようとしているように見えます。

DESCRIBE `shipmentquanitity` 
+0

についてのページをチェックして読んでいますが、それは私のエラーの原因です..ありがとう..私は主キーを変更します – proG

関連する問題