Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Foreach Loop Help

    Hi,

    I'm trying to learn about arrays and how to access them. I have a form which allows a person to select multiple items. When I do a "foreach" and echo the results of the form, the results appear correctly.

    When I try to insert the results into a table, I receive the following error message:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'York)' at line 1

    Can someone help me with this INSERT query?

    Here is the code for the form:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title></title>
    </head>
    <body>
    <form action="checkbox-test.php" method="post">
    <p>Select City:</p>
    <p><input type="checkbox" name="place[]" value="New York">New York</p>
    <p><input type="checkbox" name="place[]" value="Chicago">Chicago</p>
    <p><input type="checkbox" name="place[]" value="London">London</p>
    <p><input type="checkbox" name="place[]" value="San Francisco">San Francisco</p>
    <p><input type="submit" value="submit"></p>
    
    </form>
    </body>
    </html>
    ====
    Here is the PHP script which processes the form:
    Code:
    <?php
    
    
    require_once ('mysql_connect.php'); 
    
    foreach($_POST['place'] as $pvalue){
    $query = "INSERT INTO checkbox_test(place) VALUES ($pvalue)";
    $result = mysql_query($query) or die(mysql_error()); 
    
    //echo "$pvalue <br>";
    }
    ?>
    ==
    My table name is checkbox_test and it contains two fields. The first field is ID which is primary and auto-incremented. The second field is place.

    Thanks for the help.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    ($pvalue) should probably (since its a string) be

    PHP Code:
    <?
    $query 
    "INSERT INTO checkbox_test(place) VALUES ('$pvalue')";
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello:

    Thank you. That did the trick.


  •  

    Posting Permissions

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