如何解决名称和描述未存储在数据库中的问题

时间:2019-06-01 11:50:20

标签: php mysql

我在网上找到了这个脚本,所以我根据它进行工作,现在我正在尝试将文件存储在数据库中,但是它始终以不保存名称和描述而告终,但是不幸的是我不知道问题出在哪里或如何解决它



<?php 

class BlobDemo {

    const DB_HOST = 'localhost';
    const DB_NAME = 'PFE_db';
    const DB_USER = 'root';
    const DB_PASSWORD = '';


    private $pdo = null;


    public function __construct() {

        $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME);

        try {
            $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD);

        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }


    public function insertBlob($filePath, $mime) {
        $blob = fopen($filePath, 'rb');

        $sql = "INSERT INTO files(mime,file) VALUES(:mime,:file)";
        $stmt = $this->pdo->prepare($sql);

        $stmt->bindParam(':mime', $mime);
        $stmt->bindParam(':file', $blob, PDO::PARAM_LOB);

        return $stmt->execute();
    }

    public function selectBlob($id) {

        $sql = "SELECT mime,
                        file
                   FROM files
                  WHERE id = :id;";

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(array(":id" => $id));
        $stmt->bindColumn(1, $mime);
        $stmt->bindColumn(2, $file, PDO::PARAM_LOB);

        $stmt->fetch(PDO::FETCH_BOUND);

        return array("mime" => $mime,
            "data" => $file);
    }

    public function __destruct() {
        // close the filebase connection
        $this->pdo = null;
    }


}

$blobObj = new BlobDemo();
if(isset($_POST['submit']))
{

    $blobObj->insertBlob($_FILES['file']['tmp_name'],"application/pdf");
    $name = $_POST['filename'];
    $description = $_POST['description'];
    if($name !=''||$description !=''){
    $sql1 = "INSERT INTO files (filename, description) VALUES ('$name','$description')";
    include('disp_quiz.html'); 

    }
    else{
    echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
    }
}

?>

我希望将所有字段保存在数据库中,以便以后可以随意下载文件

2 个答案:

答案 0 :(得分:-1)

执行您的$ sql1

$sql1 = "INSERT INTO files (filename, description) VALUES (:$name,:$description)";
        $stmt = $this->pdo->prepare($sql1);
        $stmt->execute(array(               
                ':name' => $name,
                ':description' => $description               
                ));

答案 1 :(得分:-1)

您应该在代码上执行mysql这样的操作。

$stmt = $this->pdo->prepare($sql1);
$stmt->execute();