通过PHP表单将IMAGE上传到MySQL

时间:2015-02-24 19:38:20

标签: php mysql forms

我尝试上传单个png / jpeg图像并将其作为BLOB存储在MySQL中 图像是"插入"以及其他表单字段,如标题,描述等。

表单方法设置为发布并enctype="multipart/formdata"> 和我的图像列设置为blob类型

上传进程/脚本没有运行,没有错误消息也没有成功...但是用于上传其他表单输入的脚本运行得很好,如何添加这两个脚本才能正常运行?

PHP

 <?php

$user = 'root';
$password = 'root';
$db = 'db_1';
$host = 'localhost';
$port = 888;

$link = mysqli_init();
$success = mysqli_real_connect(
   $link, 
   $host, 
   $user, 
   $password, 
   $db,
   $port
);

        $title = mysqli_real_escape_string($link, $_POST['booktitle']); 

        $description = mysqli_real_escape_string($link, $_POST['bookdesc']); 

        $burl = mysqli_real_escape_string($link, $_POST['bookurl']);

        $smurl = mysqli_real_escape_string($link, $_POST['smurl']);

        $tag = mysqli_real_escape_string($link, $_POST['tag']);

        $tagurl = mysqli_real_escape_string($link, $_POST['tagurl']);

        $tag2 = mysqli_real_escape_string($link, $_POST['tag']);

        $tag2url = mysqli_real_escape_string($link, $_POST['tagurl']);


        $errors = true; 

$query = "INSERT INTO second (title, description, burl, smurl, tag, tagurl, tag2, tag2url, image) VALUES ('$title', '$description', '$burl', '$smurl', '$tag', '$tagurl', '$tag2', '$tag2url', '$image')";
 if (!mysqli_query($link,$query)) 
       {
          die('Error: ' . mysqli_error($link));
       }
       else
       {
          echo "  <strong> Welcome Mohammed Azizi! </strong>
          <a href=\"books2.php\"> Home </a> | <a href=\"admin-books.php\"> Admin Panel </a> |<hr /><br />
          <center>
          <table border=\"2\" width=\"50%\">
          <tr >
          <td style=\"background:#333\">
          <img src=\"bg/libicon2.png\">
          </td>
          <td style=\"background:#999\">
          <font size=\"32px\"> ... </font>
          </td>
          <tr>
          <td>Book Title:</td>
          <td><i>$title</i></td>
          </tr>
          <tr style=\"background:#e3e3e3\">
           <td>Description</td>
          <td><i>$description</i></td>
          </tr>
          <tr>
           <td>Book URL</td>
          <td><i>$burl</i></td>
          </tr>
          <tr style=\"background:#e3e3e3\">
           <td>SM URL</td>
          <td><i>$smurl</i></td>
          </tr>
          <tr>
           <td>Tag 1</td>
          <td><i>$tag</i></td>
          </tr>
          <tr style=\"background:#e3e3e3\">
           <td>Tag 1 URL</td>
          <td><i>$tagurl</i></td>
          </tr>
          <tr>
           <td>Tag 2</td>
          <td>$tag2</td>
          </tr>
          <tr style=\"background:#e3e3e3\">
           <td>Tag 2 URL</td>
          <td><i>$tag2url</i></td>
          </tr>

          </table> 
          <br/>
           <a href=\"admin-books.php\"> Next>> </a>";


       }

        mysqli_close($link);


    ?>

        <?php
        $user = 'root';
        $password = 'root';
        $db = 'search_1';
        $host = 'localhost';
        $port = 8889;

        $link2 = mysqli_init();
        $success = mysqli_real_connect(
       $link, 
       $host, 
       $user, 
       $password, 
       $db,
       $port
    );
        if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
      $tmpName  = $_FILES['image']['tmp_name'];  
      $fp = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);
      $query2 = "INSERT INTO second ";
      $query2 .= "(image) VALUES ('$data')";
      $results = mysqli_query($query2, $link2) or die(mysqli_error());
      print "Success";
      }
      else {
      print "Error";
      }
        mysqli_close($link2);

        ?>

HTML

<form action="addbook.php" method="post" enctype="multipart/form-data">

<fieldset>

<legend>Add Book - First Year</legend>


<label for="altText">Title</label>
<input type="text" size="60" name="booktitle" id="booktitle"/><br />
<br />

<label for="altText">Description/Author</label>
<input type="text" size="60" name="bookdesc" id="bookdesc"/><br />
<br />

<label for="altText">Book URL</label>
<input type="text" size="60" name="bookurl" id="bookurl"/><br />
<br />

<label for="altText">SM URL</label>
<input type="text" size="60" name="smurl" id="smurl"/><br />
<br />

<label for="altText">Tag 1</label>
<input type="text" size="60" name="tag" id="tag"/><br />
<br />

<label for="altText">Tag 1 URL</label>
<input type="text" size="60" name="tagurl" id="tagurl"/><br />
<br />

<label for="altText">Tag 2</label>
<input type="text" size="60" name="tag2" id="tag2"/><br />
<br />

<label for="altText">Tag 2 URL</label>
<input type="text" size="60" name="tag2url" id="tag2url"/><br />
<br />



</fieldset>

<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/png" type="file">

<input type="submit" value="Upload File" />

</form>

0 个答案:

没有答案