php - MYSQL php数据库插入包括图像上传

标签 php mysql image upload blob

我正在尝试将用户输入插入到我的数据库中。我想将标题和描述添加到广告表中,并将图像添加到图片表中。 这是我到目前为止所想出的,但它根本不起作用。这是一个 posthandler.php 页面,当用户按下提交时将调用该页面。顶部的变量被分配来自 html 页面的输入名称。目前我的图片数据库中有路径名和 blob,看看哪个选项最有效。

请帮忙

<?php

session_start();
echo "You are signed in as ". $_SESSION['username'];

 include 'mysql.php'; 

 $title='title';
 $des='des';
 $image='image';

if(isset($_POST['submit'])) {
   $error = "";
     if (!empty($_POST['title'])) {
             $title= $_POST['title'];
        } else {
             $error = "Please enter a title for your Ad. <br />";
        }
        if (!empty($_POST['des'])) {
             $des = $_POST['des'];
        } else {
             $error = "Please describe your item <br />";
        }
        if (!empty($_POST['image'])) {

           $image=addslashes($_FILES['image']['tmp_name']);
           $name=addslashes($_FILES['image']['name']);
           $image=file_get_contents($image);
           $image=base64_encode($image);
           $filepath = "images/".$filename;
           move_uploaded_file($filetmp,$filepath);
           $image = $_POST['image'];

        } else {

        }

        if (empty($error)) {

        $conn= mysql_connect("localhost","km","data");
        if (!$conn){
            die ("Failed to connect to MySQL: " . mysql_error());

        mysql_select_db("mdb_km283",$conn);

        $sql = "INSERT INTO Advert (title, description) VALUES ('$_POST[title]','$_POST[des]')";
        $sql2 = "INSERT INTO Pictures(image, Path) VALUES ('$_POST[image]' $filepath)";

        mysql_query( $sql,$sql2, $conn );
        //mysql_query( $sql2,$conn );
        mysql_close($conn); 


            echo 'Upload successfull';
        }

        }
}


  ?>

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html version="-//W3C//DTD XHTML 1.1//EN"
      xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.w3.org/1999/xhtml
                          http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd"
>

<head>
<link rel="stylesheet" type=text/css" href="stylesheet.css">
<a href="signout.php">Logout</a>

<h1>ERROR - Please go back and fix the below:</h1>
</head>
<body>


<?php 
    if (!empty($error)) {
        echo'<p class="error"><strong>Upload failed. Please go back and fix the below <br/> The following error(s) returned:</strong><br/>' . $error . '</p>';
    } else {

    echo '<meta http-equiv="refresh" content="0; URL=user.php">';


    }


?>


</body>
</html>

最佳答案

$filename = $_FILES['image']['name'];
$filepath = 'image/' . $filename;
$filetmp = $_FILES['image']['temp_name'];
$result = move_uploaded_file($filetmp,$filepath);
if(!$result)
         echo "Photo field was blank";
else 
         echo "uploaded";

然后在这里写你的mysql代码....

关于php - MYSQL php数据库插入包括图像上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33978886/

相关文章:

php - 数据库中的图像未加载 PHP PDO

php - jQuery 自动完成阿拉伯语单词

php - 将文本列从 mysql 以 HTML 格式加载到 php

mysql - 长时间闲置后 : com. mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

image - 如何提取MVIMG的照片/视频组件?

HTML .png 图像作为按钮形状

php - 如何保存响应式菜单打开/关闭值?

javascript - AJAX/JQuery : Submit form and call function without refreshing page

php pdo 查询不起作用

java - 如何将 CLOB 转换为图像,反之亦然?