这里我们去,当我尝试插入我的第一个查询运行和数据插入但我的第二个查询不工作。在同一时间评论第一个查询第二个工作良好。虽然两者都意味着我被困住了
if($order_no!="" && $cus_name!="")
{
//fs_code: set primary key into auto means use this qry before inserting
$query1 = "SET IDENTITY_INSERT order_creation ON";
$stmt1 = sqlsrv_query($conn, $query1);
$sql = "INSERT INTO order_creation (order_no,cus_name,cus_id,types,created_date,Status,id) VALUES('$order_no','$cus_name','$cus_id','$type',FORMAT(CURRENT_TIMESTAMP,'M/d/yyyy h:mm:sstt'),'$Status','$fmax')";
if(sqlsrv_query($conn,$sql))
{
echo 'success-1';
}
else
{
echo 'failure-1';
}
}
for($i=0;$i<count($goods_name);$i++)
{
if($goods_name[$i]!="")
{
$sql = "INSERT INTO order_Goods (order_no,goods_name,goods_qty,date,created_date,Balance_Qty,Supply_Qty,ProductPrice,id) VALUES('$order_no','$goods_name[$i]','$goods_qty[$i]',FORMAT(CURRENT_TIMESTAMP,'d/MM/yy h:mm:sstt'),'$created_date[$i]','$goods_qty[$i]','$Supply_Qty','$ProductPrice[$i]','$get_maxres')";
if(sqlsrv_query($conn,$sql))
{
echo 'success-2';
}
else
{
echo 'failure-2';
}
$get_maxres++;
}
}
sqlsrv_close($conn);
答案 0 :(得分:2)
您没有使用正确的语法将数组值替换为字符串。当数组索引包含变量时,您需要使用“复杂”语法,其中数组变量由{}
包围。
$sql = "INSERT INTO order_Goods (order_no,goods_name,goods_qty,date,created_date,Balance_Qty,Supply_Qty,ProductPrice,id)
VALUES('$order_no','{$goods_name[$i]}','{$goods_qty[$i]}',FORMAT(CURRENT_TIMESTAMP,'d/MM/yy h:mm:sstt'),'{$created_date[$i]}','{$goods_qty[$i]}','$Supply_Qty','{$ProductPrice[$i]}','$get_maxres')";
请参阅PHP Strings文档中的变量解析部分。
您还需要在第二个表上启用IDENTITY_INSERT
。把它放在for
循环之前:
sqlsrv_query($conn, "SET IDENTITY_INSERT order_creation OFF");
sqlsrv_query($conn, "SET IDENTITY_INSERT order_Goods ON");
答案 1 :(得分:0)
是的,我解决了这个
SET IDENTITY_INSERT Table1 ON
INSERT INTO Table1
/*Note the column list is REQUIRED here, not optional*/
(OperationID,
OpDescription,
FilterID)
VALUES (20,
'Hierachy Update',
1)
SET IDENTITY_INSERT Table1 OFF