简体   繁体   English

如何从两个不同的输入将两个不同的文件上传到数据库

[英]How to upload two different files to database from two diffrent inputs

I am trying to upload two images from two different inputs to database but only the last one does get uploaded and it's name get duplicated into the two different columns in database.我正在尝试将来自两个不同输入的两个图像上传到数据库,但只有最后一个图像被上传并且它的名称被复制到数据库中的两个不同列中。 I tried some other solutions but it didn't work and if there a solution using javascript I accept that just help me please我尝试了其他一些解决方案,但没有用,如果有使用 javascript 的解决方案,我接受,请帮助我

here is the code.这是代码。

<?php 
    session_start();
    
    if(!isset($_SESSION['id'],$_SESSION['user_role_id']))
    {
        header('location:index.php?lmsg=true');
        exit;
    }       
    require_once('layouts/left_sidebar1.php'); 
    require_once('inc/config.php');



    if(isset($_POST['add'])){
        $code = trim($_POST['code']);
        $code = mysqli_real_escape_string($conn, $code);

        $nconformetxt = trim($_POST['nconformetxt']);
        $nconformetxt = mysqli_real_escape_string($conn, $nconformetxt);

        if(isset($_FILES['nconformeimg']) && $_FILES['nconformeimg']['name'] != ""){
            $nconformeimg = $_FILES['nconformeimg']['name'];
            $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
            $uploadDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . "images/";
            $uploadDirectory .= $nconformeimg;
            $temp = explode(".", $_FILES["nconformeimg"]["name"]);
            $newfilename1 = round(microtime(true)) . '.' . end($temp);
            move_uploaded_file($_FILES["nconformeimg"]["tmp_name"], "images/{$newfilename1}");
        }
        
        $defaut = trim($_POST['defaut']);
        $defaut = mysqli_real_escape_string($conn, $defaut);

        $conformetxt = trim($_POST['conformetxt']);
        $conformetxt = mysqli_real_escape_string($conn, $conformetxt);

        if(isset($_FILES['conformeimg']) && $_FILES['conformeimg']['name'] != ""){
            $conformeimg = $_FILES['conformeimg']['name'];
            $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
            $uploadDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . "images/";
            $uploadDirectory .= $conformeimg;
            $temp = explode(".", $_FILES["conformeimg"]["name"]);
            $newfilename = round(microtime(true)) . '.' . end($temp);
            move_uploaded_file($_FILES["conformeimg"]["tmp_name"], "images/{$newfilename}");
        }
        
        $accepta = trim($_POST['accepta']);
        $accepta = mysqli_real_escape_string($conn, $accepta);  

        $pdreaction = trim($_POST['pdreaction']);
        $pdreaction = mysqli_real_escape_string($conn, $pdreaction);
        
        $id = '';

        $query = "INSERT INTO demo.defautheque   VALUES ('" . $id . "', '" . $code . "', '" . $nconformetxt . "', '" . $newfilename1 . "', '" . $defaut . "', '" . $conformetxt . "', '" . $newfilename . "', '" . $accepta . "', '" . $pdreaction . "')";
        $result = mysqli_query($conn, $query);
        if(!$result){
            echo "Can't add new data " . mysqli_error($conn);
            exit;
        } else {
            $extra="defautheque.php";
            echo "<script>window.location.href='".$extra."'</script>";
        }
    }

?>

<div class="form-popup" id="myForm" style="left: 450px;width: 500px;box-shadow:0 10px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19) !important;">
    <div class="card" style="width: 500px;box-shadow:0 10px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19) !important;">
            <div class="card-header">
                <h5 class="card-title">Nouveau Plan d'Action</h5>
            </div>
            <div style="width: 1000px;" class="card-body">
                <form method="post" action="defautheque.php" enctype="multipart/form-data"> 
                    <div class="row">
                        <div class="mb-3 col-md-2">
                            <label class="form-label" >Code</label>
                            <input type="text" name="code" class="form-control" placeholder="Code..">
                        </div>
                        <div class="mb-3 col-md-4">
                            <label class="form-label" >Défaut</label>
                            
                            <input type="text" name="defaut" class="form-control" placeholder="Projet..">
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="mb-3 col-md-6">
                            <label class="form-label" >Non Conforme</label>
                            <input type="text" name="nconformetxt" class="form-control" placeholder="Non Conforme..">
                            <input type="file" name="nconformeimg" class="form-control">
                        </div>
                    </div>
                    <div class="row">
                        <div class="mb-3 col-md-6">
                            <label class="form-label" >Conforme</label>
                            <input type="text" name="conformetxt" class="form-control" placeholder="Conforme..">
                            <input type="file" name="conformeimg" class="form-control">
                        </div>
                    </div>
                    <div class="row">
                        <div class="mb-3 col-md-3">
                            <label class="form-label" >Acceptation</label>
                            <input type="text" name="accepta" class="form-control" placeholder="Acceptatin..">
                        </div>
                        <div class="mb-3 col-md-3">
                            <label class="form-label" >Plan de réaction</label>
                            
                            <input type="text" name="pdreaction" class="form-control" placeholder="Plan de réaction..">
                        </div>
                    </div>
                    <div style="margin-left: 330px;">
                        <input type="submit" name="add" value="Ajouter" class="btn btn-primary">
                        <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
                    </div>
                </form>
            </div>
        </div>
</div>

I found the problem.我发现了问题。 and it is a very small one..when it generates the new name both files gets the same name.而且它很小..当它生成新名称时,两个文件都具有相同的名称。 I thought because it generates name depending on microseconds there will be no problem but I was wrong because both names get generated at the same time, so what I did:我想因为它根据微秒生成名称所以不会有问题但我错了因为两个名称是同时生成的,所以我做了什么:

I only added '1' before the dot and it worked.我只在点之前添加了“1”,它起作用了。

$temp = explode(".", $_FILES["nconformeimg"]["name"]); $temp = explode(".", $_FILES["nconformeimg"]["name"]); $newncomforme= round(microtime(true)). $newncomforme= round(microtime(true))。 '1.'. '1.'。 end($temp);结束($温度);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM