Jump to content

Recommended Posts

i have a form(test1.php) which i type my data in, i then have another form(test2.php) which has the data to the database. however if i just type in the address of (test2.php) then an empty field is added to the database. I know the solution is easy i just seem to be having a blank moment.

 

Any help?

 

Cheers In Advance

Link to comment
https://forums.phpfreaks.com/topic/173551-php-submit-code/
Share on other sites

i have a form(test1.php) which i type my data in, i then have another form(test2.php) which has the data to the database. however if i just type in the address of (test2.php) then an empty field is added to the database. I know the solution is easy i just seem to be having a blank moment.

 

Any help?

 

Cheers In Advance

 

Use this on your test2.php like peranha said. If nothing was submitted and you go to test2.php it'll give an error message.

if (isset($_POST['submit'])) {//  Name of your submit button
    //  Code to process
}
else {
    //  Error message
}

Link to comment
https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914852
Share on other sites

<?php

if (isset($_POST['submit'])) {//  Name of your submit button
    //  Code to process
}
else {
    //  Error message
}

$con = mysql_connect('localhost', '', 'myuklive_Bands');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("myuklive_Bands", $con);

$sql="INSERT INTO gg_news (Title, Permalink, Content, Author, Date, type)
VALUES
('$_POST[rsstitle]','$_POST[rsslink]','$_POST[rssdescription]','$_POST[rssauthor]','$_POST[rssdate]','$_POST[type]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 News Feed Added";

mysql_close($con)
?>

 

if this is correct it still allows blank fields to be added?

Link to comment
https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914883
Share on other sites

Something like the below.

 

All your code that needs to be process goes inside the if statement. If the submit button from the form was not clicked it'll show the else statement. Make sure the submit button on your form matches the first line where it says Name of your submit button.

<?php

if (isset($_POST['submit'])) {//  Name of your submit button
    //  Code to process
$con = mysql_connect('localhost', '', 'myuklive_Bands');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("myuklive_Bands", $con);

$sql="INSERT INTO gg_news (Title, Permalink, Content, Author, Date, type)
VALUES
('$_POST[rsstitle]','$_POST[rsslink]','$_POST[rssdescription]','$_POST[rssauthor]','$_POST[rssdate]','$_POST[type]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 News Feed Added";

mysql_close($con)	
}//End submit if
else {
    //  Error message
echo 'This is the error message because the submit button was not clicked on the form.';	
}


?>

Link to comment
https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914887
Share on other sites

or just do a quick check if its submit and do an error message if its not, and exit. this way you don't have to surround the process with an if or else block, but you get the same effect

 


if (!isset($_POST['submit'])){
echo "error";
exit();
}

//do processing

 

either way all the examples given in this post would work perfectly for you

Link to comment
https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914890
Share on other sites

or just do a quick check if its submit and do an error message if its not, and exit. this way you don't have to surround the process with an if or else block, but you get the same effect

 


if (!isset($_POST['submit'])){
echo "error";
exit();
}

//do processing

 

either way all the examples given in this post would work perfectly for you

 

This does not prevent from adding empty fields to db. As I posted earlier, $_POST['submit'] is set everytime user posts the form, no matter even if all the form fields are empty. That is why you have to validate the fields also and see if you allow the values to be inserted or not.

Link to comment
https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914896
Share on other sites

As i understood, he wasn't talking about submitting blank form info, but a user going directly to the page that processes the form, and since they didn't go through the form, all the form data would be blank. Not to mention that every other reply in this thread uses the same logic, and in your example wouldn't work either... All the replies in this thread call the isset function on the submit key of the post variable, but none of them do any data quality checks, so fundamentally, your post applies to every answer in this thread.

 

But you are correct, you should also make sure that the post variables are set to something before you insert them into the databse

Link to comment
https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914900
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.