I’d do something like this:
$query = $con->prepare(
"INSERT INTO pro_customer_album_price(customer_id, customer_name, customer_user_name, sl_no, album_name, paper, finish, price, vat, total_amt, date, time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$all = true;
for ($i = 0; $i < $post_count; $i++)
{
$query->bind_param('ssssssssssss', $customer_id, $customer_name, $customer_user_name, $sl_no[$i], $album_name[$i], $paper_used[$i], $finishing[$i], $price[$i], $vat[$i], $total_amt[$i], $date, $time);
$res = $query->execute($stmt);
if (false === $res)
{
echo "Query error: ".$con->error; // do NOT show this in production! Log it instead
$all = false;
}
$query->close();
}
if ($all)
{
echo "Inserted!";
}
Note that I’ve replaced your query with a prepared query. I did this because they are a lot safer than what you were doing. See SQL Injection.
The principle is that you write a query and then specify where parameters need to go by using question marks. Then you can bind parameters (via bind_param) to assign values to those question marks. So the first parameter will be “replaced” by the first parameter, i.e., $customer_id, the second one by $customer_name, etc. This is done at the database level; mysqli sends the query and the parameters separately to the database, so the database can never confuse parameters and the query, thus eliminating SQL Injection.
As for your original question, I’ve used $all to keep track of if everything is going alright. At the start it will assume everything will go fine, but as soon as any of the queries fails $all will be set to false, and the final check if ($all)
will not fire and the text “Inserted!” will not be printed. If everything goes fine the text will be printed.