Jump to content

Adding Data To MySQL Database Problem


iisthesloth

Recommended Posts

Hey guys. This has been bugging me all morning and it should be quite simple. I'm working on the administrative side of a site right now which has 1 MySQL table with 5 rows (id, title, author, text, date). I have a simple form in which the user is able to preview or submit the data. The problem is submitting, although the script is returning no errors and seems to work well, but simply does not add in the rows at all. Heres what I have:

 

<?PHP
$dates = getdate();

//convert all the posts to variables:
$title = $_POST['title'];
$text = $_POST['text'];
$author = $_POST['author'];
$date = $_POST['date'];
$id = $_POST['id'];
?>


<form method="post">
<h3>Date</h3>
<input type='text' name='date' value='<? echo $dates['month']; echo ' '; echo $dates['mday']; echo ',  '; echo $dates['year']; ?>' size=20>
<h3>Title</h3>
<input type='text' name='title' value='<? echo $title; ?>' size=60>
<h3>Content</h3>
<textarea name="text" rows="5" cols="60"><? echo $text; ?></textarea>
<h3>Author</h3>
<input type='text' NAME='author' VALUE='<? echo $author; ?>'  size=10 >

<input type="submit" name="preview" value="PREVIEW">
<input type="submit" name="submit" value="Submit">
</form>




<div id="updates">
<?PHP
require_once('../../mysql_conn.php');

//preview
if($_POST['preview']) 
{
   echo '<h1>'.$date.'</h1>';
   echo '<h2>'.$title.'</h2>';
   echo '<p>'.$text.'</p>';
   echo '<h3>'.$author.'</h3>';
   
   } else {
}

//submit
if($_POST['submit']) 
{
require_once('../../mysql_conn.php');

   $query = "INSERT INTO posts (id, title, text, author, date) VALUES ('NULL', '$title', '$text', '$author', '$date')"; 
  
  $result = @mysql_query ($query);
  
  echo '<h2>Updated!</h2>';
  echo '<h4><a href="http://rolldc.com/">Check it out</a></h4>';
  
      mysql_close();
   } else {
}

?>
</div>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/102889-adding-data-to-mysql-database-problem/
Share on other sites

Hi,

 

The line:

$result = @mysql_query ($query);

 

Try this:

 

$result = mysql_query ($query) or die(mysql_error());

 

This will stop executing your script and display any MySQL erros you may have, this will help you narrow it down to see if it is an SQL problem.

 

Let me know if this works.

Archived

This topic is now archived and is closed to further replies.

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