简体   繁体   中英

upload image to database with php

First!, I have investigated so many similar posts on the site about this title : "Upload image to mysql using php", but did not get my answer.
I am coding a php script to post images from html form to mysql database. please help me fixed it because it is not working. tnx


this is html form

<form name="main_form" action="main.php" method="post" enctype="multipart/form-data">
  <input type="text" name="title" >
  <input type="file" name="img" ><br/>
  <input type="text" name="body" >
  <input type="text" name="link" >
  <input type="text" name="linktext" >
  <input type="submit" value="submit">
</form>

this is main.php

if(isset($_POST['title'])){
  $sql="INSERT INTO main (title, img, body, link, linktext)
  VALUES
  ('$_POST[title]','','$_POST[body]','$_POST[link]','$_POST[linktext]')";
  if (!mysql_query($sql,$con)){
    die('Error: ' . mysql_error());}
  if ($_FILES['img']['tmp_name'] != ''){
    $image = addslashes(file_get_contents($_FILES['img']['tmp_name']));
    if ($image == ''){
      echo 'Image type not supported';
    } else {
      $query  = "UPDATE main SET img='$image' WHERE title='$title'";
      $result = mysql_query($query);
      var_dump($result);
      if ($result){
        echo 'Image scaled and uploaded';
      } else {
        echo 'Error running the query';}
    }
  }
  $alert='record inserted';
  echo '<script>alert("'.$alert.'");</script>';
};  

img in database has type=BLOB record inserts successfully in database but img field (BLOB) has nothing in it and is always 0 bytes.

var_dump($result);

will display: boolean false

I am getting this message at the display: Error running the query

please help!
Thanks :)

It was my mistake!

sorry for spamming stackoverflow with this question. really sorry for that.

I had two connections to two databases. I have one users database and one other main database. the page. the error origins in connection. I corrected the query by adding the right connection string variable so that the connection is established to the main database where the main table exists.

sorry again and so many thanks to Dagon & Spiritfyre

You can delete the post if it is answered. There is a second answer though for adding images to database via form:

form page :

<form enctype="multipart/form-data" action="insert_image.php" method="post" name="changer">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>

insert to database page :

<?php

  include 'conf.php';

  if ($_FILES["image"]["error"] > 0)
  {
     echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
     echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";
   }
   else
   {
     move_uploaded_file($_FILES["image"]["tmp_name"],"images/" . $_FILES["image"]["name"]);
     echo"<font size = '5'><font color=\"#0CF44A\">SAVED<br>";

     $file="images/".$_FILES["image"]["name"];
     $sql="INSERT INTO database_table (table_column_1, table_column_2) VALUES ('','$file')";

     if (!mysql_query($sql))
     {
        die('Error: ' . mysql_error());
     }
     echo "<font size = '5'><font color=\"#0CF44A\">SAVED TO DATABASE";

   }

   mysql_close();

?>

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