Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code and I want to insert the multiple image like this
the image is not inserting on sql
| id | img1 | img2 | img3 |




<?php
$conn = mysqli_connect("localhost", "", "", "");
if (isset($_POST['submit'])) {
         $img[$i] = array();            
       $uploads_dir = "images/";
       $i=1;
       foreach ($_FILES["image"]["error"] as $key => $error)
        {
         $name[$i] == "";
         if  ($error == UPLOAD_ERR_OK) {
              $tmp_name = $_FILES["image"]["tmp_name"][$key];
              $img[$i] = $_FILES["image"]["name"][$key];
              move_uploaded_file($tmp_name, "$uploads_dir/$img[$i]");
            }
         $i++;
        }
    $sql = "INSERT INTO users(img1,img2,img3) VALUES('$img1','$img2','$img3')";
  }
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function addmore(num)
{
    if(num==1)
    {
        document.getElementById('field2').style.display='block';
        document.getElementById('ni1').style.display='block';
        return false;
    }
    else if(num==2)
    {
        document.getElementById('field3').style.display='block';
        return false;
    }

}
</script>
</head>

<body>
<form enctype="multipart/form-data" name="" action="" method="post">
    <div id="field1">Enter One Image :<input type="file" name="image[]" id="img1"/><a href="#" onclick="addmore(1)" id="ni1">addmore...</a></div>
    <div id="field2"  style="display:none;">Enter Two Image :<input type="file" name="image[]" id="img2"/><a href="#" onclick="addmore(2);">add more...</a></div>
    <div id="field3"  style="display:none;">Enter Three Image :<input type="file" name="image[]" id="img3"/><a href="#" onclick="addmore(3)" id="ni3">addmore...</a></div>
    <div id="field4" style="display:none">Enter Forth Image :<input type="file" name="image[]" id="img4"/><a href="#" onclick="addmore(4)" id="ni4">addmore...</a></div>

    <input type="submit" name="submit"/>

</form>

</body>
</html>


What I have tried:

I tried the code but not working and not inserting on sql
Posted
Comments
Richard MacCutchan 28-Mar-20 10:01am    
You have created an SQL Insert statement but you have not executed it. Also you are using three variable names that have not been defined anywhere.
renmais 28-Mar-20 10:04am    
Okay! thats why I asked a question! I will not ask a question if I know the answer!
thats why Im asking for help! I hope you got it! Thats why people ask question because they want to know whats wrong!
Richard MacCutchan 28-Mar-20 10:08am    
And I have just explained what you have missed out. So you need to use the correct verb to make the INSERT actually happen. I am having to make some guesses here. I have never seen this code before and rely on you to explain what it is supposed to do. So at the moment all I can work on is what I observe in your question.
renmais 28-Mar-20 10:11am    
I am a newbie in code mate! :( 15 years old kid! my school project
Richard MacCutchan 28-Mar-20 10:16am    
Well at a guess that code will never work. You are processing three files in a loop, using an array to get their data. At the end of the loop you create an SQL statement that really does not look correct. You should insert each file inside the foreach loop as you copy it to your local disk.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900