表单后数据未传递

时间:2019-09-27 12:25:09

标签: php html mysql

当我提交下面的表格时,我得到了一个未定义索引“商店”的错误,当我给“提交”按钮命名并测试它是否存在(isset)时,我的回答很不令人满意,这是我的代码:

<?php  
session_start();
$data['titre'] = 'Ajouter des médias';
$this->load->view('templates/header',$data);
$this->load->view('templates/navbar');
if(isset($_SESSION['username'])){
    global $con;
    $userid = getUserId($_SESSION['username']);
    if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['form_posted'])){
        if(isset($_POST['shopid'])) $shop = $_POST['shopid'];
        if($_POST['shopid'] == 0){
            echo '<div class="alert alert-danger"> Aucune boutique n\'a été choisie!</div>';
        }
        else
        {
            if($_FILES['video']['name']!= NULL || $_FILES['images']['name'][0] != NULL){
                if(isset($_FILES['images']) && $_FILES['images']['name'][0] != NULL){

                                $avatarAllowedExtensions = array("jpeg", "jpg", "png", "gif");
                                $name_array = $_FILES['images']['name'];
                                $tmp_name_array = $_FILES['images']['tmp_name'];
                                $type_array = $_FILES['images']['type'];
                                $size_array = $_FILES['images']['size'];
                                $error_array = $_FILES['images']['error'];

                                for($i = 0; $i < count($tmp_name_array) ; $i++){
                                        $exploded_img = explode('.', $name_array[$i]);

                                        $img_Extension = strtolower(end($exploded_img));
                                    if(in_array($img_Extension, $avatarAllowedExtensions)){
                                        $img_name = rand(1000000,10000000). '.' . $img_Extension;
                                        move_uploaded_file($tmp_name_array[$i], "uploads/shops/" . $img_name);
                                        $stmt = $con->prepare("INSERT INTO shop.shop_images (ID_boutique, pic) VALUES (?,?)");
                                        $stmt->execute(array($shop, $img_name));
                                        $insertid = $con->lastInsertId();

                                        $stmt = $con->prepare("INSERT INTO publications ( type, tableid, ID_boutique) VALUES (?,?,?)");
                                        $stmt->execute(array(2, $insertid, $shop));
                                        if($_FILES['video']['name']== NULL){
                                            echo '<div class="alert alert-success">Chargement réussi.</div>';
                                            header('refresh:1.5;url='.base_url("boutiques/store/".$shop."#media"));
                                        }
                                    }
                                }

                            }
                    if(isset($_FILES['video']) && $_FILES['video']['name']!= NULL){
                           $maxsize = 5242880*3; 

                           $name = $_FILES['video']['name'];
                           $target_dir = "uploads/videos/";
                           $videoFileType = strtolower(pathinfo($name,PATHINFO_EXTENSION));
                           $videoName = rand(1000000,10000000). '.' . $videoFileType;
                           $target_file = $target_dir . $videoName;

                           // Select file type


                           // Valid file extensions
                           $extensions_arr = array("mp4","avi","3gp","mov","mpeg");

                           // Check extension
                           if( in_array($videoFileType,$extensions_arr) ){

                              // Check file size
                              if(($_FILES['video']['size'] >= $maxsize) || ($_FILES["video"]["size"] == 0)) {
                                echo '<div class="alert alert-danger">Vidéo très large, 15MB maximum!</div>';
                              }else{
                                if(move_uploaded_file($_FILES['video']['tmp_name'],$target_file)){
                                  // Insert record
                                    global $con;    
                                  $stmt = $con->prepare("INSERT INTO shop_videos(ID_boutique,video) VALUES('".$shop."','".$videoName."')");
                                        $stmt->execute();
                                        $insertid = $con->lastInsertId();

                                        $stmt = $con->prepare("INSERT INTO publications ( type, tableid, ID_boutique) VALUES (?,?,?)");
                                        $stmt->execute(array(4, $insertid, $shop));
                                  echo '<div class="alert alert-success">Chargement réussi.</div>';
                                  header('refresh:1.5;url='.base_url("boutiques/store/".$shop."#media"));
                                }
                              }

                           }else{
                              echo '<div class="alert alert-danger">Extension invalide!</div>';
                           }

                         }
            } 
            else{
                echo '<div class="alert alert-danger">Aucun fichier selectionné!</div>';
            }
        }
    }
    else echo 'POST VARIABLE HASNT PASSED!';
?> 
    <div class="offset-md-2 col-md-8">
    <div class="container block">
        <div class="card bg-light mb-3">
            <div class="panel-header">Nouvelles photos</div>
            <div class="card-body">
                <form enctype="multipart/form-data" action="<?php echo current_url(); ?>" method="POST" class="form-horizontal">
                                <div class="form-group row">
                                    <label class="col-sm-3 control-label" for="file">Ajouter des photos:</label>
                                <input name="images[]" id="shop_pics" type="file" multiple>
                                </div>

                                <div class="gallery"></div>
                                <div class="form-group row">
                                    <label class="col-sm-3 control-label" for="file">Ajouter une vidéo:</label>
                                    <input name="video" id="video-upload" type="file">

                                </div>
                                <p class="offset-md-2" style="font-size: 12px; font-style: italic;">NB: La vidéo ne doit pas dépasser 15mb de volume.</p>
                                <div class="video-preview"></div>


                            <div class="form-group row">
                                <label class="col-sm-2 control-label">Assigner à une boutique:</label>
                                <div class="col-sm-8 col-md-6">
                                    <?php
                                    $stmt = $con->prepare("SELECT * from shop.boutiques where userID = ?");
                                    $stmt->execute(array($userid));
                                    $boutiques = $stmt->fetchALL();
                                    if(!empty($boutiques)){
                                        echo '<select name="shopid" class="shopselect" required="required">';   
                                        echo '<option>Choisir une boutique</option>';                   
                                        foreach (myShops() as $boutique) {
                                        echo '
                                        <option value='.$boutique['ID_boutique'].'>'.$boutique['nom'].'</option>';

                            }           echo '</select>';
                                    }


                                ?> 
                                </div>
                            </div>


                            <div class="form-group">
                                <div class="text-center">
                                    <input type="submit" name="form_posted" value="Ajouter" class="btn btn-primary">
                                    <input type="reset" value="Annuler" class="btn btn-danger">
                                </div>
                            </div>

                        </form> 
                    </div>
                </div>
            </div>
            </div>
<?php
}
else{
    echo '<p class="alert alert-primary"><a href="login.php">Connectez-vous ou créez un compte</a> rapidement pour pouvoir déposer des articles</p>';
}
$this->load->view('templates/footer');

?>

PS:只有在加载视频时才能发生,上传照片时效果很好,欢迎任何帮助

1 个答案:

答案 0 :(得分:0)

大约在代码中间,

// Valid file extensions
$extensions_arr = array("mp4","avi","3gp","mov","mpeg");

// Check extension
if (in_array($videoFileType, $extensions_arr)) {
    if (($_FILES['video']['size'] >= $maxsize) || ($_FILES["video"]["size"] == 0)) {
        // do stuff

因此,您的代码正在检查您上传的文件是否在$extensions_arr中具有任何扩展名(.mp4,.avi,.3gp,.mov,.mpeg)。

上传文件后,它会检查文件大小,并确保文件大小不大于0也不大于最大文件大小。顺便说一句,为了使其更具可读性,为什么不只使用

if (in_array($videoFileType, $extensions_arr)) {
    if (($_FILES['video']['size'] <= $maxsize) || ($_FILES["video"]["size"] > 0)) {
        // do stuff

并检查文件大小是否小于最大大小但大于0?

这里的问题是您没有在代码中的任何地方声明$shop,也许您是说$target_file