Porkie Posted September 8, 2009 Share Posted September 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/ Share on other sites More sharing options...
peranha Posted September 8, 2009 Share Posted September 8, 2009 How are you passing from test1 to test2? If you are using a submit button if (isset($_POST['submit'])) {// Name of your submit button // Code to process } else { // Error message } Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914822 Share on other sites More sharing options...
pneudralics Posted September 8, 2009 Share Posted September 8, 2009 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 } Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914852 Share on other sites More sharing options...
Porkie Posted September 8, 2009 Author Share Posted September 8, 2009 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914883 Share on other sites More sharing options...
pneudralics Posted September 8, 2009 Share Posted September 8, 2009 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.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914887 Share on other sites More sharing options...
TeNDoLLA Posted September 8, 2009 Share Posted September 8, 2009 You can check the fields before inserting for example with !empty($_POST['fieldName']) and if any of the fields are empty you dont want to be then do not insert and display error message. Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914888 Share on other sites More sharing options...
mikesta707 Posted September 8, 2009 Share Posted September 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914890 Share on other sites More sharing options...
TeNDoLLA Posted September 8, 2009 Share Posted September 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914896 Share on other sites More sharing options...
mikesta707 Posted September 8, 2009 Share Posted September 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914900 Share on other sites More sharing options...
TeNDoLLA Posted September 8, 2009 Share Posted September 8, 2009 Yeah I assumed that he has a form with action="test2.php". Quote Link to comment https://forums.phpfreaks.com/topic/173551-php-submit-code/#findComment-914904 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.