glens1234 Posted February 20, 2008 Share Posted February 20, 2008 Hi there. I have just downloaded mamp and have written a small php script. The script bascially inserts data from a form into a database. When i fill out the form i get the msg "Error, insert query failed", which i defined in the script. So can someone please help me understand why my insert statement is failing? The code for processing the form is... <?php include 'config.php'; include 'opendb.php'; $name = stripslashes($_POST['Name']); //$a_file = $_POST['a_file'];dont need this code yet $languages_spoken = $_POST['languages_spoken']; $query = "INSERT INTO employees (name,lang_spoken) VALUES ('$name', ‘$languages_spoken') "; mysql_query($query) or die('Error, insert query failed'); $query = "FLUSH PRIVILEGES"; mysql_query($query) or die('Error, insert query failed'); echo "Thank you ". $name . ". We will be in contact with you shortly"; include 'closedb.php'; ?> The code of the html form is... <!--- onyl display form if it has not already been submited ---> <?php if (!isset($_POST['submit'])) { ?> <!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"> <head> <title>Application form</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="stylesheet.css" type="text/css" media="screen" /> </head> <body> <form method="post" action="process_app_form.php"> <table border="0" cellspacing="0" cellpadding="2"> <tr> <td>Name:</td><td><input type="text" name="Name" maxlength="30" size="26" /></td> </tr> <tr><td>Languages Spoken</td> <td><select name="languages_spoken" size="5"> <option value="English">English</option> <option value="French">French</option> <option value="Spanish">Spanish</option> <option value="German">German</option> </select></td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </form> </body> </html> <?php } ?> i created the table using phpmyadmin. As far as i can remember the statements were... CREATE TABLE employees ( name varchar(20) NOT NULL DEFAULT "", lang_spoken TINY INT UNSIGNED NOT NULL DEFAULT '0' ) TYPE=MyISAM; Since both the opendb.php and config.php files are not reporting any errors i will not include them. Any sugguestions? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/ Share on other sites More sharing options...
revraz Posted February 20, 2008 Share Posted February 20, 2008 Looks like you used a backtick instead of a single quote -->`$languages_spoken' Also, make your errors different for each query or echo the query so you know which one is generating the error. Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-471905 Share on other sites More sharing options...
glens1234 Posted February 20, 2008 Author Share Posted February 20, 2008 hi. i changed the backtick to a single quote but still got the erro msg. could it be anything else? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-471921 Share on other sites More sharing options...
dotBz Posted February 20, 2008 Share Posted February 20, 2008 Hi, maybe you could try casting languages spoken to an int. (int) $_POST['languages_spoken'] Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-471931 Share on other sites More sharing options...
glens1234 Posted February 20, 2008 Author Share Posted February 20, 2008 hmmm. that didnt seem to work either. thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-471940 Share on other sites More sharing options...
revraz Posted February 20, 2008 Share Posted February 20, 2008 If $languages_spoken is a INT, then remove the quotes completely from around it. And again, echo your query so you can see what is being sent. Turn on error reporting so you can get an actual error from MySQL. mysql_query($query) or die("Error, insert query failed $query" .mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-471946 Share on other sites More sharing options...
dotBz Posted February 20, 2008 Share Posted February 20, 2008 Agree. Put mysql_error() and remove single quotes if it's an INT Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-471952 Share on other sites More sharing options...
glens1234 Posted February 20, 2008 Author Share Posted February 20, 2008 thanks for you help guys! However, i just cant seem to figure this out. Firstly i noticed that the list box does not store an integer value but a string so i altered the field in my database to varchar(20). i also put the single quotes back around Languages_spoken variable. Then i echoed the Languages_spoken variable to the screen and i got the message, "your chosen language is:EnglishError, insert query failed No database selected" So can anyone sugguest why it's says EnglishError as opposed to english? In regards to "No database selected", i used the mysql_error() function and got the msg... "Access denied for user ''@'localhost' to database 'translators'" At the moment the username and password are left blank as i dont know where or how to set them. Can i do this within phpmyadmin? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-472052 Share on other sites More sharing options...
glens1234 Posted February 20, 2008 Author Share Posted February 20, 2008 ok ive set my username and pw in phpmyadmin. This what i now have for my config.php <?php $dbhost = "localhost:8889"; //this is the mysql port number on osx $dbuser = "glens1234"; $dbpass = "mypw"; $dbname = 'translators'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname) or die ('Error connecting to mysql '.mysql_error()); ?> And now i get the "Error connecting to mysql" msg. any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-472060 Share on other sites More sharing options...
craygo Posted February 20, 2008 Share Posted February 20, 2008 Did you change the port for your server because 8889 is not the default port Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-472064 Share on other sites More sharing options...
glens1234 Posted February 20, 2008 Author Share Posted February 20, 2008 all sorted!! I found some info on mamp which said both the username and password had to be 'root'! thanks for your help people! Quote Link to comment https://forums.phpfreaks.com/topic/92141-new-to-php-and-was-wandering-if-someone-could-have-a-look-at-my-code/#findComment-472072 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.