如何使用HTML,PHP和MYSQL将表单提交到MYSQL数据库?

时间:2015-04-22 02:45:39

标签: php mysql

我遇到以下代码问题...每次提交时,都没有收到错误消息,数据也没有提交到数据库。

PHP代码:

<?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代码:

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

它看起来不像HTML代码给我带来了问题。至少我看不到它。我在想它是在PHP中。

2 个答案:

答案 0 :(得分:1)

首先,你有一大堆缺少结束}大括号。

&#34;我没有收到任何错误消息&#34; - 那是因为您没有检查它们。

在打开PHP标记后立即将error reporting添加到文件顶部。

例如<?php error_reporting(E_ALL); ini_set('display_errors', 1);然后是代码的其余部分,您在运行现有代码时会看到错误。

我已经为else很多添加了缺少的结束括号。

这一行$email = $_POST['gender'];也应该是$gender = $_POST['gender'];;错误报告会发出另一个通知/警告信号。

  • $email不在你的价值观中,$gender是。

重写:

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


?>

脚注:

答案 1 :(得分:0)

我认为您的PHP代码中缺少一个} 请看一下:

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

它应该是:

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