0

我正在尝试将表单数据存储在 mysql 数据库中。我希望用户键入或单击的所有内容都存储在那里。我能够成功地将我的文本字段存储在 mysql 中。我无法弄清楚如何将多个复选框、单选按钮和下拉列表发送到 mysql 并将其存储在那里。我什至不知道从哪里开始。请帮忙。

也可以参考这个。

php 和 mysql - 将复选框、单选按钮和下拉菜单结果从用户发送到 mysql 数据库并将其存储在那里

    session_start();

    function logged_in() {
        return isset($_SESSION['user_id']);
    }

    function confirm_logged_in() {
        if (!logged_in()) {
            redirect_to("contact info.php");
        }
    }


    // Database Constants
define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'wer_application');


    function check_required_fields($required_array) {
    $field_errors = array();
    foreach($required_array as $fieldname) {
        if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) { 
            $field_errors[] = $fieldname; 
        }
    }
    return $field_errors;
}

function check_max_field_lengths($field_length_array) {
    $field_errors = array();
    foreach($field_length_array as $fieldname => $maxlength ) {
        if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $field_errors[] = $fieldname; }
    }
    return $field_errors;
}

function display_errors($error_array) {
    echo "<p class=\"errors\">";
    echo "Please review the following fields:<br />";
    foreach($error_array as $error) {
        echo " - " . $error . "<br />";
    }
    echo "</p>";
}

    // START FORM PROCESSING
    if (isset($_POST['submit'])) { // Form has been submitted.
        $errors = array();

        // perform validations on the form data
        $required_fields = array('id','student_gender', 'student_session_one_preference', 'student_session_two_preference',
        'waldron_scholarship', 'jesse_van_anterp_huyck_scholarship', 'edward_raney_scholarship', 'school_type', 
        'student_statemenet_consent_check_box', 'guardian_statemenet_consent_check_box', 'waiver');

        $errors = array_merge($errors, check_required_fields($required_fields, $_POST));

    $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

        //Captures what is typed by the user
        if(!empty($_POST) && isset($_POST["submit-btn"])){
        $student_gender = isset($_POST["student_gender"]) ? $_POST["student_gender"] : "";
        $student_session_one_preference = isset($_POST["student_session_one_preference"]) ? $_POST["student_session_one_preference"] : "";
        $student_session_two_preference = isset($_POST["student_session_two_preference"]) ? $_POST["student_session_two_preference"] : "";
        $waldron_scholarship = isset($_POST["waldron_scholarship"]) ? $_POST["waldron_scholarship"] : "";
        $jesse_van_anterp_huyck_scholarship = isset($_POST["jesse_van_anterp_huyck_scholarship"]) ? $_POST["jesse_van_anterp_huyck_scholarship"] : "";
        $edward_raney_scholarship = isset($_POST["edward_raney_scholarship"]) ? $_POST["edward_raney_scholarship"] : "";
        $school_type = isset($_POST["school_type"]) ? $_POST["school_type"] : "";
        $student_statemenet_consent_check_box = isset($_POST["student_statemenet_consent_check_box"]) ? $_POST["student_statemenet_consent_check_box"] : "";
        $guardian_statemenet_consent_check_box = isset($_POST["guardian_statemenet_consent_check_box"]) ? $_POST["guardian_statemenet_consent_check_box"] : "";
        $waiver = isset($_POST["waiver"]) ? $_POST["waiver"] : "";


    //sends the data from the form into our database we made with mysql.
        $sql = "INSERT INTO nature VALUES ('id','$student_gender', '$student_session_one_preference', '$student_session_two_preference',
        '$waldron_scholarship', '$jesse_van_anterp_huyck_scholarship', '$edward_raney_scholarship', '$school_type', 
        '$student_statemenet_consent_check_box', '$guardian_statemenet_consent_check_box', '$waiver')";


    if(!mysql_query($sql)){
            die('Error: ' . mysql_error());
        }
    }

    <html>
    <head>
        <title>WER Application Packet</title>
        <link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="header">
            <h1>WER Application Packet</h1>
        </div>
        <div id="main">

    <table id="structure">
    <tr>
        <td id="page">
            <?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
            <?php if (!empty($errors)) { display_errors($errors); } ?>

            <form action="contact info.php" method="post">

    <form method="POST">
    <input type="radio" name="student_gender" value="Male">Male<br>
    <input type="radio" name="student_gender" value="Female">Female<br>


    <select name="student_session_one_preference">
                        <option value="Session 1">Session 1: June 30 - July 21</option>
                        <option value="Session 2">Session 2: July 28 - August 18</option>
                    </select> <br/>
                    Preference 2:
                    <select name="student_session_two_preference">
                        <option value="Session 1">Session 1: June 30 - July 21</option>
                        <option value="Session 2">Session 2: July 28 - August 18</option>
                    </select>


    <input type="checkbox" name="waldron_scholarship" value="Waldron Scholarship">Waldron Scholarship<br/>
                        <input type="checkbox" name="jesse_van_anterp_huyck_scholarship" value="Jesse Van Anterp Huyck Scholarship">Jesse Van Anterp Huyck Scholarship<br/>
                        <input type="checkbox" name="edward_raney_scholarship" value="Edward Raney Scholarship">Edward Raney Scholarship<br/>


    <select name="school_type">
                        <option value="Public">Public</option>
                        <option value="Private">Private Parochial</option>
                        <option value="Other">Other</option>
                    </select>


    <input type="radio" name="waiver" value="">I waive any right of access that I may have to information submitted by my referee<br>
                        <input type="radio" name="waiver" value="">I do not waive any right of access that I may have to information submitted by my referee<br>


    <input type="checkbox" name="student_statement_consent_check_box" value="Agree">By checking this box, the student acknowledges <br/> that they have read the statement <br/> of consent and agrees with it.<br>


    <input type="checkbox" name="guardian_statement_consent_check_box" value="Agree">By checking this box, the guardian acknowledges <br/> that they have read the statement <br/> of consent and agrees with it.<br>

    <input type="submit" value="Submit" name="submit-btn">

    </form>

    </td>
    </tr>
</table>

            </div>
        <div id="footer"></div>
    </body>
</html>
<?php
    // 5. Close connection
    mysql_close($connection);
?>






    CREATE TABLE nature(
ID int NOT NULL auto_increment,
student_gender varchar(6),
student_session_one_preference varchar(30),
student_session_two_preference varchar(30),
waldron_scholarship tinyint,
jesse_van_anterp_huyck_scholarship tinyint,
edward_raney_scholarship tinyint,
school_type varchar(25),
student_statemenet_consent_check_box tinyint,
guardian_statemenet_consent_check_box tinyint,
waiver tinyint, 
PRIMARY KEY(ID)
);
4

2 回答 2

0

您的豁免单选输入都是缺失值,因此 $_POST['waiver'] 将始终为空。像这样修复:

<input type="radio" name="waiver" value="yes">
<input type="radio" name="waiver" value="no">

您的复选框字段可以像这样设置变量(只需检查是否设置):

$jesse_van_anterp_huyck_scholarship = isset($_POST["jesse_van_anterp_huyck_scholarship"]) ? 1 : 0;

仅供参考:您的 varchar(30) 字段可能不够长,无法存储诸如“Jesse Van Anterp Huyck Scholarship”之类的字符串

于 2013-04-02T04:43:17.337 回答
0

尝试使用以下 PHP 脚本来发现当您选择和省略某些表单元素时发送的内容(以及不发送的内容)。

<?php
    if (sizeOf($_POST) > 0) print_r($_POST);
?>

<form method="POST" action=".">
    <input type="checkbox" name="checkbox" id="checkbox" /> <label for="checkbox">Checkbox</label>
    <input type="radio" name="radio" value="1" id="radio_1" /> <label for="radio_1">Radio 1</label>
    <input type="radio" name="radio" value="2" id="radio_2" /> <label for="radio_2">Radio 2</label>
    <select name="select">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <input type="submit" />
</form>

您会看到,如果选中了一个复选框,则相应的表单 POST 值将设置为“on”。单选按钮将设置为其值,选择也是如此。

请注意,所有表单元素在发送到action页面时都由它们的name.

于 2013-04-02T04:43:36.040 回答