phpfan28 Posted March 22, 2012 Share Posted March 22, 2012 Hello, I'm new to this forum and I need some help. I'm creating a simple database that it submits data from a user input. Unfortunatly, it's not sending any data to mysql also the form is not validating each field. <?php if (isset($_POST['submitted'])){ $fields = array( 'email', 'state', 'district', 'gender', 'age', 'profession', 'survey', ); foreach($fields as $fieldName) { if(isset($_POST[$fieldName]) and trim($_POST[$fieldName]) !==''){ $fieldName = trim($_POST[$fieldName]); }else { $errors[] = "Please enter your". $fieldName .""; //code to validate fields } } if(isset($errors)){ require_once('Connections/encourage.php'); $query = "INSERT INTO participants (email, state, district, gender, age, profession, survey) VALUES ('$email', '$state', '$district', '$gender', '$age', '$profession','$survey')"; //databasse connection $result = mysql_query ($query); if ($result){ echo '<h1 id="mainhead">Thanks for submitting</hl> <p>You are now registered</p>'; exit(); }else{ echo '<h1 id="mainhead">System Error</hl> <p>Your registration could not be completed due to a system error We apologize for any incovience</p>';//gives system error echo 'p' . mysql_error(). '<br /><br />Query: ' . $query . '</p>'; exit(); } mysql_close(); } else { echo '<h1 id="mainhead">Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach($errors as $msg) { echo " - $msg<br/>\n"; } echo '</p><p>Please try again.</p><p><br/></p>'; } } ?> <form id="form1" name="form1" method="post" action"registration.php"> <fieldset class="first"> <label class="lableone" for="email">Email:* </label> <input name="email" value="<?php if(isset($_POST['email'])) echo $_POST['name'];?>"/> <label for="state"/>State:* </label> <input name="state" value="<?php if(isset($_POST['state'])) echo $_POST['state'];?>"/> <label for="schooldistrict"/>School District:* </label> <input name="schooldistrict" value="<?php if(isset($_POST['district'])) echo $_POST['district'];?>" /> <label for="gender">Gender:* </label> <select name="gender"> <option>Choose Your Gender</option> <option value="male" <?php echo ($form['gender'] == 'male' ? ' selected' : ''); ?>>Male</option> <option value="female"<?php echo ($form['gender'] == 'female' ? ' selected' : ''); ?>>Female</option> </select> <label for="age"/>Your Age:* </label> <input name="age" type="text" class="age" maxlength="2" value="<?php if(isset($_POST['age'])) echo $_POST['age'];?>" /> <label for="profession"/>Profession:* </label> <input name="profession" value="<?php if(isset($_POST['age'])) echo $_POST['age'];?>" /> <label for="surveys"/>Willingness to participate in future surveys: </label> <input name="surveys" type="checkbox" id="surveys" value="yes" <?php echo ($form['survey'] == 'yes' ? 'checked' : '');?>/> </fieldset> <fieldset> <input class="btn" name="submit" type="submit" value="Submit" /> <input class="btn" name="reset" type="reset" value="Clear Form" /> <input type="hidden" name="submitted" value="TRUE" /> </fieldset> </form> Can someone help me out? Thanks in advanced! Quote Link to comment https://forums.phpfreaks.com/topic/259514-data-is-not-submitting-to-the-mysql-database/ Share on other sites More sharing options...
rythemton Posted March 22, 2012 Share Posted March 22, 2012 $fieldName = trim($_POST[$fieldName]); Can someone help me out? Thanks in advanced! Your problem is in this line of code. You need to use a variable variable here, which means you need a double $: $$fieldName = trim($_POST[$fieldName]); Quote Link to comment https://forums.phpfreaks.com/topic/259514-data-is-not-submitting-to-the-mysql-database/#findComment-1330296 Share on other sites More sharing options...
phpfan28 Posted March 22, 2012 Author Share Posted March 22, 2012 Thanks for your response, It worked but partially. The data does submits but when I click on the checkbox, I get an error message. "Error! The following error(s) occurred: Warning: Invalid argument supplied for foreach() in /homepages/25/d232402382/htdocs/encourage/registration.php on line 79 Please try again." Line 79 points to this line of syntax. echo '<h1 id="mainhead">Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach($errors as $msg) { echo " - $msg<br/>\n"; } echo '</p><p>Please try again.</p><p><br/></p>'; I've made the checkbox optional, so its not required for the user to check it. Quote Link to comment https://forums.phpfreaks.com/topic/259514-data-is-not-submitting-to-the-mysql-database/#findComment-1330300 Share on other sites More sharing options...
jcbones Posted March 22, 2012 Share Posted March 22, 2012 The error you are getting, is because there is no error array. This line: if(isset($errors)){ reads "if there are errors". But, then your error block is held in the else clause of this if/else block. You should change it to, if(!isset($errors)){ which reads "if there are NO errors". Quote Link to comment https://forums.phpfreaks.com/topic/259514-data-is-not-submitting-to-the-mysql-database/#findComment-1330327 Share on other sites More sharing options...
phpfan28 Posted March 22, 2012 Author Share Posted March 22, 2012 Awesome, now my last issue is this error here. "Your registration could not be completed due to a system error We apologize for any incovience pYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADDDATE())' at line 2 Query: INSERT INTO participants (email, state, district, gender, age, profession, survey, registration_date) VALUES ('fakeemail', 'Arizona', 'mesa', 'female', '20', 'something','' ADDDATE())" It seems to me that this has to do with the mysql syntax of "date" my previous mysql syntax was $query = "INSERT INTO participants (email, state, district, gender, age, profession, survey, registration_date) VALUES ('$email', '$state', '$district', '$gender', '$age', '$profession','$survey' ADDDATE())"; I added the registration_date and it was NOW instead of ADDDATE()) since my version of mysql is 5.0. I am just trying to add a date stamp whenever a user submits their data to the database. Quote Link to comment https://forums.phpfreaks.com/topic/259514-data-is-not-submitting-to-the-mysql-database/#findComment-1330363 Share on other sites More sharing options...
Mrwoodchuck Posted March 23, 2012 Share Posted March 23, 2012 it looks like your missing a , in that line of code. $query = "INSERT INTO participants (email, state, district, gender, age, profession, survey, registration_date) VALUES ('$email', '$state', '$district', '$gender', '$age', '$profession','$survey', ADDDATE())"; Quote Link to comment https://forums.phpfreaks.com/topic/259514-data-is-not-submitting-to-the-mysql-database/#findComment-1330383 Share on other sites More sharing options...
phpfan28 Posted March 23, 2012 Author Share Posted March 23, 2012 Thanks. I've inserted the comma into the syntax and still no luck. $query = "INSERT INTO participants (email, state, district, gender, age, profession, survey, registration_date) VALUES ('$email', '$state', '$district', '$gender', '$age', '$profession','$survey', ADDDATE())"; Quote Link to comment https://forums.phpfreaks.com/topic/259514-data-is-not-submitting-to-the-mysql-database/#findComment-1330482 Share on other sites More sharing options...
litebearer Posted March 23, 2012 Share Posted March 23, 2012 I believe ADDDATE() needs some parameters http://www.w3resource.com/mysql/date-and-time-functions/mysql-adddate-function.php Quote Link to comment https://forums.phpfreaks.com/topic/259514-data-is-not-submitting-to-the-mysql-database/#findComment-1330487 Share on other sites More sharing options...
jcbones Posted March 23, 2012 Share Posted March 23, 2012 Perhaps you meant. NOW() or CURDATE(). Quote Link to comment https://forums.phpfreaks.com/topic/259514-data-is-not-submitting-to-the-mysql-database/#findComment-1330519 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.