Jump to content

Data is not submitting to the mysql database


phpfan28

Recommended Posts

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!  8)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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". 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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())";

 

 

 

Link to comment
Share on other sites

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())";

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.