我想插入3个图片到一个文件夹和路径到mysql的新名称

pepwfjgg  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(378)

我想插入3个图片到一个文件夹和路径到mysql的新名称。
我的表名是'ict',数据库是'ctg'。表字段是id、image1、image2和image3。
现在我想使用一个包含三个input type=“file”和一个submit按钮的表单。
php怎么可能呢?
谢谢。

a8jjtwal

a8jjtwal1#


# Below Database.php

<?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "ctg";

    // Create connection
    connecttodb($servername,$dbname,$username,$password);
    function connecttodb($servername,$dbname,$dbuser,$dbpassword)
    {
        global $link;

        $link=mysql_connect ("$servername","$dbuser","$dbpassword");

        if(!$link){
                die("Could not connect to MySQL");
        }

        mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error()); 
    }
    ?>

# Below File.php

    <?php
//error_reporting(0);

include("database.php");

 if(isset($_POST['upload']))
{
    /* Concept Image */

    $aMyUploads = array();
    foreach ($_FILES as $key => $aFile) {
        for($i = 0; $i<count($aFile['error']); $i++){
            //echo $aFile['error'][$i]; exit;
            if(0 === $aFile['error'][$i]){
                    $uploadLocation = 'uploads/'.$aFile['name'][$i];
            }   
            if(0 === $aFile['error'][$i] && (false !== move_uploaded_file($aFile['tmp_name'][$i], $uploadLocation))){
                $aMyUploads[] = $uploadLocation;
            } else {
                $aMyUploads[] = '';
            }
        }   
    }

    if(is_array($aMyUploads)){
        $image1 = $aMyUploads[0]; 
        $image2 = $aMyUploads[1];
        $image3 = $aMyUploads[2];
        $qry = mysql_query("INSERT INTO `ict` (`image1`, `image2`, `image3`) VALUES ('$image1', '$image2', '$image3')");
        //$ex =mysql_query($qry,$con);
    }else{
        echo "ERROR :: Not insert Please try";
    }
}
?>
<html>
 <form method="post" enctype="multipart/form-data" class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <div class="form-group">
      <label class="control-label col-sm-4">IMAGE1</label>
      <div class="col-sm-10 col-md-offset-0 col-md-4">
      <input type="file" class="form-control" name="upload_files[]">
      </div> 
    </div>
    <div class="form-group">
      <label class="control-label col-sm-4">IMAGE2</label>
      <div class="col-sm-10 col-md-offset-0 col-md-4">
      <input type="file" class="form-control" name="upload_files[]">
      </div> 
    </div>
    <div class="form-group">
      <label class="control-label col-sm-4">IMAGE3</label>
      <div class="col-sm-10 col-md-offset-0 col-md-4">
      <input type="file" class="form-control" name="upload_files[]">
      </div> 
    </div>
    <div class="col-sm-10 col-md-7 col-md-offset-4">
      <button type="submit" name="upload">Upload</button>
    </div> 
    </form>
</html>

相关问题