How to show message only once in a loop?

hi,
I have a loop on my code, i know it will show message everytime that code will run but i want that the message only show on the final count.

for ($i = 0; $i < $post_count; $i++)
{
    $sql[] = "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 ('$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')";

}
foreach ($sql as $query)
{

 $data = mysqli_query($con, $query);
 if(!$data)
{
echo "<br> Error".mysqli_error($con);
}
else
{
echo "Inserted!"; /// this is the message coming everytime but i want this message only show when the loop complete or in last count.
}

}

thanks

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.

Thanks for your reply,
i have some query in your code,
1- Will the ??? marks will not cause any error?
2- What this ‘ssssssssssss’ mean in the query?

Thanks

No, because it’s not an actual query, it’s a prepared query. The ?? mark places where variables will be filled in.

That means that all the parameters following are strings (there are 12 parameters, and there are 12x s in that string).
Some of them may be numbers, but since they are coming from $_POST they are strings nonetheless because everything in $_POST is a string.

Hi,
Here are some error i am getting

1- Notice: Undefined variable: stmt .
2- Warning: mysqli_stmt::execute() expects exactly 0 parameters, 1 given
3- Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Couldn’t fetch mysqli_stmt.
4- Notice: Undefined variable: stmt
5- Warning: mysqli_stmt::close() [mysqli-stmt.close]: Couldn’t fetch mysqli_stmt
Pls have a look
thanks

That’s what I get for posting untested code I suppose :wink:

This should work:


<?php
$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();
    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!";
}

Hey Thanks ScallioXTX ,
Its working man, great job.
Thanks for helping.
Hey i become a fan of yours, can you give me some tips how to be a good programmer in php or any specific sites where i can do some practice to be a good one.
and where i can get latest update happening in php, what should use and what not etc etc.

Thanks

I lot of what I know I’ve learned right here at sitepoint, or from articles on sitepoint, or from articles I find on twitter.
It also always a good idea to read books about PHP, or about programming in general.
Plus, I have a university degree in Information Technology, that also doesn’t hurt :wink:

Thanks,
Actually i too have a master degree in computer, i switched from .net to php recently, here the main problem of debugging the php code and my main problem i do not like reading books.
that’s why, i post here in forums and get solution by doing these things practically. :slight_smile: