php - 使用 PHP/SQL 将图像上传到文件夹并将信息上传到数据库

标签 php mysql upload

我正在尝试使用 PHP 表单上传图片,以便将信息保存到数据库中,但图片会保存到文件夹中。我将“$pic”变量设置为“BLOB”,但是当我上传数据库时,它告诉我它是 0kb,并且文件夹中没有图片。我对 PHP 还很陌生,所以我希望你能理解我的意思:D Thx

代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert car!</title>
        <link rel="stylesheet" href="css/newgame.css"/>
    </head>
    <body>
    <?php
            require_once "includes/connection.php";

            $target_dir = "img/";
            $target_file = $target_dir . basename($_FILES['pic']['name']);

            $car = mysqli_real_escape_string($con, $_POST['car']);
            $descr = mysqli_real_escape_string($con, $_POST['description']);
            $manuf = mysqli_real_escape_string($con, $_POST['manuf']);
            $dist = mysqli_real_escape_string($con, $_POST['dist']);
            $price = mysqli_real_escape_string($con, $_POST['price']);
            $name = mysqli_real_escape_string($con, $_POST['name']);
            $pic = mysqli_real_escape_string($con, $_FILES['pic']['name']);
            move_uploaded_file($_FILES["pic"]["tmp_name"], $target_file);

            mysqli_query($con, "INSERT INTO carlist (pic, car, descr, year, dist, price, seller) VALUES ('$pic', '$car', '$descr', '$price', '$manuf', '$dist', '$name')");

            echo "<p>The following car information was successfully added to database:</p>";
            echo "<p>\"$car\"</p>";
            echo "<p>Description: $descr</p>";
            echo "<p>Price: $price</p>";
            echo "<p>Manufacturer: $manuf</p>";
            echo "<p>Picture: $pic</p>";
            echo "<p>Name: $name</p>";
            echo "<p>Distance: $dist</p>";

            mysqli_close($con);

        ?>
        <br><br>
        <a href="newcar.html">Insert another car</a>
        <br>
        <a href="index.php">Home</a>
    </body>
</html>

最佳答案

做了一些改变尝试这个代码 如果您遇到任何错误,请随时询问

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert car!</title>
        <link rel="stylesheet" href="css/newgame.css"/>
    </head>
    <body>
        <?php
        require_once "includes/connection.php";

        $target_dir = "img/";
        $target_file = $target_dir . basename($_FILES['pic']['name']);

        $car = mysqli_real_escape_string($con, $_POST['car']);
        $descr = mysqli_real_escape_string($con, $_POST['description']);
        $manuf = mysqli_real_escape_string($con, $_POST['manuf']);
        $dist = mysqli_real_escape_string($con, $_POST['dist']);
        $price = mysqli_real_escape_string($con, $_POST['price']);
        $name = mysqli_real_escape_string($con, $_POST['name']);
        $pic = mysqli_real_escape_string($con, $_FILES['pic']['name']);


        mysqli_query($con, "INSERT INTO carlist (pic, car, descr, year, dist, price, seller) VALUES ('$pic', '$car', '$descr', '$price', '$manuf', '$dist', '$name')");
        //Writes the photo to the server
        if (move_uploaded_file($_FILES['pic']['tmp_name'], $target)) {

//Tells you if its all ok
            echo "The file " . basename($_FILES['pic']['name']) . " has been uploaded, and your information has been added to the directory";
        } else {

//Gives and error if its not
            echo "Sorry, there was a problem uploading your file.";
        }

        echo "<p>The following car information was successfully added to database:</p>";
        echo "<p>\"$car\"</p>";
        echo "<p>Description: $descr</p>";
        echo "<p>Price: $price</p>";
        echo "<p>Manufacturer: $manuf</p>";
        echo "<p>Picture: $pic</p>";
        echo "<p>Name: $name</p>";
        echo "<p>Distance: $dist</p>";

        mysqli_close($con);
        ?>
        <br><br>
        <a href="newcar.html">Insert another car</a>
        <br>
        <a href="index.php">Home</a>
    </body>
</html>

关于php - 使用 PHP/SQL 将图像上传到文件夹并将信息上传到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29408342/

相关文章:

mysql - 使用附加搜索条件查找每组链接表中字段的总和

php - 无法通过php脚本连接到mysql,但可以在LAMP UBUNTU中登录phpmyadmin

php - 如何在 Ubuntu 14.04 LTS 中指向默认虚拟主机

php - MYSQL解析为XML代码

java - 无法填充池(没有可用的缓冲区空间)

javascript - 如何确定用户是否选择了要上传的文件?

node.js - 推送 Bluemix 应用程序会删除公用文件夹中的文件

javascript - 将图像从 localStorage 上传到 PHP

PHP 文档中的 Javascript 弹出 Yes 和 No 对话框窗口

java - JSP 或 JavaScript 相当于 PHP 的 $_SERVER ["HTTP_HOST"]?