SitePoint Sponsor

User Tag List

Results 1 to 20 of 20

Thread: foreach?

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    foreach?

    I'm a little confused as to how foreach works...
    I have a form where I select Images to delete via radio buttons (each image has an ID in the Photo database. So I want the foreach to loop through each radio button and delete any selected...
    Heres the PHP
    PHP Code:
    foreach ($_POST['ID'] as $ID) {
    $sql ="DELETE FROM Photo WHERE ID = '$ID'";

    echo 
    $sql;
    mysql_query($sql) or die(mysql_error());

    Whats wrong with my logic?
    Thanks...
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you post a mock-up of your HTML form?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ya, here are 2 screenshots..
    The first is the form (in an email)
    Notice how im trying to make it possible to delete each image..
    The 2nd is the source of the two images to show you each radio button has an ID (for the Photo table)
    Attached Images Attached Images
    "Oh, and Jenkins--apparently your mother died this morning."

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What does echo $sql; give you each time? Is that code doing anything at all?

    Keep in mind the HTML radios (sounds like they should be checkboxes) need to have something like
    name="ID[]"
    for it to give you an array to loop through.
    - Robert

  5. #5
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    heres the code
    PHP Code:
    <?php
    if (isset($_POST['delete_photos']))
    {
    foreach (
    $_POST['ID'] as $ID) {
    $sql ="DELETE FROM Photo WHERE ID = '$ID'";

    echo 
    $sql;
    mysql_query($sql) or die(mysql_error());
    }
    echo 
    "<h1>Updated, photo deleted.</h1>";
    } else {
    echo 
    "<h1>Not deleted.<h1>";


    ?>
    And heres the result (screenshot)
    Attached Images Attached Images
    "Oh, and Jenkins--apparently your mother died this morning."

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We'd really need to see the code of your HTML form. It should look something like this:
    PHP Code:
    <?php
    if (isset($_POST['action'])) {
        foreach (
    $_POST['ID'] as $ID) {
            
    $sql ="DELETE FROM Photo WHERE ID = '$ID'";
            echo 
    "<pre>$sql</pre>";
            
    //do your MySQL stuff here
        
    }
    }

    ?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
             <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Form Test</title>
        </head>
        <body>
            <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
                <table cellspacing="0">
                    <caption>Items</caption>
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Delete?</th>
                        </tr>
                    </thead>
                    <tbody>
    <?php
    //populate the form
    for ($i=0$i<10$i++) { 
        echo <<<EOM
                        <tr>
                            <td>Item 
    $i</td>
                            <td><input type="checkbox" name="ID[]" value="
    $i" /></td>
                        </tr>
    EOM;
    }
    ?>
                    </tbody>
                </table>
                <p><input type="submit" name="action" value="Show Values"/></p>
            </form>
        </body>
    </html>
    Though keep in mind your mysql statement is very insecure and susceptible to an injection attack.

  7. #7
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thx, how do I secure it?
    "Oh, and Jenkins--apparently your mother died this morning."

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach ($_POST['ID'] as $ID) {
    $sql ="DELETE FROM Photo WHERE ID = '$ID'";

    echo 
    $sql;
    mysql_query($sql) or die(mysql_error());

    is inefficient because it queries once for EACH item. A single query could accomplish this.

    As your input should be an integer (I'm assuming, being an ID), cast it to an integer to stop attacks.

    The following is more efficient and is much more secure:
    PHP Code:
    $IDs = array();
    foreach (
    $_POST['ID'] as $ID) {
        
    $IDs[] = (int)$ID;
    }
    if(
    count($IDs) > 0){
        
    $sql 'DELETE FROM Photo WHERE ID IN (' implode(', '$IDs) . ')';
        
    mysql_query($sql) or die(mysql_error());

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I selected an image (with an ID of 3) and tried to delete iy, but I got an error (screenshot)
    Heres my html of that php page
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org\tR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>HoustonASP: Delete providers photos</title>
    <?php include("db_conn_open.php"); ?>
    </head>
    <BODY>
    <?php
    print('<pre>');
    print_r($_POST);
    print(
    '</pre>');


    if (isset(
    $_POST['delete_photos']))
    {
    $IDs = array();
    foreach (
    $_POST['ID'] as $ID) {
        
    $IDs[] = (int)$ID;
        }
        if(
    count($IDs) > 0){
        
    $sql 'DELETE FROM Photo WHERE ID IN (' implode(', '$IDs) . ')';
        
    mysql_query($sql) or die(mysql_error());
        }
    echo 
    "<h1>Updated, photo deleted.</h1>";
    } else {
    echo 
    "<h1>Not deleted.<h1>";

    ?>
    <form>
    <input type=button value="Close" onClick="javascript:window.close();">
    </form>
    </body>
    </html>
    Attached Images Attached Images
    "Oh, and Jenkins--apparently your mother died this morning."

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Your SQL is malformed.
    PHP Code:
    $sql sprintf(
        
    "DELETE FROM photo WHERE id IN ('%s')",
        
    implode(', '$IDs)
    ); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Heres the updated php page
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org\tR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>HoustonASP: Delete providers photos</title>
    <?php include("db_conn_open.php"); ?>
    </head>
    <BODY>
    <?php
    print('<pre>');
    print_r($_POST);
    print('</pre>');
    
    
    if (isset($_POST['delete_photos']))
    {
    $IDs = array();
    foreach ($_POST['ID'] as $ID) {
        $IDs[] = (int)$ID;
        }
        if(count($IDs) > 0){
    $sql = sprintf(
        "DELETE FROM photo WHERE id IN ('%s')", implode(', ', $IDs) ); 
        mysql_query($sql) or die(mysql_error());
        }
    echo "<h1>Updated, photo deleted.</h1>";
    } else {
    echo "<h1>Not deleted.<h1>";
    } 
    ?>
    <form>
    <input type=button value="Close" onClick="javascript:window.close();">
    </form>
    </body>
    </html>
    Attached Images Attached Images
    "Oh, and Jenkins--apparently your mother died this morning."

  12. #12
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ...and does it work?

    If not, what happens, or doesn't?

    Ta.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  13. #13
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    no, I get a warning though (screenshot)
    Thanks...
    Do you know what it means??


    Warning: Invalid argument supplied for foreach() in /home/houst33/public_html/php/update_table_photo.php on line 17
    "Oh, and Jenkins--apparently your mother died this morning."

  14. #14
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure, can you post the form which submits the data?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  15. #15
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's better to post error messages and debugging info (i.e what you go from print_r($_POST)) rather than screenshots. Screenshots have to be approved and aren't visible immediately.
    It seems $_POST['ID'] is not an array. Did you use <input type="checkbox" name="ID[]"> in your markup?

  16. #16
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, when I did
    PHP Code:
    print('<pre>');
    print_r($_POST);
    print(
    '</pre>'); 
    I got,
    Array
    (
    [ID] => 4
    [delete_photos] =>
    [submit] => Delete Photos
    )
    "Oh, and Jenkins--apparently your mother died this morning."

  17. #17
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah, as cranial-bore correctly stated, $_POST['ID'] is not an array and therefore cannot be iterated over with foreach.

    Can you post the form that submits the data to this script? Are you expecting more than one ID?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  18. #18
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, your right...It works when I make the checkbox an array.
    Thx...
    "Oh, and Jenkins--apparently your mother died this morning."

  19. #19
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Oh, are we done then?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  20. #20
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Yes, Thx...
    "Oh, and Jenkins--apparently your mother died this morning."


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
  •