Page 1 of 1

form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 3:17 pm
by podarum
Please help, I'm trying to add some of my form variables from PHP to mysql. I have a form that connects to my process.php page and POSTs the varaibles to it as well.. that works fine... I also can connect to mysql no problem, but I cannot (for some reason) add the results of the form to mysql... I'm using a localhost (XAMPP) and I've created a table in mysql (using myphpadmin) with the variables ... code I'm using is below : Thanks

Code: Select all

 
<?php
mysql_connect ("localhost","root","") or die(mysql_error()); 
mysql_select_db("mysql") or die(mysql_error());
 
$Email = $_POST['Email'];
$Score = $_POST['Score'];
 
mysql_query ("INSERT INTO index (Email, Score) VALUES ('{$Email}', '{$Score}');");
 
print "Your Score is : " . $Score;
print "<br />";
?>

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 3:30 pm
by patrickmvi
Add this after your mysql_query statement:

echo "The MySQL Error Was: " . mysql_error();

Re-run the script, see what the error is and post it back.

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 3:36 pm
by podarum
The error is:

The MySql Error Was: 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 'index (Email, Score) VALUES (", '344')' at line 1 Your Score is : 344

Score is one the values on my form and I just put in 344 to test it...

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 6:06 pm
by Eric!
What's up with the brackets? Try this:

mysql_query ("INSERT INTO index (Email, Score) VALUES ('$Email', '$Score')");

Your string should look like:

INSERT INTO index (Email, Score) VALUES (joe@blow.com,344)

you could assign a variable to your query and echo it out to see if you are sending what you think you are.

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 7:26 pm
by McInfo
Eric! wrote:What's up with the brackets?
Curly braces (brackets) are legitimate delimiters in strings.

I believe the problem is that INDEX is a reserved word in SQL. Use backticks (backticks are not single quotes) or change the name of your table. Also, queries passed to mysql_query() should not include a semicolon.

Code: Select all

INSERT INTO `index` (Email, Score) VALUES ...
Edit: This post was recovered from search engine cache.

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 9:18 pm
by podarum
Thanks everyone ..changing the table name worked... I assumed that wasn't the problem becasue I also tried it with index2 and that didn;t work as well..

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 9:44 pm
by podarum
I have a new problem now, with inserting new records into mysql table..using the form it only takes the first entry, then any other entry I try adding, it doesn't show up or add to the table..

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 10:11 pm
by patrickmvi
Check the error now and output it again. It may have to do with a primary key or unique index you set up on the table.

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 10:15 pm
by podarum
error is this:

The MySql Error Was:Duplicate entry '0' for key 'PRIMARY'Your Score is : 111

how can I fix that?

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 10:37 pm
by podarum
thanks it worked... I changed the primary field from an INT to a BIGINT and made it Auto Increment.

Re: form -> PHP -> mySql adding new results problems

Posted: Tue Jun 23, 2009 10:38 pm
by patrickmvi
That means that you're trying to enter a second record with the same primary key as a record that already exists. If you don't want to have that restriction, you'll have to remove the primary key from the table itself.

Re: form -> PHP -> mySql adding new results problems

Posted: Wed Jun 24, 2009 2:02 am
by emix
Instead of `bigint` use `int unsigned`.