У меня есть ajax php файл с двумя основными запросами INSERT ... сначала мы вставляем порядок, а затем вставляем разные строки в этом порядке. Я могу правильно вставить порядок, но не могу вставить строки в той же базе данных. Таким образом, один запрос работает, а второй нет. Сервер работает в экземпляре EC2 в AWS
Перепробовал все, что я знаю ... даже дважды проверяя параметры sql, чтобы убедиться, что что-то в MSSQL препятствует правильному хранению строк.
<?php
session_start();
if(!isset($_SESSION['idCompany'])){
$login=false;
echo ' <meta HTTP-EQUIV="REFRESH" content="0; url=http://xxx.xxx.xxx.xxx/">';
return;
}
if(!ini_get('date.timezone') )
{
date_default_timezone_set('Europe/Berlin');
}
$idItem=$_POST['articulo'];
$quantity=1;
$uid = "user";
$pwd = "password";
$serverName = "localhost";
// Array DB-Conn
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>"CompanyApp");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Error.</br>";
die( print_r( sqlsrv_errors(), true));
}
// QUERY TO SHOW ACTUAL USER
$sql = "select * from OrdersLCompany where control='3' and idCompany='".$_SESSION['idCompany']."' and serie='D'";
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$query = sqlsrv_query( $conn, utf8_decode($sql), $params, $options);
//CHECK IF USER HAS ANY ORDERS
$row_count = sqlsrv_num_rows($query);
$order = sqlsrv_fetch_array($query);
function mssql_addslashes($data) {
$data = str_replace("'", "''", $data);
return $data;
}
if($row_count==0)
{
//CREATE ORDER
$sql = "select isnull(max(number),0)+1 from OrdersLCompany where idCompany='".$_SESSION['idCompany']."'";
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$query = sqlsrv_query( $conn, utf8_decode($sql), $params, $options);
$row = sqlsrv_fetch_array($query);
$number=$row[0];
//INSERT ORDER
$sql_insert="INSERT INTO OrdersLCompany (serie, number, idCompany, fecha, control)
VALUES ('D','".$number."','".$_SESSION['idCompany']."','".date('Ymd H:i:s')."',3)";
$query_ins = sqlsrv_query( $conn, utf8_decode($sql_insert), $params, $options);
if( $query_ins === false )
{
echo "Error al insertar un order nuevo.</br>";
die( print_r( sqlsrv_errors(), true));
}else{
//ORDER JUST CREATED SO LINE IS GOING TO BE 1
$sql_price_stock = "select priceLO, CASE WHEN stock>0 THEN '1' ELSE '0' END, Abreviación from Items_tarifa_stock
left join Items on Items.idItem=Items_Tarifa_Stock.idItem
right join Items_CompanyApp on Items_CompanyApp.idItem=Items.idItem
where Items_tarifa_stock.idItem='".$idItem."'";
$query_line = sqlsrv_query( $conn, utf8_decode($sql_price_stock), $params, $options);
$price_stock = sqlsrv_fetch_array($query_line);
$price=$price_stock[0];
$stock=$price_stock[1];
$abrev=mssql_addslashes($price_stock[2]);
if($price=="null" or $price<="0"){
echo "5";
return;
}
if($stock=="1")
{
$sql_insert_line="INSERT INTO OrderslCompany_lines (serie, number, line, order, idCompany, idItem, description, quantity, cost, total, stock)
VALUES ('D','".$number."','1','1','".$_SESSION['idCompany']."','".$idItem."','".$abrev."','1','".$price."','".$price."','1')";
$query_ins_line = sqlsrv_query( $conn, utf8_decode($sql_insert_line), $params, $options);
if( $query_ins_line === false )
{
echo "Error inserting line.</br>";
die( print_r( sqlsrv_errors(), true));
}
}else{
echo "No stock";
}
}
// Close conn.
sqlsrv_free_stmt( $query);
sqlsrv_close( $conn);
return;
}else{
//Check order and line...
$sql = "select number from OrdersLCompany where control='3' and idCompany='".$_SESSION['idCompany']."' and serie='D'";
$query = sqlsrv_query( $conn, utf8_decode($sql), $params, $options);
$order = sqlsrv_fetch_array($query);
$numberl=$order[0];
//Aquí saber la última line que hay que meter.
$sql = "select isnull(max(line),0)+1 from OrderslCompany_lines where number='".$numberl."' and idCompany='".$_SESSION['idCompany']."' and serie='D'";
$query = sqlsrv_query( $conn, utf8_decode($sql), $params, $options);
$orderl = sqlsrv_fetch_array($query);
$line=$orderl[0];
$sql_price_stock = "select priceLO, CASE WHEN stock>0 THEN '1' ELSE '0' END, Abreviación from Items_tarifa_stock
left join Items on Items.idItem=Items_Tarifa_Stock.idItem
right join Items_CompanyApp on Items_CompanyApp.idItem=Items.idItem
where Items_tarifa_stock.idItem='".$idItem."'";
$query_line = sqlsrv_query( $conn, utf8_decode($sql_price_stock), $params, $options);
$price_stock = sqlsrv_fetch_array($query_line);
$price=$price_stock[0];
$stock=$price_stock[1];
$abrev=mssql_addslashes($price_stock[2]);
if($price=="null" or $price<="0"){
echo "5";
return;
}
if($stock=="1")
{
//if item exists in the order we add up.
$sql_consulta_productos_order="Select quantity FROM OrderslCompany_lines where number='".$numberl."' and idCompany='".$_SESSION['idCompany']."' and serie='D' and cost<>0 and idItem='".$idItem."'";
$query = sqlsrv_query( $conn, utf8_decode($sql_consulta_productos_order));
$exists = sqlsrv_fetch_array($query);
$exists=$exists[0];
//If item exists we update same line
if($exists>=1){
if($exists>=150)
{
echo "NOMORE";
return;
}
//Add 1 unit to existing items. Increment quantity, price y total.
$sql_actualizar_line="UPDATE OrderslCompany_lines SET quantity=quantity+1, total=convert(float,cost)*(quantity+1) WHERE number='".$numberl."' and idCompany='".$_SESSION['idCompany']."' and serie='D' and cost<>0 and idItem='".$idItem."'";
$stmt = sqlsrv_query( $conn, utf8_decode($sql_actualizar_line));
if( $stmt === false )
{
echo "Error.</br>";
die( print_r( sqlsrv_errors(), true));
}
}else{
if($quantity>=150)
{
echo "NOMORE";
}
$sql_insert_line="INSERT INTO OrderslCompany_lines (serie, number, line, order, idCompany, idItem, description, quantity, cost, total, stock)
VALUES ('D','".$numberl."','".$line."','".$line."','".$_SESSION['idCompany']."','".$idItem."','".$abrev."','1','".$price."','".$price."','1')";
$query_ins_line = sqlsrv_query( $conn, utf8_decode($sql_insert_line), $params, $options);
if( $query_ins_line === false )
{
echo "Error inserting line within the order.</br>";
die( print_r( sqlsrv_errors(), true));
}
}
}else{
echo "No stock";
}
if( $query === false )
{
echo "Error.</br>";
die( print_r( sqlsrv_errors(), true));
}
$marcadores ="";
// show result
while($row = sqlsrv_fetch_array($query))
{
echo "-".$row[0];
}
// Close conn.
sqlsrv_free_stmt( $query);
sqlsrv_close( $conn);
}
?>