I have to write a php script with mysql queries to insert and update some tables in database. I should use transactions to make sure all the queries are run. The problem is I have to used first query data as the input values of the second query inside a while loop. I write a sample script, but it doesn't work. No data insert or update. Please help..!
Here's the php script.
$processes = array();
$select_qty = "SELECT * FROM stock_master WHERE itemid='$itemId'";
$result_qty = mysql_query($select_qty);
while($row = mysql_fetch_assoc($result_qty)){
$id = $row['id'];
$stokqty = $row['availableqty'];
$processes[] = "UPDATE stock_master SET availableqty = '$stokqty' WHERE itemid = '$id'";
$processes[] = "INSERT INTO nista_bill_master (billno, billdate, icode, iname, warehouse, quantity) VALUES ('$id', 'xx', 'xx', 'xx','xx', '$stokqty')";
}
mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");
$count_processes = count($processes);
for($i=0; $i < $count_processes; $i++){
try {
$q = mysql_query($processes[$i]);
if (!$q) throw new Exception(mysql_error());
}
catch (Exception $e) {
mysql_query("ROLLBACK");
break;
}
}
mysql_query("COMMIT");
mysql_query("SET AUTOCOMMIT=1");