Jump to content

empty


richard_PHP

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
}

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.