Does not populate the table!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saminternetmarketing
Forum Newbie
Posts: 3
Joined: Sun Oct 03, 2010 3:02 pm

Does not populate the table!

Post by saminternetmarketing »

Hi,

Below is a bit of simple code which I am trying to use, but amazingly it is not getting inserted to the table.

Code: Select all

<html>
<form action="testmulti.php" method="post">
<select multiple="yes" size="3" name="services[]">
<option value="food">Food Products</option>
<option value="apparels">Apparels</option>
<option value="electronics">Electronics</option>
<option value="beauty">Beauty Services</option>
<option value="taxnfinance">Tax and Finance</option>
<option value="it">Information Technology</option>
</select>
       <input type="submit" value="Submit" />
</form>
</html>

Code: Select all

<?php

print_r($_POST['services']);
include 'connection.php';

foreach($_POST['services'] as $val) {
mysql_query("INSERT INTO test_multi (values)
VALUES ('$val')");
}
?>
The print_r is showing the array correctly.

But the insert into query is not working. Is there any probable reason?
saminternetmarketing
Forum Newbie
Posts: 3
Joined: Sun Oct 03, 2010 3:02 pm

Re: Does not populate the table!

Post by saminternetmarketing »

The following Mysql error is being shown after the array when I add the code or die("Insert Error: ".mysql_error()); at the end of the foreach loop:-

Array ( [0] => food [1] => apparels [2] => electronics ) Insert Error: 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 'values) VALUES ('food')' at line 1.

Please help me out with this. M going nuts... :banghead:
saminternetmarketing
Forum Newbie
Posts: 3
Joined: Sun Oct 03, 2010 3:02 pm

Re: Does not populate the table!

Post by saminternetmarketing »

Is this a code compatibility issue with the Mysql version. What is the correct code. Tried to find but could not!
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Does not populate the table!

Post by DigitalMind »

saminternetmarketing wrote:

Code: Select all

mysql_query("INSERT INTO test_multi (values)
VALUES ('$val')");

Code: Select all

mysql_query("INSERT INTO test_multi (`values`) VALUES ('$val')");
`values` is a very bad name for field because it's a reserved word!
Post Reply