SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast rkrathor's Avatar
    Join Date
    Feb 2014
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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.

    Code:
    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

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I'd do something like this:

    PHP Code:
    $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.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast rkrathor's Avatar
    Join Date
    Feb 2014
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by rkrathor View Post
    1- Will the ??? marks will not cause any error?
    No, because it's not an actual query, it's a prepared query. The ?? mark places where variables will be filled in.

    Quote Originally Posted by rkrathor View Post
    2- What this 'ssssssssssss' mean in the query?
    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.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Enthusiast rkrathor's Avatar
    Join Date
    Feb 2014
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    That's what I get for posting untested code I suppose

    This should work:

    PHP Code:
    <?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!";
    }
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Enthusiast rkrathor's Avatar
    Join Date
    Feb 2014
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Enthusiast rkrathor's Avatar
    Join Date
    Feb 2014
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •