-2

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.

2 Answers2

0

I think in your PHP code you are missing one } please take a look :

 }else{
 echo "Schedule not received";
 exit;

it should be :

 }else{
 echo "Schedule not received";
 exit;
 }
AkshayP
  • 2,141
  • 2
  • 18
  • 27
0

Firstly, you have a whole bunch of missing closing } braces.

"I get no error messages" - That's because you're not checking for them.

Add error reporting to the top of your file(s) right after your opening PHP tag.

For example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, and you'll see the errors when running your present code.

I've added the missing closing braces for many of your else's.

Also this line $email = $_POST['gender']; that should be $gender = $_POST['gender'];, again; error reporting would have signaled another notice/warning for that.

  • $email isn't in your VALUES, $gender is.

Rewrite:

<?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());
}

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']){
$gender = $_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.


?>

Footnotes:

  • Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

  • Use an IDE when coding. There are many good ones out there, some of which are Freeware such as Notepad++.

  • You don't need this $db_selected = mysqli_select_db($dbc, $dbname); You've already declared it with $dbc = mysqli_connect($servername, $username, $password, $dbname);

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141