How to upload images into MySQL database using PHP code











up vote
20
down vote

favorite
12












I am trying to save images in my database from HTML form. I have written PHP code to accomplish this task. The program is not generating any error message, but also not inserting image data in MySQL database. Kindly check it.
Here i am sharing a excerpt from my code.



        /*-------------------
IMAGE QUERY
---------------*/


$file =$_FILES['image']['tmp_name'];
if(!isset($file))
{
echo 'Please select an Image';
}
else
{
$image_check = getimagesize($_FILES['image']['tmp_name']);
if($image_check==false)
{
echo 'Not a Valid Image';
}
else
{
$image = file_get_contents ($_FILES['image']['tmp_name']);
$image_name = $_FILES['image']['name'];
if ($image_query = mysql_query ("insert into product_images values (1,'$image_name',$image )"))
{
echo $current_id;
//echo 'Successfull';
}
else
{
echo mysql_error();
}
}
}
/*-----------------
IMAGE QUERY END
---------------------*/

<form action='insert_product.php' method='POST' enctype='multipart/form-data' ></br>
File : <input type='file' name= 'image' >
</form>



Error Message
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1











share|improve this question




















  • 7




    You can store in your database base64 of the image, or it's path on server.
    – GoldenTabby
    Jul 18 '13 at 7:46










  • What is base64?
    – Taha Kirmani
    Jul 18 '13 at 8:42










  • Don't save images into db, unless it's a core part of your application.
    – moonwave99
    Jul 18 '13 at 8:52















up vote
20
down vote

favorite
12












I am trying to save images in my database from HTML form. I have written PHP code to accomplish this task. The program is not generating any error message, but also not inserting image data in MySQL database. Kindly check it.
Here i am sharing a excerpt from my code.



        /*-------------------
IMAGE QUERY
---------------*/


$file =$_FILES['image']['tmp_name'];
if(!isset($file))
{
echo 'Please select an Image';
}
else
{
$image_check = getimagesize($_FILES['image']['tmp_name']);
if($image_check==false)
{
echo 'Not a Valid Image';
}
else
{
$image = file_get_contents ($_FILES['image']['tmp_name']);
$image_name = $_FILES['image']['name'];
if ($image_query = mysql_query ("insert into product_images values (1,'$image_name',$image )"))
{
echo $current_id;
//echo 'Successfull';
}
else
{
echo mysql_error();
}
}
}
/*-----------------
IMAGE QUERY END
---------------------*/

<form action='insert_product.php' method='POST' enctype='multipart/form-data' ></br>
File : <input type='file' name= 'image' >
</form>



Error Message
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1











share|improve this question




















  • 7




    You can store in your database base64 of the image, or it's path on server.
    – GoldenTabby
    Jul 18 '13 at 7:46










  • What is base64?
    – Taha Kirmani
    Jul 18 '13 at 8:42










  • Don't save images into db, unless it's a core part of your application.
    – moonwave99
    Jul 18 '13 at 8:52













up vote
20
down vote

favorite
12









up vote
20
down vote

favorite
12






12





I am trying to save images in my database from HTML form. I have written PHP code to accomplish this task. The program is not generating any error message, but also not inserting image data in MySQL database. Kindly check it.
Here i am sharing a excerpt from my code.



        /*-------------------
IMAGE QUERY
---------------*/


$file =$_FILES['image']['tmp_name'];
if(!isset($file))
{
echo 'Please select an Image';
}
else
{
$image_check = getimagesize($_FILES['image']['tmp_name']);
if($image_check==false)
{
echo 'Not a Valid Image';
}
else
{
$image = file_get_contents ($_FILES['image']['tmp_name']);
$image_name = $_FILES['image']['name'];
if ($image_query = mysql_query ("insert into product_images values (1,'$image_name',$image )"))
{
echo $current_id;
//echo 'Successfull';
}
else
{
echo mysql_error();
}
}
}
/*-----------------
IMAGE QUERY END
---------------------*/

<form action='insert_product.php' method='POST' enctype='multipart/form-data' ></br>
File : <input type='file' name= 'image' >
</form>



Error Message
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1











share|improve this question















I am trying to save images in my database from HTML form. I have written PHP code to accomplish this task. The program is not generating any error message, but also not inserting image data in MySQL database. Kindly check it.
Here i am sharing a excerpt from my code.



        /*-------------------
IMAGE QUERY
---------------*/


$file =$_FILES['image']['tmp_name'];
if(!isset($file))
{
echo 'Please select an Image';
}
else
{
$image_check = getimagesize($_FILES['image']['tmp_name']);
if($image_check==false)
{
echo 'Not a Valid Image';
}
else
{
$image = file_get_contents ($_FILES['image']['tmp_name']);
$image_name = $_FILES['image']['name'];
if ($image_query = mysql_query ("insert into product_images values (1,'$image_name',$image )"))
{
echo $current_id;
//echo 'Successfull';
}
else
{
echo mysql_error();
}
}
}
/*-----------------
IMAGE QUERY END
---------------------*/

<form action='insert_product.php' method='POST' enctype='multipart/form-data' ></br>
File : <input type='file' name= 'image' >
</form>



Error Message
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1








php html mysql database image-uploading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 '14 at 6:49









NoobEditor

11.1k84878




11.1k84878










asked Jul 18 '13 at 7:42









Taha Kirmani

64761645




64761645








  • 7




    You can store in your database base64 of the image, or it's path on server.
    – GoldenTabby
    Jul 18 '13 at 7:46










  • What is base64?
    – Taha Kirmani
    Jul 18 '13 at 8:42










  • Don't save images into db, unless it's a core part of your application.
    – moonwave99
    Jul 18 '13 at 8:52














  • 7




    You can store in your database base64 of the image, or it's path on server.
    – GoldenTabby
    Jul 18 '13 at 7:46










  • What is base64?
    – Taha Kirmani
    Jul 18 '13 at 8:42










  • Don't save images into db, unless it's a core part of your application.
    – moonwave99
    Jul 18 '13 at 8:52








7




7




You can store in your database base64 of the image, or it's path on server.
– GoldenTabby
Jul 18 '13 at 7:46




You can store in your database base64 of the image, or it's path on server.
– GoldenTabby
Jul 18 '13 at 7:46












What is base64?
– Taha Kirmani
Jul 18 '13 at 8:42




What is base64?
– Taha Kirmani
Jul 18 '13 at 8:42












Don't save images into db, unless it's a core part of your application.
– moonwave99
Jul 18 '13 at 8:52




Don't save images into db, unless it's a core part of your application.
– moonwave99
Jul 18 '13 at 8:52












4 Answers
4






active

oldest

votes

















up vote
38
down vote



accepted










Firstly, you should check if your image column is BLOB type!



I don't know anything about your SQL table, but if I'll try to make my own as an example.



We got fields id (int), image (blob) and image_name (varchar(64)).



So the code should look like this (assume ID is always '1' and let's use this mysql_query):



$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
if (!mysql_query($sql)) { // Error handling
echo "Something went wrong! :(";
}


You are doing it wrong in many ways. Don't use mysql functions - they are deprecated! Use PDO or MySQLi. You should also think about storing files locations on disk. Using MySQL for storing images is thought to be Bad Idea™. Handling SQL table with big data like images can be problematic.



Also your HTML form is out of standards. It should look like this:



<form action="insert_product.php" method="POST" enctype="multipart/form-data">
<label>File: </label><input type="file" name="image" />
<input type="submit" />
</form>




Sidenote:



When dealing with files and storing them as a BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error.






share|improve this answer



















  • 1




    its not working.Undefined variable: _FILE and file_get_contents(): Filename cannot be empty is showing
    – SANDEEP
    May 6 '14 at 10:12










  • @SANDEEP: I fixed that.
    – Wiktor Mociun
    May 6 '14 at 10:38






  • 1




    To be safe from SQL Injection I would use mysqli_real_escape_string or equivalent instead of addslashes. Best way to do this would use PDO prepared statements.
    – MTJ
    Nov 26 '14 at 7:28












  • You are right. This is just an example code.
    – Wiktor Mociun
    Nov 26 '14 at 13:47










  • I came about this Q&A from another question asked today. When dealing with files and storing as BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error. This not just "to be safe", it's because it must be done.
    – Funk Forty Niner
    May 17 '15 at 23:50




















up vote
18
down vote













Just few more details




  • Add mysql field



`image` blob




  • Get data from image



$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));




  • Insert image data into db



$sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";




  • Show image to the web



<img src="data:image/png;base64,'.base64_encode($row['image']).'">




  • End






share|improve this answer



















  • 1




    it should be $_FILES *
    – Tilak Maddy
    May 10 '17 at 12:07


















up vote
-1
down vote













This is the perfect code for uploading and displaying image through MySQL database.



<html>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="submit" value="Upload"/>
</form>
<?php
if(isset($_POST['submit']))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
{
echo " error ";
}
else
{
$image = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($image));
saveimage($image);
}
}
function saveimage($image)
{
$dbcon=mysqli_connect('localhost','root','','dbname');
$qry="insert into tablename (name) values ('$image')";
$result=mysqli_query($dbcon,$qry);
if($result)
{
echo " <br/>Image uploaded.";
header('location:urlofpage.php');
}
else
{
echo " error ";
}
}
?>
</body>
</html>





share|improve this answer






























    up vote
    -2
    down vote













    How to INSERT INTO DB?



    <html>
    <head>
    <title>Uploads</title>
    </head>
    <body>
    <input type="file" name="file_array"/>
    <input type="file" name="file_array"/>
    <input type="file" name="file_array"/>
    <input type="submit" name="submit"/>
    </body>
    </html>

    <?php
    if(isset($_FILES['file_array']))
    {
    $name_array = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array = $_FILES['file_array']['type'];
    $size_array = $_FILES['file_array']['size'];
    $error_array = $_FILES['file_array']['error'];
    $dir = "slideshow";

    for($i = 0; $i<count($tmp_name_array); $i++)
    {
    if(move_uploaded_file($tmp_name_array,"slideshow/".$name_array))
    {
    echo $name_array[$i]."Upload is complete<br>";

    } else {
    echo"Move_uploaded_file function failed for".$name_array[$i]."<br>";
    }
    }
    }
    ?>





    share|improve this answer






















      protected by Community May 17 '15 at 21:40



      Thank you for your interest in this question.
      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



      Would you like to answer one of these unanswered questions instead?














      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      38
      down vote



      accepted










      Firstly, you should check if your image column is BLOB type!



      I don't know anything about your SQL table, but if I'll try to make my own as an example.



      We got fields id (int), image (blob) and image_name (varchar(64)).



      So the code should look like this (assume ID is always '1' and let's use this mysql_query):



      $image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
      $image_name = addslashes($_FILES['image']['name']);
      $sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
      if (!mysql_query($sql)) { // Error handling
      echo "Something went wrong! :(";
      }


      You are doing it wrong in many ways. Don't use mysql functions - they are deprecated! Use PDO or MySQLi. You should also think about storing files locations on disk. Using MySQL for storing images is thought to be Bad Idea™. Handling SQL table with big data like images can be problematic.



      Also your HTML form is out of standards. It should look like this:



      <form action="insert_product.php" method="POST" enctype="multipart/form-data">
      <label>File: </label><input type="file" name="image" />
      <input type="submit" />
      </form>




      Sidenote:



      When dealing with files and storing them as a BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error.






      share|improve this answer



















      • 1




        its not working.Undefined variable: _FILE and file_get_contents(): Filename cannot be empty is showing
        – SANDEEP
        May 6 '14 at 10:12










      • @SANDEEP: I fixed that.
        – Wiktor Mociun
        May 6 '14 at 10:38






      • 1




        To be safe from SQL Injection I would use mysqli_real_escape_string or equivalent instead of addslashes. Best way to do this would use PDO prepared statements.
        – MTJ
        Nov 26 '14 at 7:28












      • You are right. This is just an example code.
        – Wiktor Mociun
        Nov 26 '14 at 13:47










      • I came about this Q&A from another question asked today. When dealing with files and storing as BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error. This not just "to be safe", it's because it must be done.
        – Funk Forty Niner
        May 17 '15 at 23:50

















      up vote
      38
      down vote



      accepted










      Firstly, you should check if your image column is BLOB type!



      I don't know anything about your SQL table, but if I'll try to make my own as an example.



      We got fields id (int), image (blob) and image_name (varchar(64)).



      So the code should look like this (assume ID is always '1' and let's use this mysql_query):



      $image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
      $image_name = addslashes($_FILES['image']['name']);
      $sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
      if (!mysql_query($sql)) { // Error handling
      echo "Something went wrong! :(";
      }


      You are doing it wrong in many ways. Don't use mysql functions - they are deprecated! Use PDO or MySQLi. You should also think about storing files locations on disk. Using MySQL for storing images is thought to be Bad Idea™. Handling SQL table with big data like images can be problematic.



      Also your HTML form is out of standards. It should look like this:



      <form action="insert_product.php" method="POST" enctype="multipart/form-data">
      <label>File: </label><input type="file" name="image" />
      <input type="submit" />
      </form>




      Sidenote:



      When dealing with files and storing them as a BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error.






      share|improve this answer



















      • 1




        its not working.Undefined variable: _FILE and file_get_contents(): Filename cannot be empty is showing
        – SANDEEP
        May 6 '14 at 10:12










      • @SANDEEP: I fixed that.
        – Wiktor Mociun
        May 6 '14 at 10:38






      • 1




        To be safe from SQL Injection I would use mysqli_real_escape_string or equivalent instead of addslashes. Best way to do this would use PDO prepared statements.
        – MTJ
        Nov 26 '14 at 7:28












      • You are right. This is just an example code.
        – Wiktor Mociun
        Nov 26 '14 at 13:47










      • I came about this Q&A from another question asked today. When dealing with files and storing as BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error. This not just "to be safe", it's because it must be done.
        – Funk Forty Niner
        May 17 '15 at 23:50















      up vote
      38
      down vote



      accepted







      up vote
      38
      down vote



      accepted






      Firstly, you should check if your image column is BLOB type!



      I don't know anything about your SQL table, but if I'll try to make my own as an example.



      We got fields id (int), image (blob) and image_name (varchar(64)).



      So the code should look like this (assume ID is always '1' and let's use this mysql_query):



      $image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
      $image_name = addslashes($_FILES['image']['name']);
      $sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
      if (!mysql_query($sql)) { // Error handling
      echo "Something went wrong! :(";
      }


      You are doing it wrong in many ways. Don't use mysql functions - they are deprecated! Use PDO or MySQLi. You should also think about storing files locations on disk. Using MySQL for storing images is thought to be Bad Idea™. Handling SQL table with big data like images can be problematic.



      Also your HTML form is out of standards. It should look like this:



      <form action="insert_product.php" method="POST" enctype="multipart/form-data">
      <label>File: </label><input type="file" name="image" />
      <input type="submit" />
      </form>




      Sidenote:



      When dealing with files and storing them as a BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error.






      share|improve this answer














      Firstly, you should check if your image column is BLOB type!



      I don't know anything about your SQL table, but if I'll try to make my own as an example.



      We got fields id (int), image (blob) and image_name (varchar(64)).



      So the code should look like this (assume ID is always '1' and let's use this mysql_query):



      $image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
      $image_name = addslashes($_FILES['image']['name']);
      $sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
      if (!mysql_query($sql)) { // Error handling
      echo "Something went wrong! :(";
      }


      You are doing it wrong in many ways. Don't use mysql functions - they are deprecated! Use PDO or MySQLi. You should also think about storing files locations on disk. Using MySQL for storing images is thought to be Bad Idea™. Handling SQL table with big data like images can be problematic.



      Also your HTML form is out of standards. It should look like this:



      <form action="insert_product.php" method="POST" enctype="multipart/form-data">
      <label>File: </label><input type="file" name="image" />
      <input type="submit" />
      </form>




      Sidenote:



      When dealing with files and storing them as a BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 18 '15 at 12:08









      Funk Forty Niner

      80.4k124799




      80.4k124799










      answered Jul 18 '13 at 8:50









      Wiktor Mociun

      577411




      577411








      • 1




        its not working.Undefined variable: _FILE and file_get_contents(): Filename cannot be empty is showing
        – SANDEEP
        May 6 '14 at 10:12










      • @SANDEEP: I fixed that.
        – Wiktor Mociun
        May 6 '14 at 10:38






      • 1




        To be safe from SQL Injection I would use mysqli_real_escape_string or equivalent instead of addslashes. Best way to do this would use PDO prepared statements.
        – MTJ
        Nov 26 '14 at 7:28












      • You are right. This is just an example code.
        – Wiktor Mociun
        Nov 26 '14 at 13:47










      • I came about this Q&A from another question asked today. When dealing with files and storing as BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error. This not just "to be safe", it's because it must be done.
        – Funk Forty Niner
        May 17 '15 at 23:50
















      • 1




        its not working.Undefined variable: _FILE and file_get_contents(): Filename cannot be empty is showing
        – SANDEEP
        May 6 '14 at 10:12










      • @SANDEEP: I fixed that.
        – Wiktor Mociun
        May 6 '14 at 10:38






      • 1




        To be safe from SQL Injection I would use mysqli_real_escape_string or equivalent instead of addslashes. Best way to do this would use PDO prepared statements.
        – MTJ
        Nov 26 '14 at 7:28












      • You are right. This is just an example code.
        – Wiktor Mociun
        Nov 26 '14 at 13:47










      • I came about this Q&A from another question asked today. When dealing with files and storing as BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error. This not just "to be safe", it's because it must be done.
        – Funk Forty Niner
        May 17 '15 at 23:50










      1




      1




      its not working.Undefined variable: _FILE and file_get_contents(): Filename cannot be empty is showing
      – SANDEEP
      May 6 '14 at 10:12




      its not working.Undefined variable: _FILE and file_get_contents(): Filename cannot be empty is showing
      – SANDEEP
      May 6 '14 at 10:12












      @SANDEEP: I fixed that.
      – Wiktor Mociun
      May 6 '14 at 10:38




      @SANDEEP: I fixed that.
      – Wiktor Mociun
      May 6 '14 at 10:38




      1




      1




      To be safe from SQL Injection I would use mysqli_real_escape_string or equivalent instead of addslashes. Best way to do this would use PDO prepared statements.
      – MTJ
      Nov 26 '14 at 7:28






      To be safe from SQL Injection I would use mysqli_real_escape_string or equivalent instead of addslashes. Best way to do this would use PDO prepared statements.
      – MTJ
      Nov 26 '14 at 7:28














      You are right. This is just an example code.
      – Wiktor Mociun
      Nov 26 '14 at 13:47




      You are right. This is just an example code.
      – Wiktor Mociun
      Nov 26 '14 at 13:47












      I came about this Q&A from another question asked today. When dealing with files and storing as BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error. This not just "to be safe", it's because it must be done.
      – Funk Forty Niner
      May 17 '15 at 23:50






      I came about this Q&A from another question asked today. When dealing with files and storing as BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error. This not just "to be safe", it's because it must be done.
      – Funk Forty Niner
      May 17 '15 at 23:50














      up vote
      18
      down vote













      Just few more details




      • Add mysql field



      `image` blob




      • Get data from image



      $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));




      • Insert image data into db



      $sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";




      • Show image to the web



      <img src="data:image/png;base64,'.base64_encode($row['image']).'">




      • End






      share|improve this answer



















      • 1




        it should be $_FILES *
        – Tilak Maddy
        May 10 '17 at 12:07















      up vote
      18
      down vote













      Just few more details




      • Add mysql field



      `image` blob




      • Get data from image



      $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));




      • Insert image data into db



      $sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";




      • Show image to the web



      <img src="data:image/png;base64,'.base64_encode($row['image']).'">




      • End






      share|improve this answer



















      • 1




        it should be $_FILES *
        – Tilak Maddy
        May 10 '17 at 12:07













      up vote
      18
      down vote










      up vote
      18
      down vote









      Just few more details




      • Add mysql field



      `image` blob




      • Get data from image



      $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));




      • Insert image data into db



      $sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";




      • Show image to the web



      <img src="data:image/png;base64,'.base64_encode($row['image']).'">




      • End






      share|improve this answer














      Just few more details




      • Add mysql field



      `image` blob




      • Get data from image



      $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));




      • Insert image data into db



      $sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";




      • Show image to the web



      <img src="data:image/png;base64,'.base64_encode($row['image']).'">




      • End







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 25 '17 at 6:32

























      answered Feb 12 '14 at 15:25









      Sean

      8691012




      8691012








      • 1




        it should be $_FILES *
        – Tilak Maddy
        May 10 '17 at 12:07














      • 1




        it should be $_FILES *
        – Tilak Maddy
        May 10 '17 at 12:07








      1




      1




      it should be $_FILES *
      – Tilak Maddy
      May 10 '17 at 12:07




      it should be $_FILES *
      – Tilak Maddy
      May 10 '17 at 12:07










      up vote
      -1
      down vote













      This is the perfect code for uploading and displaying image through MySQL database.



      <html>
      <body>
      <form method="post" enctype="multipart/form-data">
      <input type="file" name="image"/>
      <input type="submit" name="submit" value="Upload"/>
      </form>
      <?php
      if(isset($_POST['submit']))
      {
      if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
      {
      echo " error ";
      }
      else
      {
      $image = $_FILES['image']['tmp_name'];
      $image = addslashes(file_get_contents($image));
      saveimage($image);
      }
      }
      function saveimage($image)
      {
      $dbcon=mysqli_connect('localhost','root','','dbname');
      $qry="insert into tablename (name) values ('$image')";
      $result=mysqli_query($dbcon,$qry);
      if($result)
      {
      echo " <br/>Image uploaded.";
      header('location:urlofpage.php');
      }
      else
      {
      echo " error ";
      }
      }
      ?>
      </body>
      </html>





      share|improve this answer



























        up vote
        -1
        down vote













        This is the perfect code for uploading and displaying image through MySQL database.



        <html>
        <body>
        <form method="post" enctype="multipart/form-data">
        <input type="file" name="image"/>
        <input type="submit" name="submit" value="Upload"/>
        </form>
        <?php
        if(isset($_POST['submit']))
        {
        if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
        {
        echo " error ";
        }
        else
        {
        $image = $_FILES['image']['tmp_name'];
        $image = addslashes(file_get_contents($image));
        saveimage($image);
        }
        }
        function saveimage($image)
        {
        $dbcon=mysqli_connect('localhost','root','','dbname');
        $qry="insert into tablename (name) values ('$image')";
        $result=mysqli_query($dbcon,$qry);
        if($result)
        {
        echo " <br/>Image uploaded.";
        header('location:urlofpage.php');
        }
        else
        {
        echo " error ";
        }
        }
        ?>
        </body>
        </html>





        share|improve this answer

























          up vote
          -1
          down vote










          up vote
          -1
          down vote









          This is the perfect code for uploading and displaying image through MySQL database.



          <html>
          <body>
          <form method="post" enctype="multipart/form-data">
          <input type="file" name="image"/>
          <input type="submit" name="submit" value="Upload"/>
          </form>
          <?php
          if(isset($_POST['submit']))
          {
          if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
          {
          echo " error ";
          }
          else
          {
          $image = $_FILES['image']['tmp_name'];
          $image = addslashes(file_get_contents($image));
          saveimage($image);
          }
          }
          function saveimage($image)
          {
          $dbcon=mysqli_connect('localhost','root','','dbname');
          $qry="insert into tablename (name) values ('$image')";
          $result=mysqli_query($dbcon,$qry);
          if($result)
          {
          echo " <br/>Image uploaded.";
          header('location:urlofpage.php');
          }
          else
          {
          echo " error ";
          }
          }
          ?>
          </body>
          </html>





          share|improve this answer














          This is the perfect code for uploading and displaying image through MySQL database.



          <html>
          <body>
          <form method="post" enctype="multipart/form-data">
          <input type="file" name="image"/>
          <input type="submit" name="submit" value="Upload"/>
          </form>
          <?php
          if(isset($_POST['submit']))
          {
          if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
          {
          echo " error ";
          }
          else
          {
          $image = $_FILES['image']['tmp_name'];
          $image = addslashes(file_get_contents($image));
          saveimage($image);
          }
          }
          function saveimage($image)
          {
          $dbcon=mysqli_connect('localhost','root','','dbname');
          $qry="insert into tablename (name) values ('$image')";
          $result=mysqli_query($dbcon,$qry);
          if($result)
          {
          echo " <br/>Image uploaded.";
          header('location:urlofpage.php');
          }
          else
          {
          echo " error ";
          }
          }
          ?>
          </body>
          </html>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 at 9:35









          Pang

          6,8061563101




          6,8061563101










          answered Nov 10 at 22:00









          Mayur Gudi

          71




          71






















              up vote
              -2
              down vote













              How to INSERT INTO DB?



              <html>
              <head>
              <title>Uploads</title>
              </head>
              <body>
              <input type="file" name="file_array"/>
              <input type="file" name="file_array"/>
              <input type="file" name="file_array"/>
              <input type="submit" name="submit"/>
              </body>
              </html>

              <?php
              if(isset($_FILES['file_array']))
              {
              $name_array = $_FILES['file_array']['name'];
              $tmp_name_array = $_FILES['file_array']['tmp_name'];
              $type_array = $_FILES['file_array']['type'];
              $size_array = $_FILES['file_array']['size'];
              $error_array = $_FILES['file_array']['error'];
              $dir = "slideshow";

              for($i = 0; $i<count($tmp_name_array); $i++)
              {
              if(move_uploaded_file($tmp_name_array,"slideshow/".$name_array))
              {
              echo $name_array[$i]."Upload is complete<br>";

              } else {
              echo"Move_uploaded_file function failed for".$name_array[$i]."<br>";
              }
              }
              }
              ?>





              share|improve this answer



























                up vote
                -2
                down vote













                How to INSERT INTO DB?



                <html>
                <head>
                <title>Uploads</title>
                </head>
                <body>
                <input type="file" name="file_array"/>
                <input type="file" name="file_array"/>
                <input type="file" name="file_array"/>
                <input type="submit" name="submit"/>
                </body>
                </html>

                <?php
                if(isset($_FILES['file_array']))
                {
                $name_array = $_FILES['file_array']['name'];
                $tmp_name_array = $_FILES['file_array']['tmp_name'];
                $type_array = $_FILES['file_array']['type'];
                $size_array = $_FILES['file_array']['size'];
                $error_array = $_FILES['file_array']['error'];
                $dir = "slideshow";

                for($i = 0; $i<count($tmp_name_array); $i++)
                {
                if(move_uploaded_file($tmp_name_array,"slideshow/".$name_array))
                {
                echo $name_array[$i]."Upload is complete<br>";

                } else {
                echo"Move_uploaded_file function failed for".$name_array[$i]."<br>";
                }
                }
                }
                ?>





                share|improve this answer

























                  up vote
                  -2
                  down vote










                  up vote
                  -2
                  down vote









                  How to INSERT INTO DB?



                  <html>
                  <head>
                  <title>Uploads</title>
                  </head>
                  <body>
                  <input type="file" name="file_array"/>
                  <input type="file" name="file_array"/>
                  <input type="file" name="file_array"/>
                  <input type="submit" name="submit"/>
                  </body>
                  </html>

                  <?php
                  if(isset($_FILES['file_array']))
                  {
                  $name_array = $_FILES['file_array']['name'];
                  $tmp_name_array = $_FILES['file_array']['tmp_name'];
                  $type_array = $_FILES['file_array']['type'];
                  $size_array = $_FILES['file_array']['size'];
                  $error_array = $_FILES['file_array']['error'];
                  $dir = "slideshow";

                  for($i = 0; $i<count($tmp_name_array); $i++)
                  {
                  if(move_uploaded_file($tmp_name_array,"slideshow/".$name_array))
                  {
                  echo $name_array[$i]."Upload is complete<br>";

                  } else {
                  echo"Move_uploaded_file function failed for".$name_array[$i]."<br>";
                  }
                  }
                  }
                  ?>





                  share|improve this answer














                  How to INSERT INTO DB?



                  <html>
                  <head>
                  <title>Uploads</title>
                  </head>
                  <body>
                  <input type="file" name="file_array"/>
                  <input type="file" name="file_array"/>
                  <input type="file" name="file_array"/>
                  <input type="submit" name="submit"/>
                  </body>
                  </html>

                  <?php
                  if(isset($_FILES['file_array']))
                  {
                  $name_array = $_FILES['file_array']['name'];
                  $tmp_name_array = $_FILES['file_array']['tmp_name'];
                  $type_array = $_FILES['file_array']['type'];
                  $size_array = $_FILES['file_array']['size'];
                  $error_array = $_FILES['file_array']['error'];
                  $dir = "slideshow";

                  for($i = 0; $i<count($tmp_name_array); $i++)
                  {
                  if(move_uploaded_file($tmp_name_array,"slideshow/".$name_array))
                  {
                  echo $name_array[$i]."Upload is complete<br>";

                  } else {
                  echo"Move_uploaded_file function failed for".$name_array[$i]."<br>";
                  }
                  }
                  }
                  ?>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 30 '14 at 7:09









                  Jeeped

                  1




                  1










                  answered Nov 30 '14 at 7:01









                  Smmy Sa

                  52




                  52

















                      protected by Community May 17 '15 at 21:40



                      Thank you for your interest in this question.
                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                      Would you like to answer one of these unanswered questions instead?



                      Popular posts from this blog

                      Full-time equivalent

                      さくらももこ

                      13 indicted, 8 arrested in Calif. drug cartel investigation