注意:php中的未定义索引甚至我定义索引正确

时间:2017-10-07 11:06:06

标签: php mysql html5

我已经创建了一个表单,用户填写并上传附带的文件,但每当我点击提交按钮时,我会在输入文件字段中获得注意:未定义的索引,而不是其他的

我的,代码似乎很好,但我不知道为什么我得到这个。我是个菜鸟,任何人都可以帮助我!

我的PHP代码,

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
include("config.php");
$name =$_POST["u_n"];
$pass =$_POST["u_p"];
$type =$_POST["u_t"];
$hobb =implode(',',$_POST["u_h"]);
$ctry =$_POST['u_c'];
$fav =implode(',',$_POST['u_f']);
    $file = $_FILES['res'];
    $file_name = $file['name'];
    $file_type = $file ['type'];
    $file_size = $file ['size'];
    $file_path = $file ['tmp_name'];
    move_uploaded_file ($file_path,'Uploads/'.$file_name);
    $img =$_FILES['img'];
    $img_count = count($img["name"]);
    $arr =[];
    for($i=0;$i<$img_count;$i++){
        $name= $img["name"][$i];
        $temp= $img["tmp_name"][$i];
        $type= $img["type"][$i];
        $size= $img["size"][$i];
        $error= $img["error"][$i];
        move_uploaded_file ($temp,'Uploads/'.$name);
        $arr[$i]= $name;
    }
$count= count($arr);
for($i=0; $i<4; $i++){
    if($i>$count-1){
        $arr[$i]='';
    }        
}
$query = "INSERT INTO users2 (U_name, U_pass, U_type, U_hob, U_img, img2, img3, img4, U_res, U_fav, U_ctry)
VALUES('$name', '$pass', '$type', '$hobb', '$arr[0]', '$arr[1]', '$arr[2]', '$arr[3]', '$file_name', '$fav', '$ctry')";
if(mysqli_query($db, $query)){
    echo "uploaded";
}
else{
    echo "not uploaded";
}?>

我的HTML代码,

 <!DOCTYPE html>
<html>
    <head>
        <title>Reg Form:</title>
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
    </head>
    <body>
    <div class='users_form'>
    <form action="../../panel/database_uploader3.php" method= "post">
        <b>Username:</b><br><input class="users_form_input" type="text" name="u_n" required><br><br>
        <b>Password:</b><br>
        <input class="users_form_input" type="password" name="u_p" required><br><br>
        <b>User type:</b><br>
        <input class="user_input_form" type="radio" name="u_t" value="Admin" required> admin<br>
        <input class="user_input_form" type="radio" name="u_t" value="Others" required> Other<br><br>
        <b>User status:</b><br><input class="user_input_form" type="radio" name ="status" value="Active">Active<br>
        <input class="user_input_form" type="radio" name="status" value="Not-Active" required>Not-Active<br>
        <b>Select Hobbies:</b><br>
        <input name='u_h[]' type='checkbox' value='Draw.'>Drawing<br>
        <input name='u_h[]' type='checkbox' value='Danc.'>Dancing<br>
        <input name='u_h[]' type='checkbox' value='Sng.'>Singing<br>
        <input name='u_h[]' type='checkbox' value='game.'>gamming<br><br>
        <b>Upload your images:</b><br>
         <input type="file" id="fileupload" name="img[]" accept="image/x-png,image/gif,image/jpeg" multiple/><br>
         <b>Upload Resume:</b><br>
         <input type="file" name="res" ><br>
         <b>Favourite TV Sereis:</b><br>
         <select  multiple required name="u_f[]">
          <option value="Robot" >Mr.Robot</option>
          <option value="BB" >Breaking Bad</option>
          <option value="GOT" >Game of Thrones</option>
          <option value="HOC" >House of Cards</option>
         </select><br><br>
         <b>your Country</b><br>
         <select name="u_c" required>
          <option value="India" name="u_c">India</option>
          <option value="England" name="u_c">England</option>
          <option value="France" name="u_c">France</option>
          <option value="Argentina" name="u_c">Argentina</option>
         </select><br><br>
        <input class='users_form_button' type= "submit" value="Create">
        </form>
</div>
    </body>
</html>

我不知道为什么我得到这个请帮助我!

1 个答案:

答案 0 :(得分:2)

在php中处理文件上传时,请添加enctype属性,如

<form enctype="multipart/form-data" action="../../panel/database_uploader3.php" method= "post">

您正在使用支持预准备语句的api。请用它