Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数据库插入到两个表中_Php_Mysql - Fatal编程技术网

Php 将数据库插入到两个表中

Php 将数据库插入到两个表中,php,mysql,Php,Mysql,这段代码真让我困惑。 我第一次和第二次运行它时,它工作得很好,但之后就停止工作了 让我解释一下: 我使用两张桌子。 我向其中插入的第一个表是当前日期、当前时间和从会话中获取的用户id。 我相信这很管用 我的问题在第二个表中,我得到的错误是我在第二次插入后在“打印”中键入的错误 这是我的代码: session_start(); //Check whether the session variable SESS_MEMBER_ID is present or not if(!isset($_SES

这段代码真让我困惑。
我第一次和第二次运行它时,它工作得很好,但之后就停止工作了

让我解释一下:

我使用两张桌子。
我向其中插入的第一个表是当前日期、当前时间和从会话中获取的用户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";
session_start();
//检查会话变量SESS_MEMBER_ID是否存在
如果(!isset($\u会话['con\u id'])){
标题(“location:login.html”);
退出();
}
$DB_USER='root';
$DB_密码=“”;
$DB_数据库=“”;
$con=mysql\u connect($DB\u主机,$DB\u用户,$DB\u密码);
如果(!$con){
die('未能连接到服务器:'.mysql_error());
}
$db=mysql\u select\u db($db\u DATABASE);
如果(!$db){
die(“无法选择数据库”);
}
//第一桌
$qry=“插入装运值(“”,NOW(),CURTIME(),“$\u会话['con\u id']。”);”;
$resultop=mysql\u查询($qry);
//从上次插入中获取id,因为我在第二次插入中需要它
$SNo=mysql_insert_id();
如果($TOP){
$options=$\u POST['op'];//这是复选框的名称
如果(空($选项)){
标题(“位置:manage_itemsE.php”);}
//这是第二张桌子我真正的问题
$qun=$_POST['Quantit'];
$size=计数($options);

对于($i=0;$i),只需添加一些调试语句即可找出问题所在

$resultqun = mysql_query($qqry) or print mysql_error();
您需要阅读一些关于此脚本的内容,因为此脚本易受攻击

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

}

“shipmentquantity”表的主键是什么?看起来您试图为主键输入两个“3”值,这就是问题所在

DESCRIBE `shipmentquanitity`

谢谢你…我这样做了,我得到了这个…键'PRIMARY'的重复条目'3',这意味着数据库中的错误,对吗?这意味着你试图将3插入到主键中,但该值已经存在。非常感谢,这真的很好…!我将检查并阅读有关PDYUP的页面,这是我错误的原因。.谢谢。。我将更改主键