richard_PHP Posted March 3, 2010 Share Posted March 3, 2010 hi all, ive made a page to update a 'blog' however, when i accidentally went on the processing page it added a blank entry into the blog. i want it so that if the page is accessed directly it wont add to the blog. ive tried adding the 'emtpy' syntax but it doesnt work. some of the code $date = $_POST['date']; $title = $_POST['title']; $text = $_POST['entry']; $sql = "INSERT INTO `*****`.`blog` (`entry_id`, `entry_date`, `entry_title`, `entry_text`) VALUES (NULL, '$date', '$title', '$text')"; $result = mysql_query($sql); if ($result) { echo "<p>The following entry:<br /><strong>$date - $title</strong><br /> was added into the database."; echo "<p>Click <a href='updateblog.html'>here</a> to add another entry</p>"; } if (empty($date)) { echo "<p>You have not entered any data. Blog not updated.</p>"; echo "<p>Click <a href='updateblog.html'>here</a> to add another entry</p>"; } else { echo "<p>There was a problem. <br /> $sql <br /> ".mysql_error()."</p>"; } Quote Link to comment Share on other sites More sharing options...
MockY Posted March 3, 2010 Share Posted March 3, 2010 Using the supplied code, you could change it to the following: if ($date == "") { echo "<p>You have not entered any data. Blog not updated.</p>"; echo "<p>Click <a href='updateblog.html'>here</a> to add another entry</p>"; } else { $sql = "INSERT INTO `*****`.`blog` (`entry_id`, `entry_date`, `entry_title`, `entry_text`) VALUES (NULL, '$date', '$title', '$text')"; $result = mysql_query($sql); if ($result) { echo "<p>The following entry:<br /><strong>$date - $title</strong><br /> was added into the database."; echo "<p>Click <a href='updateblog.html'>here</a> to add another entry</p>"; } else { echo "<p>There was a problem. <br /> $sql <br /> ".mysql_error()."</p>"; } } Quote Link to comment Share on other sites More sharing options...
richard_PHP Posted March 3, 2010 Author Share Posted March 3, 2010 i love this forum! =D lol worked a treat, thanks for your help. Quote Link to comment Share on other sites More sharing options...
MockY Posted March 3, 2010 Share Posted March 3, 2010 I'm glad I could help. However, keep in mind that you are altering your database when the script is run, so it's usually a good idea to limit such actions with a submit button/form and only execute the code once the button is clicked. You can simply do that by checking for the existence of the variable created based on the name of the button: if (isset(['buttonname'])) { // Do stuff if the button is clicked } else { // show the form or button } 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.