在这里,当我尝试插入运行的第一个查询和插入的数据时,我的第二个查询不是Working.At,而是同时注释第一个查询,第二个很好。两者都意味着我被困住了
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);发布于 2016-07-09 10:06:36
您没有使用正确的语法将数组值替换为字符串。当数组索引包含变量时,需要使用“复合”语法,其中数组变量被{}包围。
$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文档中的变量解析一节。
您还需要在第二个表上启用IDENTITY_INSERT。将其放在for循环之前:
sqlsrv_query($conn, "SET IDENTITY_INSERT order_creation OFF");
sqlsrv_query($conn, "SET IDENTITY_INSERT order_Goods ON");发布于 2016-07-09 10:42:34
是的,我把这个修好了
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 https://stackoverflow.com/questions/38280566
复制相似问题