form -> PHP -> mySql adding new results problems

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
podarum
Forum Commoner
Posts: 51
Joined: Mon Jun 15, 2009 1:36 pm

form -> PHP -> mySql adding new results problems

Post 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 />";
?>
Last edited by Benjamin on Tue Jun 23, 2009 11:42 pm, edited 1 time in total.
Reason: Added [code=php] tags.
patrickmvi
Forum Commoner
Posts: 32
Joined: Mon Jun 22, 2009 6:45 am
Location: Fort Lauderdale, FL

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

Post 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.
podarum
Forum Commoner
Posts: 51
Joined: Mon Jun 15, 2009 1:36 pm

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

Post 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...
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 11:48 am, edited 1 time in total.
podarum
Forum Commoner
Posts: 51
Joined: Mon Jun 15, 2009 1:36 pm

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

Post 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..
podarum
Forum Commoner
Posts: 51
Joined: Mon Jun 15, 2009 1:36 pm

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

Post 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..
patrickmvi
Forum Commoner
Posts: 32
Joined: Mon Jun 22, 2009 6:45 am
Location: Fort Lauderdale, FL

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

Post 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.
podarum
Forum Commoner
Posts: 51
Joined: Mon Jun 15, 2009 1:36 pm

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

Post by podarum »

error is this:

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

how can I fix that?
podarum
Forum Commoner
Posts: 51
Joined: Mon Jun 15, 2009 1:36 pm

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

Post by podarum »

thanks it worked... I changed the primary field from an INT to a BIGINT and made it Auto Increment.
patrickmvi
Forum Commoner
Posts: 32
Joined: Mon Jun 22, 2009 6:45 am
Location: Fort Lauderdale, FL

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

Post 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.
User avatar
emix
Forum Newbie
Posts: 8
Joined: Mon Jun 22, 2009 10:32 am
Location: Poland

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

Post by emix »

Instead of `bigint` use `int unsigned`.
Post Reply