aquatradehub Posted April 21, 2014 Share Posted April 21, 2014 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! Quote Link to comment Share on other sites More sharing options...
Zane Posted April 21, 2014 Share Posted April 21, 2014 (edited) Make sure $diary_entry is free of any and all apostrophes and backslashes. Edited April 21, 2014 by Zane Quote Link to comment Share on other sites More sharing options...
aquatradehub Posted April 21, 2014 Author Share Posted April 21, 2014 Hi thanks for your reply. $diary_entry does not contain anything until the user submits the form. The only thing is the form is not displayed. The only thing showing on that page is nothing. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 21, 2014 Share Posted April 21, 2014 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 Quote Link to comment 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.