April 18, 201214 yr Hi Guys, Ok i've made a simple Form for my website, code as follows; <form action='submit.php' action='POST'> <p><input type='text' name='fullname' />Full name </p> <p><input type='text' name='email' />Email</p> <input type='submit' value='Sign Up!' /> </form> I have created a table in my database on my host. here is my submit.php code; <?php $con = mysql_connect("localhost","*****","*******"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("********", $con); if(mysql_query("INSERT INTO email_list (fullname, email) VALUES ('".$_POST['fullname']."', '".$_POST['email']."')")) { echo "Your email has been added to our list!"; } else { echo "There was an error adding your email to our list. Please try again."; } mysql_close($con); ?> Ok so here is the problem, i uploaded everything to the host and filled out the form a few times to make sure it worked. and i got the message "your email has been added to our list" all 3 times. When i went to my table on phpmyadmin the table showed there were 3 records, but the 'name' and 'email' fields were blank......???! Any one have any idea why? You can probably guess im kinda new to php/mysql but trying my bloody best to learn!
April 18, 201214 yr Try it with static values like: if(mysql_query("INSERT INTO email_list (fullname, email) VALUES ('test name', 'test email')")) I can't really see what's wrong with it, just keep substituting static values in place of the dynamic parts to see where the problem resides.
April 18, 201214 yr Author Ok using Static Values Worked, i'll try mess around with it a little more but its doing my bloody head in today lol!
April 18, 201214 yr Think you may need to add ids? <p><input type='text' name='fullname' id='fullname' />Full name </p> Edited April 18, 201214 yr by mrchristoph
April 18, 201214 yr Author Arrrghhhhh I'm starting to hate php/mysql already!!!!! i've spent all morning on this, and looks like i'll be spending all day on it!! Tried as much as i could think of but it just wont work with the dynamic parts!
April 18, 201214 yr http://stackoverflow.com/questions/9002424/php-post-not-working Not much else I can think of I'm afraid
April 18, 201214 yr Author what a tail i really am, in the HTML i had < form action='submit.php' action='POST'> POST is meant to be method='' Well i say im a tail, i hardly know any php/mysql and i got this off of a blog! very amateurish!! Thanks for all your help though mate!! Edited April 18, 201214 yr by gormo7
April 18, 201214 yr what a tail i really am, in the HTML i had < form action='submit.php' action='POST'> POST is meant to be method='' Well i say im a tail, i hardly know any php/mysql and i got this off of a blog! very amateurish!! Thanks for all your help though mate!! I know you maybe new to PHP but if you try to learn it's syntax and how to code your PHP so that it looks a bit neater will make things a lot easier for you. If you aren't already then check out this site, it's really good for the basics and a bit more... w3schools Also when inserting values into a database, create a variable for the GET/POST values like so; $email = $_POST['email']; This not only makes your code look neater but allows you to take appropriate actions towards securing your site such as the following; $email = mysql_real_escape_string($_POST['email']);
April 18, 201214 yr Author Thanks Adam, Yeah i have created variables now im trying to write a code to validate the form before its submitted. Struggling a bit here though! The form is submitting regardless of whether a/all field is empty, but still displaying the correct error message! This PHP lark is going to be a steep learning curve!!
April 18, 201214 yr Well for start you can't actually have $_post[""] in the insert table. What you do is the following. Check variable if excist; if(!isset($_POST["name"])) { echo "Variable has not been set" $name=""; } else { $name=$_POST["name"]; } Then use the insert statement. mysql_query("INSERT INTO email_list (fullname, email) VALUES ('$name','$name')") Note; If variable hasn't been set, no database will be added. Edited April 18, 201214 yr by kensha
April 18, 201214 yr Author Thanks Kensha, I have variables in the INSERT table now. The rest of what you put went straight over my head haha!! But as 'isset ()' returns a boolean value wouldnt i have to have some other php code somewhere to say what to output depending on boolean value? I really need to learn this stuff quick!!! anyway here is what i have so far. <?php $con = mysql_connect("localhost","*****","*******"); if (!$con) { die('Could not connect: ' . mysql_error()); } $fullname=$_POST['fullname']; $email=$_POST['email']; mysql_select_db("********", $con); if(mysql_query("INSERT INTO email_list (fullname, email) VALUES ('$fullname', '$email')")) if (empty($fullname) && empty($email)){ print "No email address and no name was entered.<br>"; } elseif (empty($fullname)){ print "No name was entered.<br>Please include a name.<br>"; } elseif (empty($email)){ print "No email address was entered.<br>Please include your email."; } else { echo "Your email has been added to our list!"; } mysql_close($con); ?> I can understand why its submitting the form first and then displaying the relevant error message, but if i put the INSERT INTO bit at the end of the code it doesnt work at all!!
April 18, 201214 yr Thanks Adam, Yeah i have created variables now im trying to write a code to validate the form before its submitted. Struggling a bit here though! The form is submitting regardless of whether a/all field is empty, but still displaying the correct error message! This PHP lark is going to be a steep learning curve!! To validate the form before it is submitted needs to be done in Javascript because Javascript is client side, the form can only be validated after it has been submitted with PHP because PHP is server side. It should always be validated with PHP before you start validating with Javascript. To validate with PHP you would do something like the following... $email = mysql_real_escape_string($_POST['email']); if ($email == "") { // email is empty header('location: /'); die(); } // Then if the email is not empty the script will continue and you can // validate against other things such as if the email address is an email address Edited April 18, 201214 yr by adamsmith
April 18, 201214 yr Also keep using if. Else is just a second option to that group. You can say. if (empty($lala)) {echo "blabla";} if(empty($lulu)) {echo "blublu";} else {} only use the else if you want to get something echo'd out of the opposite statement. Edited April 18, 201214 yr by kensha
April 18, 201214 yr Author Thanks for all the advice guys, its much appreciated. I've actually got it all working now!! i know its only really simple but its a big achievement for me! Now going to look at making the validation more thorough but at least now its at the lowest acceptable level! this is the code i used for anyones future reference - hopefully this will help someone else out someday, its took me 7 bloody hours!!! <?php $con = mysql_connect("localhost","*****","*****"); if (!$con) { die('Could not connect: ' . mysql_error()); } $fullname=$_POST['fullname']; $email=$_POST['email']; //if no fullname entered and no email entered print an error if (empty($fullname) && empty($email)){ print "No email address and no name was entered.<br>Please include a name and email address."; } //if no name entered send print an error elseif (empty($fullname)){ print "No name was entered.<br>Please include a name."; } //if no email entered send print an error elseif (empty($email)){ print "No email address was entered.<br>Please include your email."; } //if the form has both an email and a message elseif (!empty($fullname) && !empty($email)) { mysql_select_db("*****", $con); if(mysql_query("INSERT INTO email_list (fullname, email) VALUES ('$fullname', '$email')")) echo "Your email has been added to our list!"; } mysql_close($con); ?> Edited April 18, 201214 yr by gormo7
Create an account or sign in to comment