无法通过php代码将多个图像上载到sql数据库

ymdaylpp  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(389)

最近,我尝试在数据库中插入一些包含文本和图像的记录。文本插入时没有问题,图像无法进入。我尝试插入的图像并不重。通常只有几kb。我的.php文件有没有出错?

<?php

// php code to Insert data into mysql database from input text
if(isset($_POST['insert'])){
    $hostname = "localhost";
    $username = "root";
    $password = "";
    $databaseName = "database1";
    // get values form input text and number
    $fname = $_POST['fname'];
      $contact1 = $_POST['contact1'];
      $contact2 = $_POST['contact2'];
     $filename = $_FILES['uploadfile']['name'];
  $filetmpname = $_FILES['uploadfile']['tmp_name'];
   $filename2 = $_FILES['uploadfile2']['name'];
  $filetmpname2 = $_FILES['uploadfile2']['tmp_name'];
  $folder = 'imagesuploadedf/';
   $folder = 'imagesuploadedf2/';
  move_uploaded_file($filetmpname, $folder.$filename);
  move_uploaded_file($filetmpname2, $folder2.$filename2);
    $connect = mysqli_connect($hostname, $username, $password, $databaseName);
    // mysql query to insert data
    $query = "INSERT INTO `tabela`(`name`, `imagename`, `imagename2`, `contact1`, `contact2`) VALUES ('$fname', '$filename', '$filename2', '$contact1', '$contact2')";    
    $result = mysqli_query($connect,$query);
    // check if mysql query successful
    if($result)
    {
        echo 'Data Inserted';
    }
    else{
        echo 'Data Not Inserted';
    }   
    mysqli_free_result($result);
    mysqli_close($connect);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title> PHP INSERT DATA </title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="index.php" method="post">
            <input type="text" name="fname"  placeholder="First Name"><br><br>
           <input type="file" name="uploadfile" ><br><br>
            <input type="file" name="uploadfile2" ><br><br>
                 <input type="text" name="contact1"  placeholder="Contact1"><br><br>
                    <input type="text" name="contact2"  placeholder="Contact2"><br><br>

            <input type="submit" name="insert" value="Add Data To Database">
        </form>
    </body>
</html>
oo7oh9g9

oo7oh9g91#

请添加 enctype="multipart/form-data" 在表单标签中
就像 <form action="index.php" method="post" enctype="multipart/form-data">

kcrjzv8t

kcrjzv8t2#

请试一试

<?php

 // php code to Insert data into mysql database from input text
if(isset($_POST['insert'])){
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "database1";
// get values form input text and number
  $fname = $_POST['fname'];
  $contact1 = $_POST['contact1'];
  $contact2 = $_POST['contact2'];
  $filename = $_FILES['uploadfile']['name'];
  $filetmpname = $_FILES['uploadfile']['tmp_name'];
  $filename2 = $_FILES['uploadfile2']['name'];
  $filetmpname2 = $_FILES['uploadfile2']['tmp_name'];
  $folder = 'imagesuploadedf/';
  $folder2 = 'imagesuploadedf2/';

 // edited and added below code. it will check if folder exists and create if not exists.....
 $foldername = 'imagesuploadedf';
 $foldername2 = 'imagesuploadedf2';
 if ( ! is_dir($foldername)) {
     mkdir($foldername);
 }
 if ( ! is_dir($foldername2)) {
    mkdir($foldername2);
  }
 // end of edited and added code

  move_uploaded_file($filetmpname, $folder.$filename);
  move_uploaded_file($filetmpname2, $folder2.$filename2);
  $connect = mysqli_connect($hostname, $username, $password, $databaseName);
  // mysql query to insert data
$query = "INSERT INTO `tabela`(`name`, `imagename`, `imagename2`, `contact1`, `contact2`) VALUES ('$fname', '$filename', '$filename2', '$contact1', '$contact2')";    
$result = mysqli_query($connect,$query);
// check if mysql query successful
 if($result)
 {
     echo 'Data Inserted';
 }
 else{
    echo 'Data Not Inserted';
 }   
 mysqli_free_result($result);
  mysqli_close($connect);
}
?>
<!DOCTYPE html>
 <html>
  <head>
     <title> PHP INSERT DATA </title>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
 <body>
    <form action="index.php" method="post" enctype="multipart/form-data">
        <input type="text" name="fname"  placeholder="First Name"><br><br>
       <input type="file" name="uploadfile" ><br><br>
        <input type="file" name="uploadfile2" ><br><br>
             <input type="text" name="contact1"  placeholder="Contact1"><br><br>
                <input type="text" name="contact2"  placeholder="Contact2"><br><br>

        <input type="submit" name="insert" value="Add Data To Database">
    </form>
</body>
</html>

是文件夹,即 imagesuploadedf 以及 imagesuploadedf2 创建?
在添加到数据库之前,还要小心并清理输入数据。

相关问题