Form/PHP Help
I have a form that I want to store the results in a mysql table. I must have something wrong with my code. could someone look at it for me.
Form Code
Name
Questions/Comments
Questionaire.PHP Code
Untitled Document
<?php
include("dogpics.inc");
mysql_connect($hostname,$username, $password) OR DIE (" ");
mysql_select_db($dbname);
$name =$_POST['name'];
$mail =$_POST['email'];
$comment =$_POST['comment'];
$sql = 'INSERT INTO `questionaire` (`Date`,`Name`,`Email`,`Comment`)
VALUES (CURDATE(), \'$name\',\'$mail\',\'$comment\');';
mysql_query($sql);
?>
Busy posted this at 09:26 — 25th January 2006.
He has: 6,151 posts
Joined: May 2001
change
$sql = 'INSERT INTO `questionaire` (`Date`,`Name`,`Email`,`Comment`)
VALUES (CURDATE(), \'$name\',\'$mail\',\'$comment\');';
to
$sql = "INSERT INTO questionaire (Date,Name,Email,Comment)
VALUES (CURDATE(), '$name','$mail','$comment')";
Also if you can tell us the error you are getting it would be helpful
Chrislt posted this at 13:19 — 25th January 2006.
They have: 14 posts
Joined: Jan 2006
That didnt seem to work. I'm not getting any errors, everything seems to be working but for some reason the database is not getting updated. Im using godaddy hosting if that helps. The script I used came from them.
timjpriebe posted this at 16:58 — 25th January 2006.
He has: 2,667 posts
Joined: Dec 2004
Does dogpics.inc have the correct values for $hostname,$username, $password and $dbname ?
Greg K posted this at 17:13 — 25th January 2006.
He has: 2,145 posts
Joined: Nov 2003
Even though it is still not working with what Busy suggested, keep the formatting that he used. Without the double quotes you will get the actual string '$name','$mail','$comment' instead of the values of the variables.
Also, your or DIE(" ") statement is not that informative. Imagine if you had about 10 of these in a script, you run it and the page is blank. Unless you happen to be outputing things to the browser in between then, you will not know where it died. It is good to get into the practice of using good error messages.
For this one, I would recommend something along the lines of die('Could not connect: ' . mysql_error());
Lastly if all else seems to be working correctly, echo out the sql statement right before executing it. If there is something misformatted in it, this can help you spot it. Sometimes with all the quotes, variable names, backslashes, etc, it is easy to miss the mistake in the assignemtn line.
Until you get it working, add echo "\nSQL: $sql \n"; right before the query line.
-Greg
MyFreeCounter posted this at 21:05 — 27th January 2006.
They have: 7 posts
Joined: Jan 2006
also along the same lines as Greg K. Change:
mysql_query($sql);
to:
mysql_query($sql) or die("MySQL Query Error: ".mysql_error());
if there's something wrong with your query, this will tell you. Also make sure your connection variables are correct (someone already suggested that), and make sure you have added the user to the database you are trying to connect to.
Matt Pegler
MyFreeCounter Owner/Operator
http://www.MyFreeCounter.net - free hit, realtime, and click counters, as well as statistics
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.