简体   繁体   中英

How to upload image in Mysql database using PHP

I got trouble uploading image and saving records in Mysql database using PHP, can somebody help me?

Code:

<?php
ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);
?>
<html>
  <body>
    <form method="post" enctype="multipart/form-data">
      </br>
      <input type="text" name="dbname"/>
      <input type="file" name="dbimage"/>
      <br> <br>
      <input type="submit" name"submit" value"Upload"/>
    </form>
    <?php
    if(isset($_POST['submit'])){

      if(getimage($_FILES['dbimage']['tmp_name']) == FALSE) {
        echo "Please select an image";
      } else {
        $dbimage = addcslashes($_FILES['dbimage']['tmp_name']);
        $dbname = addcslashes($_FILES['dbimage']['dbname']);
        $dbimage = file_get_contents($image);
        $dbimage = base64_encode($dbimage);
        saveimage($dbname, $dbimage);
      }

      function saveimage() {
        $con = mysql_connect("localhost", "root", "");
        mysql_select_db("db_test", $con);
        $qry = "insert into table1 (dname,dpic) values ('$dbname','$dbimage')";
        $result = mysql_query($qry, $con);
        if ($result){
          echo "Image uploaded.";
        } else {
          echo " Image not uploaded.";
        }
      }
    }?>
  </body>
</html>

你必须给函数 saveimage() 变量 $dbname 和 $dbimage ... 尽管如此,请使用 PDO ...

You have missed parameters in function saveimage(). It should be like this: function saveimage($dbname,$dbimage)

I think that you forgot to pass the params to your function

function saveimage($dbname,$dbimage){...}

Try to declare your function outside of your condition.

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