Page 1 of 1

Writing a value from a dropdown list to a database

Posted: Fri Apr 02, 2010 9:52 pm
by dimendoll
I am making an RSVP page for a wedding web page and my script is retuning my own error array as though there has been no choice made from the drop down lists, even though a choice has been made.

THIS IS MY CODE:

Code: Select all

<?php # rsvp2.php 
//March 2010
session_start();

//check if the form has been submitted:
 if (isset($_POST['submitted'])) {
	$errors = array(); // Initialize and error array.

 require_once ('../mysqli_connect.php');
	 //connect to the db.
	
	//check for number of guests:
	if (empty($_POST['attending'])) {
			$errors[] = 'Please let us know if you are joining us on our happy day.'; 
			} else {
		$a =mysqli_real_escape_string($dbc,trim($_POST['attending']));
		} 
	
	//Check for a first name:
	if (empty($_POST['guest1'])) {
		$errors[] = 'You forgot to enter your name.';
	}else{
		$g1=mysqli_real_escape_string($dbc,trim($_POST['guest1']));
	}

	//Guest 2
	if (!empty($_POST['guest2'])) {
		$g2 =mysqli_real_escape_string($dbc,trim($_POST['guest2']));
	}
	
	//Guest 3
	if (!empty($_POST['guest3'])) {
		$g3 =mysqli_real_escape_string($dbc,trim($_POST['guest3']));
	}
	
	//Guest 4
	if (!empty($_POST['guest4'])) {
		$g4 =mysqli_real_escape_string($dbc,trim($_POST['guest4']));
	}
	
	//check for type of party attendance:
	if (empty($_POST['event'])) {
			$errors[] = 'Please confirm which events you are attending.';
			} else {
		$event =mysqli_real_escape_string($dbc,trim($_POST['event']));
		} 
	
	if(empty($errors)) { //If everything's OK.
	
	//Register the user in the database...
	
	
	//Make the query:
	$q = "INSERT INTO rsvp ( attending, guest1, guest2, guest3, guest4, event) VALUES ('$a', '$g1', '$g2', '$g3', '$g4', '$event' )";
	$r = mysqli_query ($dbc, $q); // Run the query.
	if ($r) {// If it ran OK.
	
	//Print a message:
	echo '<h1>Thank you!</h1>
	<p> we appreciate your response, Kalyn and Jason. </p><p><br /></p>';
	
	}else {// If it did not run OK.
	
	// Public message:
	echo '<h1>System Error</h1>
	<p class="error"> You could not be registered due to a system error. We apologise for any inconvenience.</p>';
	
	//Debugging message:
	echo '<p>' . mysqli_error($dbc). '<br /><br /> query: ' . $q. '</p>';
	}
	// End of if ($r) IF.
	
	mysqli_close($dbc); 
	
	// Close the database connection.
	//Include the footer and quit the script:

	exit();		

	} else { //Report the Errors.
	
	echo '<h1>Error!</h1> <p class="error"> The following error(s) occured:<br />';
	foreach ($errors as $msg) {// Print each error.
	echo " - $msg<br />\n";
	}
	echo '</p><p>Please try again. </p><p><br /></p>';
	} // End of if (empty($errors)) IF.
	
	mysqli_close($dbc);// Close the database connection.
 }
 session_write_close();
 //End of the main Submit conditional.

?>
<h1> RSVP</h1>
<form action="rsvp2.php" method="post">
	<p>Please indicate how many people are attending on your invitiation, or if you can't make it select "None" and leave your name.
  <?php 
  $attendees = array (1=> 'Select one', 'none', 'one', 'two', 'three', 'four');

echo '<select name="attendees">';

foreach ($attendees as $key => $attending) {
	
	echo "<option value= \"$key\" >$attending</option>\n";
	
}

	echo '</select>';
	?></p>
  	<p>Name 1: <input type="text" name="guest1" size="60" maxlength="80" value="<?php if (isset($_POST['guest1'])) echo $_POST['guest1'];?>" /></p>
    
    
    <p>Partner or Guest name 2: <input type="text" name="guest2" size="60" maxlength="80" value="<?php if (isset($_POST['guest2'])) echo $_POST['guest2']; ?>" /></p>
    
    <p> *Please indicate childrens' names if you have recieved a family invitation:</p>
    <p>Partner or Guest name 3: <input type="text" name="guest3" size="60" maxlength="80" value="<?php if (isset($_POST['guest3'])) echo $_POST['guest3']; ?>" /></p>
    
    <p>Partner or Guest name 4: <input type="text" name="guest4" size="60" maxlength="80" value="<?php if (isset($_POST['guest4'])) echo $_POST['guest4']; ?>" /> </p>
   
   <p> Please let us know what part of our day you will be sharing:</p>
    <p>
<?php 
	$party = array (1=> 'Select one', 'None', 'All', 'Ceremony only', 'Ceremony and Dinner', 'Ceremony and Dance', 'Dinner only', 'Dinner and Dance', 'Dance only');

	echo '<select name="party">';

	foreach ($party as $key => $event) {
	
	echo "<option name =\"$key\">$event</option>\n";
	
}
	 if (isset($_POST['event'])) echo $_POST['event']; 
     
	echo '</select>';
?>
	</p>  
    
    <p><input type="submit" name="submit" value="RSVP" /> </p>
     
    <input type="hidden" name="submitted" value="true" /> 
    </form>
I hope someone can help me solve this.

Thanks,
Dimendoll

Re: Writing a value from a dropdown list to a database

Posted: Fri Apr 02, 2010 11:33 pm
by JakeJ
The first thing you should check when your data is not being written to the database is whether or not your field is actually populated.

Echo out your data first to see what it's trying to write to the database. Next, check to see that your data type you're trying to input, matches the field you're trying to insert it in to.

Then you should also (this does happen) make sure none of your field or table names are reserved words.

Re: Writing a value from a dropdown list to a database

Posted: Sat Apr 03, 2010 9:45 am
by samwho
Initial thoughts:

$_POST['attending'] is never set anywhere, you don't have a form field with the name="attending" attribute. You do, however, have name="attendee" at some point :) Possibly the issue?

Re: Writing a value from a dropdown list to a database

Posted: Sat Apr 03, 2010 9:55 am
by dimendoll
You are absolutely right Samwho, I stumble accross that myself. Now that I have changed it all is good and the wedding will go on! :D

Re: Writing a value from a dropdown list to a database

Posted: Sat Apr 03, 2010 9:56 am
by dimendoll
Thank you both for your replies. I appreciate you taking the time to try and help.

Dimendoll

Re: Writing a value from a dropdown list to a database

Posted: Sat Apr 03, 2010 10:32 am
by samwho
Not a problem, sir :)