I'm having issues with the following code...every time I submit, I get no error messages and the data does not submit to the database.
PHP code:
<?php // Script 12.5 - add_student.php
/* This script adds a student to the database. */
// Connect and select.
$servername = "localhost";
$username = "emcc_enrollment";
$password = "******";
$dbname = "emcc_enrollment";
// Create connection
$dbc = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$dbc) {
die("Connection failed: " . mysqli_connect_error());
}
$db_selected = mysqli_select_db($dbc, $dbname);
if($_POST['first_name']){
$first_name = $_POST['first_name'];
}else{
echo "First Name not received";
exit;
}
if($_POST['last_name']){
$last_name = $_POST['last_name'];
}else{
echo "Last Name not received";
exit;
}
if($_POST['gender']){
$email = $_POST['gender'];
}else{
echo "Gender not received";
exit;
}
if($_POST['birthdate']){
$birthdate = $_POST['birthdate'];
}else{
echo "Birthdate not received";
exit;
}
if($_POST['child_lives_with']){
$child_lives_with = $_POST['child_lives_with'];
}else{
echo "Child Lives With not received";
exit;
if($_POST['address1']){
$address1 = $_POST['address1'];
}else{
echo "Address1 not received";
exit;
if($_POST['address2']){
$address2 = $_POST['address2'];
}else{
echo "Address2 not received";
exit;
if($_POST['city']){
$city = $_POST['city'];
}else{
echo "City not received";
exit;
if($_POST['state']){
$state = $_POST['state'];
}else{
echo "State not received";
exit;
if($_POST['zip']){
$zip = $_POST['zip'];
}else{
echo "Zipcode not received";
exit;
if($_POST['schedule']){
$schedule = $_POST['schedule'];
}else{
echo "Schedule not received";
exit;
$sql = "INSERT INTO child_info (first_name, last_name, gender, birthdate, child_lives_with, address1, address2, city, state, zip, schedule) VALUES ('$first_name', '$last_name', '$gender', '$birthdate', '$child_lives_with', '$address1', '$address2', '$city', '$state', '$zip', '$schedule')";
if (mysqli_query($dbc, $sql)) {
echo 'The student has been added!';
} else {
echo 'Error' . $sql . mysqli_connect_error($dbc);
}
mysqli_close($dbc); // Close the connection.
?>
HTML Code:
<!DOCTYPE html PUBLIC "-//w3c//dtd xhtml 1.0 transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Add Student</title>
</head>
<body>
<h1>Add Student</h1>
<form action="add_student.php" method="post">
<p>First Name: <input type="text" name="first_name" size="30" maxsize="30" /></p>
<p>Last Name: <input type="text" name="last_name" size="30" maxsize="30" /></p>
<p>
<select name="gender">
<option value="--">--</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</p>
<p>Birthdate: <input type="date" name="birthdate" /></p>
<p>
<select name="child_lives_with">
<option value="Both Parents">Both Parents</option>
<option value="Mother">Mother</option>
<option value="Father">Father</option>
<option value="Legal Guardian">Legal Guardian</option>
<option value="Other">Other</option>
</select>
</p>
<p>Address Line 1: <input type="text" name="address1" size="30" maxsize="30" /></p>
<p>Address Line 2: <input type="text" name="address2" size="30" maxsize="30" /></p>
<p>City: <input type="text" name="city" size="30" maxsize="30" /></p>
<p>State: <input type="text" name="state" size="2" maxsize="2" /></p>
<p>Zipcode: <input type="text" name="zip" size="5" maxsize="5" /></p>
<p>
<select name="schedule">
<option value="Full-time">Full-time</option>
</select>
</p>
<input type="submit" name="submit" value="Add Student" />
</form>
</body>
</html>
It doesn't look like it's the HTML code giving me problems. At least I cannot see it. I'm thinking it's in the PHP.