what is the correct way to do a sql server Update statement with PHP variables


#1

I am getting Order number, Order Status, and assignment number from a file that I download and I need to take this information and update a sql server table but I really don’t know how to do that. In my code you will see that I echo the statements out then copy those run them in sql query manager. I can connect to my sql server and run a select statement.

[php]$connect = odbc_connect(“removed”);
if (!$connect) {
exit("Connection Failed: " . $connect);
}
$approve_statues=array(“AA”,“AC”,“AD”,“AX”);
$decline_ststus=array(“DA”,“DR”,“HC”,“CI”,“CR”,“CZ”);

$local_file=‘CCDATA1.TXT’;
sleep(2);
$fp = fopen($local_file, ‘r’);
while (!feof($fp))
{
$line = fgets($fp);

$order = substr($line, 69, 5);
$status=substr($line, 117, 2);
$assignment=substr($line, 91, 10);

$order=ltrim(rtrim($order));

if (in_array($status,$approve_statues)){

$file_array= array($order=> array($assignment,$status));
//echo json_encode($file_array);

echo “

update salesorders set uompcreditstatus=‘CLEAR’ uompschedulecolor=’$status$assignment’ where ompsalesorderid =’$order’
“;
// $sql_update=””;
// $update = odbc_exec($connect, $sql_update);
}
elseif (in_array($status,$decline_ststus)){
echo “update salesorders set status=$status, assignment=$assignment where ompsalesorderid =’$order’ on Hold
“;
// $sql_update=””;
// $update = odbc_exec($connect, $sql_update);
}

}

fclose($fp);
odbc_close($connect);[/php]


#2

The way you are doing it except in a cmd.

I would recommend you use PDO either way.


#3

I am using odbc only because that is the way the code came to me. Would this be correct?

[php] if (in_array($status,$approve_statues)){

$file_array= array($order=> array($assignment,$status));

$clear=“update salesorders set uompcreditstatus=‘CLEAR’, uompschedulecolor=’$status$assignment’ where ompsalesorderid =’$order’ and ompOrderDate > ‘10-9-2017’
”;
$result_clear = odbc_exec($connect, $$clear);

}
elseif (in_array($status,$decline_ststus)){
$hold="update salesorders set uompcreditstatus=‘HOLD’, uompschedulecolor=’$status$assignment’ where ompsalesorderid =’$order’ and ompOrderDate > ‘10-9-2017’
";

	$result_hold = odbc_exec($connect, $hold);
  }
	
	echo $result_clear;
	echo $result_hold;
	echo"<br>";

}[/php]