图像名称不会上传到数据库

时间:2017-01-31 06:03:36

标签: php

我不知道我的鳕鱼有什么问题。当我提交我的图片时,我没有收到任何错误,并且我获得了成功。当我检查我的数据库表以查看图像名称是否已被上传时,我只得到一个空白的表格行,只插入了ID,除此之外别无其他我也没有在文件夹中获取图像。



<?php 
require_once("configur.php");
	
$mysqli = new mysqli(localhost, root, password, user);

$query_image = 'INSERT INTO shirt_table (images1, images2, images3, images4) 
    values( "' . $_FILES['file1']['name'] . '",
            "' . $_FILES['file2']['name'] . '",
            "' . $_FILES['file3']['name'] . '",
            "' . $_FILES['file4']['name'] . '"
)';


if ($mysqli->query($query_image) === TRUE) {
	
  echo "<script language='javascript'>\n";
  echo "alert('Upload successful!')";
  echo "</script>\n";
} else {
    echo "Error updating record: " . $conn->error;
}

$mysqli->close();
    
?>
<?php

include("configur.php");
 if($_POST)
 { 
 // $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.

 

 if ($_FILES["file1"]["error"] > 0)
 {
 // if there is error in file uploading 
 echo "Return Code: " . $_FILES["file1"]["error"] . "<br />";

 

 }
 else
 {
 // check if file already exit in "images" folder.
 if (file_exists("shirtimgs/" . $_FILES["file1"]["name"]))
 {
 
 }
 else
 {  //move_uploaded_file function will upload your image. 
 if(move_uploaded_file($_FILES["file1"]["tmp_name"],"shirtimgs/" . $_FILES["file1"]["name"]))
 {
 // If file has uploaded successfully, store its name in data base
 $query_image = "insert into shirt_table";
  
 if(mysqli_query($link, $query_image))
 {
 echo "Stored in: " . "shirtimgs/" . $_FILES["file1"]["name"];
 }
 else
 {
 echo'';
 }
 }
 }

 


 }
 }


 ?><?php

include("configur.php");
 if($_POST)
 { 


 

 if ($_FILES["file2"]["error"] > 0)
 {
 // if there is error in file uploading 
 echo "Return Code: " . $_FILES["file2"]["error"] . "<br />";

 

 }
 else
 {
 // check if file already exit in "images" folder.
 if (file_exists("shirtimgs/" . $_FILES["file2"]["name"]))
 {
 
 }
 else
 {  //move_uploaded_file function will upload your image. 
 if(move_uploaded_file($_FILES["file2"]["tmp_name"],"shirtimgs/" . $_FILES["file2"]["name"]))
 {
 // If file has uploaded successfully, store its name in data base
 $query_image = "insert into shirt_table";
  
 if(mysqli_query($link, $query_image))
 {
 echo "Stored in: " . "shirtimgs/" . $_FILES["file2"]["name"];
 }
 else
 {
 echo'';
 }
 }
 }

 


 }
 }


 ?><?php

include("configur.php");
 if($_POST)
 { 
 // $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.

 

 if ($_FILES["file3"]["error"] > 0)
 {
 // if there is error in file uploading 
 echo "Return Code: " . $_FILES["file3"]["error"] . "<br />";

 

 }
 else
 {
 // check if file already exit in "images" folder.
 if (file_exists("shirtimgs/" . $_FILES["file3"]["name"]))
 {
 
 }
 else
 {  //move_uploaded_file function will upload your image. 
 if(move_uploaded_file($_FILES["file3"]["tmp_name"],"shirtimgs/" . $_FILES["file3"]["name"]))
 {
 // If file has uploaded successfully, store its name in data base
 $query_image = "insert into shirt_table";
  
 if(mysqli_query($link, $query_image))
 {
 echo "Stored in: " . "shirtimgs/" . $_FILES["file3"]["name"];
 }
 else
 {
 echo'';
 }
 }
 }

 


 }
 }


 ?><?php

include('configur.php');
 if($_POST)
 { 
 // $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.

 

 if ($_FILES["file4"]["error"] > 0)
 {
 // if there is error in file uploading 
 echo "Return Code: " . $_FILES["file4"]["error"] . "<br />";

 

 }
 else
 {
 // check if file already exit in "images" folder.
 if (file_exists("shirtimgs/" . $_FILES["file4"]["name"]))
 {
 
 }
 else
 {  //move_uploaded_file function will upload your image. 
 if(move_uploaded_file($_FILES["file4"]["tmp_name"],"shirtimgs/" . $_FILES["file4"]["name"]))
 {
 // If file has uploaded successfully, store its name in data base
 $query_image = "insert into shirt_table";
  
 if(mysqli_query($link, $query_image))
 {
 echo "Stored in: " . "shirtimgs/" . $_FILES["file4"]["name"];
 }
 else
 {
 echo'';
 }
 }
 }

 


 }
 }


 ?>
&#13;
&#13;
&#13;

&#13;
&#13;
<form id="myform" action='https://website.com/results' method="POST">
  
  <input type="file" name="file3" id="file3" required formvalidate>
  <input type="file" class="upload-img" name="file1" id="file1" onchange="readURL(this);" />
      <input type="file" class="upload-img2" name="file2" id="file2"  onchange="readURL(this);"  />
      <input type="file" class="upload-img3" name="file4" id="file4"   onchange="readURL(this);" />
  
  
  <input type="submit" name="submit" value="Submit" />
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

使用预准备语句插入数据库。就像这个..

$query_image = mysqli_prepare($connection,"INSERT INTO shirt_table (images1, images2, images3, images4) 
    values(?,?,?,?)");//$connection is database connection object

mysqli_stmt_bind_param($query_image,"ssss",$_FILES['file1']['name'], $_FILES['file2']['name'], $_FILES['file3']['name'], $_FILES['file4']['name']);
mysqli_stmt_execute($query_image );

有关详情,请参阅Prepared Statements

答案 1 :(得分:0)

<?php
include("configur.php");
if($_POST)
 // $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
{ 
    if ($_FILES["file1"]["error"] > 0)
    {
        // if there is error in file uploading 
        echo "Return Code: " . $_FILES["file1"]["error"] . "<br />";
    }
    else
    {
        // check if file already exit in "images" folder.
        if (file_exists("shirtimgs/" . $_FILES["file1"]["name"]))
        {

        }
        else
        {  //move_uploaded_file function will upload your image. 
            if(move_uploaded_file($_FILES["file1"]["tmp_name"],"shirtimgs/" . $_FILES["file1"]["name"]))
            {
                // If file has uploaded successfully, store its name in data base
               **$image_name = $_FILES["file1"]["name"];**
                $query_image = "insert into shirt_table **SET image_column_nam='$image_name'**";

                if(mysqli_query( $query_image))
                {
                   echo "Stored in: " . "shirtimgs/" . $_FILES["file1"]["name"];
                }
                else
                {
                    echo'';
                }
            }
        }
    }
}
?>

您还没有在插入查询中使用图片名称。