简体   繁体   中英

Image name will not upload to database

I do not know what is wrong with my cod. I don't get any errors when i submit my image and i get a success pop up. When i check my database table to see if the image name has been uploaded or not, I just get a blank table row with just the id inserted and nothing else other than that also i don't get an image in the folder.

 <?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''; } } } } } ?> 

 <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" /> 

Use prepared statement for inserting into database.Like this..

$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 );

For more see Prepared Statements

<?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'';
                }
            }
        }
    }
}
?>

You haven't use image name in insert query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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