Log in

View Full Version : PHP form data not populating table



kairick
06-30-2010, 06:44 PM
I need to populate user input on this form to a table, "registrations". I inserted some INSERT TO code (used w3c example as reference). I'm not getting any errors, but it's not populating the table with the information. INSERT TO code begins with $con below.


<?php
print_r($_POST);

{
if(($_POST['college']=="")||($_POST['streetaddress']=="")||($_POST['city']=="")||($_POST['zip']=="")||($_POST['state']=="")||($_POST['contactperson']=="")||($_POST['contactemail']=="")||($_POST['phone']=="")||($_POST['fax']=="")||($_POST['giss1']=="")||($_POST['giss2']==""))
{
echo "<html><body><p>The following fields are <strong>required</strong>.</p><ul>";
if($_POST['college'] == ""){ echo "<li>*College</li>"; }
if($_POST['streetaddress'] == ""){ echo "<li>*Street Address</li>"; }
if($_POST['city'] == ""){ echo "<li>*City</li>"; }
if($_POST['zip'] == ""){ echo "<li>*Zip</li>"; }
if($_POST['state'] == ""){ echo "<li>*State</li>"; }
if($_POST['contactperson'] == ""){ echo "<li>*Contact Person</li>"; }
if($_POST['contactemail'] == ""){ echo "<li>*Contact Email</li>"; }
if($_POST['phone'] == ""){ echo "<li>*Phone</li>"; }
if($_POST['session'] == "unchecked") { echo "<li>You must select a GISS session</li>"; }
echo "</ul><p>Please use your browsers <a href=\"javascript:history.back();\">Back</a> button and fill out these fields.</p></body></html>";
}

if ($selected_radio == 'giss1') {
$giss1_status = 'checked';
}
else if ($selected_radio == 'giss2') {
$giss2_status = 'checked';
}

if(isset($_POST['submit'])) {

$to = "me@email.com";
$subject = "Contact Has Registered";
$college = $_POST['college'];
$streetaddress = $_POST['streetaddress'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$contactperson = $_POST['contactperson'];
$contactemail = $_POST['contactemail'];
$phone = $_POST['phone'];
$selected_radio = $_POST['session'];
print $session;
}
$body = "From:$college
Street Address:$streetaddress
City:$city
State:$state
Zip Code:$zip
E-Mail:$contactemail
Phone Number:$phone
Contact Email:$contactemail
GISS Session:$selected_radio";

mail($to, $subject, $body);

$sendto = $_POST['contactemail'];
$ccto = "me@email.com";
$subject = "Registration";
$message = "Thank you for registering";

if(mail($sendto, $subject, $message, $header)) header("location: attendee.html");
}

$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("registrations", $con);

$sql="INSERT INTO attendee_registrants (college, street_address, city, state, zip, contact_person, contact_email, phone, fax, session)
VALUES
('$_POST[college]','$_POST[streetaddress]','$_POST[city]'$_POST[state]','$_POST[zip]','$_POST[contactperson]','$_POST[contactemail]','$_POST[phone]','$_POST[fax]','$_POST[session]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)

?>
<script type=text/javascript>
setTimeout("location.href='index.html'", [3000]);
</script>

bluewalrus
06-30-2010, 06:56 PM
If you echo $sql; is the statement appearing as you'd expect it to (with the values)?

You also may want to use javascript for the validation that all fields are entered, but that's something else...

kairick
06-30-2010, 08:43 PM
Thanks for responding.

Nothing's appearing at all. I originally had the form working to just send the confirmation emails and later added in the INSERT TO. The form is still behaving as though I never entered that extra code, so nothing is different.

bluewalrus
07-01-2010, 02:10 AM
The blankness than is from an error(s) with your php. I've rewritten parts of your code and put in notes.



<?php
print_r($_POST);
if($_POST['college']==""||$_POST['streetaddress']==""||$_POST['city']==""||$_POST['zip']==""||$_POST['state']==""||$_POST['contactperson']==""||$_POST['contactemail']==""||$_POST['phone']==""||$_POST['fax']==""||$_POST['giss1']==""||$_POST['giss2']=="") {
?>
<html>
<body>
<p>The following fields are <strong>required</strong>.</p>
<ul>
<?php
if($_POST['college'] == ""){ echo "<li>*College</li>"; }
if($_POST['streetaddress'] == ""){ echo "<li>*Street Address</li>"; }
if($_POST['city'] == ""){ echo "<li>*City</li>"; }
if($_POST['zip'] == ""){ echo "<li>*Zip</li>"; }
if($_POST['state'] == ""){ echo "<li>*State</li>"; }
if($_POST['contactperson'] == ""){ echo "<li>*Contact Person</li>"; }
if($_POST['contactemail'] == ""){ echo "<li>*Contact Email</li>"; }
if($_POST['phone'] == ""){ echo "<li>*Phone</li>"; }
if($_POST['session'] == "unchecked") { echo "<li>You must select a GISS session</li>"; }
?>
</ul>
<p>Please use your browsers <a href=\"javascript:history.back();\">Back</a> button and fill out these fields.</p>
</body>
</html>
<?php
if ($selected_radio == 'giss1') {
$giss1_status = 'checked';
} else if ($selected_radio == 'giss2') {
$giss2_status = 'checked';
}
if(isset($_POST['submit'])) {
$to = "me@email.com";
$subject = "Contact Has Registered";
$college = $_POST['college'];
$streetaddress = $_POST['streetaddress'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$contactperson = $_POST['contactperson'];
$contactemail = $_POST['contactemail'];
$phone = $_POST['phone'];
$selected_radio = $_POST['session'];
$fax = $_POST['fax'];
//print $session; session is never set and you don't need to print it
echo $selected_radio;// remove this if using header.
//} why close the if here?
$body = "From:$college\nStreet Address:$streetaddress\nCity:$city\nState:$state\nZip Code:$zip\nE-Mail:$contactemail\nPhone Number:$phone\nContact Email:$contactemail\nGISS Session:$selected_radio";
mail($to, $subject, $body);
$sendto = $_POST['contactemail'];
$ccto = "me@email.com";
$subject = "Registration";
$message = "Thank you for registering";
$header = "From: me@me.com";
// this is jambled are you trying to redirect them if the email is sent? this can't be achieved because you are echoing the selected radio and printing the posts above there are also missing brackets
//assuming you dont echo selected radio and print the posts though...
if (mail($sendto, $subject, $message, $header)) {
header("location: attendee.html");
}
$con = mysql_connect("localhost","username","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("registrations", $con);

$sql="INSERT INTO attendee_registrants (college, street_address, city, state, zip, contact_person, contact_email, phone, fax, session) VALUES ('$college','$streetaddress','$city'$state','$zip','$contactperson','$contactemail','$phone','$fax','$selected_radio')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
mysql_close($con);
?>
1 record added
<?php
}
?>
<script type=text/javascript>
setTimeout("location.href='index.html'", [3000]);
</script>

kairick
07-01-2010, 04:31 AM
Thanks for putting in the time to edit my code :)

The parts you noted for correction are functioning/were functioning fine (confirmation emails). I tried to remove/alter the options you suggested and got T_variable errors. It's the INSERT TO stuff that's not working. The form is sending correctly and confirmation emails are sending fine with the code I posted. I'm not getting any errors as-is. The table's just not populating.

nextdownload
01-06-2011, 06:14 PM
Replace
$sql="INSERT INTO attendee_registrants (college, street_address, city, state, zip, contact_person, contact_email, phone, fax, session)
VALUES
('$_POST[college]','$_POST[streetaddress]','$_POST[city]'$_POST[state]','$_POST[zip]','$_POST[contactperson]','$_POST[contactemail]','$_POST[phone]','$_POST[fax]','$_POST[session]')"; with


$sql="INSERT INTO attendee_registrants (college, street_address, city, state, zip, contact_person, contact_email, phone, fax, session)
VALUES
('$_POST[college]','$_POST[streetaddress]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[contactperson]','$_POST[contactemail]','$_POST[phone]','$_POST[fax]','$_POST[session]')";