gazfocus Posted November 27, 2008 Share Posted November 27, 2008 I have created a form in PHP and want the data from the form to submit into a table in my MySQL database but it is not saving the data to the table. Can anyone help? My code is below: { $title = $_POST[title]; $eventDate = $_POST[dd]."/".$_POST[mm]."/".$_POST[yyyy]; $eventTime = $_POST[eventTime]; $venue = $_POST[venue]; $description = $_POST[description]; $sql="INSERT INTO kirkbydiarydates (eventTime, eventDate, venue, title, description) VALUES ($eventTime, $eventDate, $venue, $title, $description)"; mysql_query($sql) or die('Error, insert query failed'); echo "<b>".$title."</b><br />".$eventTime." ".$eventDate."<br />".$venue."<br /><br />".$description."<br /><br />This event has been successfully added"; } Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/ Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 $sql="INSERT INTO kirkbydiarydates (eventTime, eventDate, venue, title, description) VALUES ('$eventTime', '$eventDate', '$venue', '$title', '$description')"; Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-699990 Share on other sites More sharing options...
gazfocus Posted November 27, 2008 Author Share Posted November 27, 2008 $sql="INSERT INTO kirkbydiarydates (eventTime, eventDate, venue, title, description) VALUES ('$eventTime', '$eventDate', '$venue', '$title', '$description')"; I did try that before posting the question but it still comes up with the Die message Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-699994 Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 Please use: mysql_query($sql) or die(mysql_error()); and tell me what the error message says. Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-699997 Share on other sites More sharing options...
gazfocus Posted November 27, 2008 Author Share Posted November 27, 2008 Please use: mysql_query($sql) or die(mysql_error()); and tell me what the error message says. ah ok, I'm getting Table 'citycm_co_uk.kirkbydiarydates' doesn't exist however the table does exist Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-699999 Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 Hmm... You may want to double check that because mysql_error() doesn't lie. Well, sometimes it does. table - citycm_co_uk field - kirkbydiarydates They both exist? Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700004 Share on other sites More sharing options...
gazfocus Posted November 27, 2008 Author Share Posted November 27, 2008 Hmm... You may want to double check that because mysql_error() doesn't lie. Well, sometimes it does. table - citycm_co_uk field - kirkbydiarydates They both exist? The database name is citycm_co_uk and the table name is kirkbydiarydates. Am I doing this wrong? Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700007 Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 You select your database when you first connect. The others for you query are as follows: $sql="INSERT INTO kirkbydiarydates (eventTime, eventDate, venue, title, description) VALUES ('$eventTime', '$eventDate', '$venue', '$title', '$description')"; kirkbydiarydates - table name eventTime, eventDate, venue, title, description - field names Is this error generated from the insert query I mentioned javascript:void(0);above? ah ok, I'm getting Table 'citycm_co_uk.kirkbydiarydates' doesn't exist Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700011 Share on other sites More sharing options...
gazfocus Posted November 27, 2008 Author Share Posted November 27, 2008 The entire code for the page is below (although I've hidden the username and password). Yeah the error is from the form you helped me with earlier <?php ////////////////////////////////////////// //// MySQL Database Connection /////////// ////////////////////////////////////////// $host = "localhost"; $user = "username"; $db_name= "citycm_co_uk"; $pass= "password"; $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($db_name, $conn) or die(mysql_error()); if ($_POST[viewed] != "yes") { echo "<form name=\"form1\" method=\"post\" action=\"$SERVER[php_SELF]\"> <table width=\"200\" border=\"0\"> <tr> <td valign=\"top\"><input name=\"title\" type=\"text\" id=\"title\" tabindex=\"1\" value=\"Event Title\" size=\"80\" maxlength=\"100\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td height=\"27\" valign=\"top\"> <input name=\"dd\" type=\"text\" id=\"dd\" tabindex=\"2\" value=\"dd\" size=\"5\" maxlength=\"2\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"> <input name=\"mm\" type=\"text\" id=\"mm\" tabindex=\"2\" value=\"mm\" size=\"5\" maxlength=\"2\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"> <input name=\"yyyy\" type=\"text\" id=\"yyyy\" tabindex=\"2\" value=\"yyyy\" size=\"10\" maxlength=\"4\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td valign=\"top\"><input name=\"eventTime\" type=\"text\" id=\"eventTime\" tabindex=\"3\" value=\"Time\" maxlength=\"7\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td valign=\"top\"><input name=\"venue\" type=\"text\" id=\"venue\" tabindex=\"4\" value=\"Venue\" size=\"80\" maxlength=\"100\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td valign=\"top\"><textarea name=\"description\" id=\"description\" cols=\"45\" rows=\"5\" tabindex=\"5\"onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\">Enter discription here</textarea></td> </tr> <tr> <td valine=\"top\"><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Add Event\" /></td> </tr> <input name=\"viewed\" type=\"hidden\" id=\"viewed\" value=\"yes\"> </table> </form>"; } else { $title = $_POST[title]; $eventDate = $_POST[dd]."/".$_POST[mm]."/".$_POST[yyyy]; $eventTime = $_POST[eventTime]; $venue = $_POST[venue]; $description = $_POST[description]; $sql="INSERT INTO kirkbydiarydates (eventTime, eventDate, venue, title, description) VALUES ('$eventTime', '$eventDate', '$venue', '$title', '$description')"; mysql_query($sql) or die(mysql_error()); echo "<b>".$title."</b><br />".$eventTime." ".$eventDate."<br />".$venue."<br /><br />".$description."<br /><br />This event has been successfully added"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700014 Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 Change: echo "</pre> <form name='\"form1\"' method='\"post\"' action="%5C%22%24_SERVER%5B'PHP_SELF'%5D%5C%22">< and before your query, add: echo "".$title." ".$eventTime." ".$eventDate." ".$venue." ".$description." This is what you JUST ADDED..."; echo " Here is your query: $sql "; Tell me what it says... Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700021 Share on other sites More sharing options...
gazfocus Posted November 27, 2008 Author Share Posted November 27, 2008 If I change the $SERVER['PHP_SELF'] to $_SERVER['PHP_SELF'] when I submit the form, it just goes back to the home page. As for the stuff before the query, I get the following: Joint Merseyside Prayer Meeting 7pm 07/12/2008 Millbrook School Jubilee Churches in Liverpool, Wirral & Kirkby meeting together to pray. This is what you JUST ADDED... Here is your query: INSERT INTO kirkbydiarydates (eventTime, eventDate, venue, title, description) VALUES ('7pm', '07/12/2008', 'Millbrook School', 'Joint Merseyside Prayer Meeting', 'Jubilee Churches in Liverpool, Wirral & Kirkby meeting together to pray.') Table 'citycm_co_uk.kirkbydiarydates' doesn't exist Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700029 Share on other sites More sharing options...
xtopolis Posted November 27, 2008 Share Posted November 27, 2008 I tend to agree that mysql doesn't lie.. What if on a test page you do all your DB connection stuff and perform a query: <?php $sql = "SELECT * FROM kirkbydiarydates WHERE 1"; mysql_query($sql) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700069 Share on other sites More sharing options...
gazfocus Posted November 27, 2008 Author Share Posted November 27, 2008 I get "Table 'citycm_co_uk.kirkbydiarydates' doesn't exist" Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700257 Share on other sites More sharing options...
gazfocus Posted November 27, 2008 Author Share Posted November 27, 2008 I've just checked my database and the table 'kirkbydiarydates' definately exists and I have just been able to add a line of data using phpmyadmin. Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700264 Share on other sites More sharing options...
xtopolis Posted November 27, 2008 Share Posted November 27, 2008 This is a toughie.. All I can think of is backtrack until you find out where it stops. So now try: <?php $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($db_name, $conn) or die(mysql_error()); $sql = "SHOW TABLES"; $result = mysql_query($sql); while(list($table) = mysql_fetch_array($result)){ echo $table.'<br />'; } ?> You should get a list of those same tables. Obviously if it shows up on this list, then something else is up.. If it doesn't, then we know a little bit more towards the problem. An alternative is to recreate the table under a different name and try to access it. Quote Link to comment https://forums.phpfreaks.com/topic/134450-submitting-form-data-to-table/#findComment-700489 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.