Jump to content

Recommended Posts

Hi, I have this form which should write the details to the database and display the results afterwards, enabling the user to create a diary.

 

Here is the code

<?php 
include 'core/init.php';
protect_page();
include 'includes/overall/header.php'; 

    $user_id = $_POST['user_id'];
	$username = $_POST['username'];
	$diary_entry = $_POST['diary_entry'];

	mysql_query("INSERT INTO diary (user_id, username, diary_entry, datetime) VALUES({$_SESSION['user_id']}, {$_SESSION['username']}, '$diary_entry', NOW())");
    die(mysql_error());
	

?>

    <form action="" method="POST">
	<input type="hidden" name="user_id" value="<?php echo $user_data['user_id']; ?>"></li>
    <input type="hidden" name="username" value="<?php echo $user_data['username']; ?>"></li>
	<ul>
		<li>
			Diary Entry*:<br>
			<textarea name="diary_entry"></textarea>
		</li>
		<li><input type="submit" value="Submit"></li>
		</ul>
	</form>
 
<?php
	$result = mysql_query("SELECT * FROM diary WHERE username = $username");

while($row = mysql_fetch_array($result))
  {
  echo "<ul>";
  echo "<li>" . 'Entry by' . $row['username'] . "</li>";
  echo "<li>" . 'Date of Entry' . $row['entry_date'] . "</li>";
  echo "<li>" . 'Diary Entry' . $row['diary_entry'] . "</li>";
  echo "</ul>";
  }
  ?> 

<?php 

include 'includes/overall/footer.php'; ?>

But I get this error

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , NOW())' at line 1

 

I cant work out whats wrong :( Please help!

Link to comment
https://forums.phpfreaks.com/topic/287920-sql-error-due-to-now/
Share on other sites

the query error is probably because your $_SESSION['username'] value is a string and it isn't enclosed by single-quotes.

 

some tips -

 

1) you should build your sql query statement in a php variable so that you can echo it to see what it actually is.

 

2) your form processing code needs to test if the form was submitted so that it only runs when there's some form data to use.

 

3) you need to validate external data before using it.

 

4) you need to escape string data before putting it into a query statement or you need to use prepared queries.

 

5) the mysql_ database functions are depreciated and will be removed from php in the future. you should be learning either mysqli or PDO functions. see this link - http://us2.php.net/manual/en/mysqlinfo.api.choosing.php

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.