复选框错误未定义变量/索引错误php

时间:2016-07-05 17:59:57

标签: php

我学习者,创建简单的表单,所有其他输入工作正常但只面对复选框错误注意未定义的索引/变量,如果复选框ID未选中它在sql表中插入数据时显示错误,如果我们检查在sql中插入的任何复选框特定数据。只有未经检查的复选框错误,如果你解决了这个感谢

 <?php
    // put your code here
    // put your code here

    $Err[1] = $Err[2] = $Err[3] = $Err[4] = $Err[5] = $Err[6] = $Err[7] = "";
    $name = $gender = $number = $email = $website = $comment = $submit  =  "";

    if ($_SERVER["REQUEST_METHOD"] == "POST"){
        if(empty($_POST["name"])){
           $Err[1] = "Name is Required";
          }elseif(!preg_match("/^[a-zA-Z0-9]*$/",($_POST["name"])))
          { $Err[1] = "Only Letters and Numbers are allowed,";
                  }elseif(strlen($_POST["name"]) >= 30) {
                $Err[1] = "Maximum 30 letters allowed thank you";
                  }else{$name = test_input($_POST["name"]);} 




        if(empty($_POST["gender"])){
            $Err[2] = " Gender is required";
                }else{
                    $gender = test_input($_POST["gender"]);}

        if(empty($_POST["number"])){
            $Err[3] = "Phone Number is required";
                }elseif(!is_numeric($_POST['number'])){
                    (!filter_var($number,FILTER_VALIDATE_INT) == FALSE);
                    $Err[3] = "only numeric number are allowed";}
                         elseif(strlen($_POST['number']) >= 15){
                            $Err[3] = "Maximum 15 digits allowed thank you";}
                                else{$number = test_input($_POST["number"]);}



        if(empty($_POST["email"])){
            $Err[4] = "Email is Required";
              }else{
                 $email = ($_POST["email"]);
                    if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
                         $Err[4] = "Inavalid Email Format";
                }
        }
        if(empty($_POST["website"])){
            $Err[5] = " Website name required";
                }else{
                    $website = test_input($_POST["website"]);
                        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%"
                         . "?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
                               $Err[5] = "Invalid URL"; 
        }
        }

       if(isset($_POST['vech'])){
        foreach($_POST['vech'] as $vech){
           if($_POST['vech']==="car"){
              $car  = $vech;
           } 

           if($vech==="bike"){
               @$bike = $vech;
           }
            if($vech === "suv"){
                   $suv = $vech;
         }}} 

        if(isset($_POST["state"])){
            foreach ($_POST['state'] as $state){
                $state;
            }
        }


        if($_POST["date"]){
            $date = test_input($_POST['date']);
        }

        if(empty($_POST["comment"])){
            $Err[6] = "Please enter some input";
                }else{
                    $comment = test_input($_POST["comment"]);
        }


    }            
        function test_input($data){
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
        }



    ?>

    <form name="myform" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        Name:<input type="text" id="name" name="name" value="<?php echo $name;?>">
        <span class="error">* <?php echo $Err[1]?></span><br><br>

       phone <input type="text" name="number" value="<?php echo $number;?>">
       <span class="error">* <?php echo $Err[3];?></span><br><br>

       Website<input type="text" name="website" value="<?php echo $website ?>" >
       <span class="error">* <?php echo $Err[5];?></span><br><br>

       Gender<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male")
           echo "checked";?>value="male">Male
       <input  type="radio" name="gender" <?php if (isset($gender) && $gender=="female")
           echo "checked";?>  value="Female">female
       <span class="error">* <?php echo $Err[2];?></span><br><br>
       Your state:
       <select name="state[]">
           <option value="">select</option>
           <option value="DELHI">DELHI</option>
           <option value="CALCUTTA">CALCUTTA</option>
           <option value="MADRAS">madras</option>
           <option value="BOMBAY">BOMBAY</option>
       </select>
       <br><br>
       Do you Have Vehicle;<br>

       Car<input type="checkbox" name="vech[]" value="car" id="ca">

       Bike<input type="checkbox" name="vech[]" value="bike" id="bak">

       SUV<input type="checkbox" name="vech[]" value = "suv" id="su">

       <br>
       enter your date of birth<br>
       <input type="date" name="date"><br><br>


       email<input type="text" name="email">
       <span class="error">*<?php echo $Err[4]?></span><br><br>

       <textarea name="comment" cols="40" rows="15" style="resize: none"></textarea>
       <span class="error">*<?php echo $Err[6];?></span><br><br>

       <input type="submit" name="submit"  value="submit"><br><br>
       <input type="reset" name="reset" value="reset">


    </form>

    <?php


    if (isset($_POST['submit'])){
    $servername = "localhost";
    $username   =   "localhost";
    $password   = "friends";
    $dbname     = "friends";

    $conn = mysqli_connect($servername, $username, $password, $dbname);

    if(!$conn){
        die("connection error" . mysqli_connect_error());
    }

    if(
    $Err[1] == FALSE &&
    $Err[2] == FALSE &&
    $Err[3] == FALSE &&
    $Err[4] == FALSE &&
    $Err[5] == FALSE &&
    $Err[6] == FALSE )
        {
    $sql = "INSERT INTO myfriends(firstname,number,gender,date,car,bike,suv,state)
            VALUE('$_POST[name]','$_POST[number]','$_POST[gender]',"
            . "'$_POST[date]','$car','$bike','$suv','$state')";
   if(mysqli_query($conn, $sql)){
       echo  "data inserted sucessfully";
       echo '<script>';
       echo 'document.getElementById("name").value = ""';
       echo '</script>';

    }else{
           echo "error" . $sql . "<br>" . die (mysqli_error($conn));
   };
   }

   mysqli_close($conn);
   }

&GT;

0 个答案:

没有答案