表单验证不起作用

时间:2016-02-02 07:59:26

标签: php forms validation

我对我的表单有疑问。我想我犯了一个错误,它似乎没有起作用。当我选中一个复选框时,不仅会检查该特定复选框,还会检查所有复选框。

<?php
session_start();
function quote_smart($value)
    {
        if (get_magic_quotes_gpc()) {
            $value = stripslashes($value);
        }

        if(version_compare(phpversion(),"4.3.0") == "-1") {
            return mysqli_escape_string($value);
        } else {
            return mysqli_real_escape_string($value);
        }
    }

if(isset($_POST['submit'])){
    if(empty($_SESSION['captcha_code'] ) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha_code']) != 0) {  
        $msg="<span style='color:red'>The Validation code does not match!</span>";  
    } else {    
        $msg="<span style='color:green'>The Validation code has been matched.</span>";  
        $servername = "";
        $username = "";
        $password = "";
        $dbname = "";

        $conn = new mysqli($servername, $username, $password, $dbname);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        else {
            echo ("Connection good");
        }

        $fruit = array('berry', 'pear', 'apple', 'ananas', 'watermelon');
        $collection = array('berry', 'pear', 'apple', 'ananas', 'watermelon');
        if(isset($_POST['submit'])) {
            $fruit = $_POST['champions'];
            $values = array();

            foreach($collection as $selection ) {
                if($selection === $fruit) {
                    $values[ $selection ] = 1;
                }
                else {
                    $values[ $selection ] = 0;
                }
            }

            $sql = "INSERT INTO fruit (student_name, student_email, berry, pear, apple, ananas, watermelon) {$values['berry']}, {$values['pear']}, {$values['apple']}, {$values['ananas']}, {$values['watermelon']},
           '".$conn->real_escape_string($_POST["student_name"])."',
           '".$conn->real_escape_string($_POST["student_email"])."')";          

            if ($conn->query($sql) === TRUE) {
                echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            } 
        };
    }   
}   
?>

0 个答案:

没有答案