Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-31-2007, 03:57 AM   PM User | #1
focus310
New Coder

 
Join Date: May 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
focus310 is an unknown quantity at this point
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.
focus310 is offline   Reply With Quote
Old 07-31-2007, 04:16 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,898
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
($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)
firepages is online now   Reply With Quote
Old 07-31-2007, 09:30 AM   PM User | #3
focus310
New Coder

 
Join Date: May 2007
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
focus310 is an unknown quantity at this point
Hello:

Thank you. That did the trick.
focus310 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:50 PM.


Advertisement
Log in to turn off these ads.