Jump to content

Submitting form data to table


gazfocus

Recommended Posts

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";


}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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";


}
?>

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.