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

时间:2015-04-02 08:26:46

标签: 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>

1 个答案:

答案 0 :(得分:0)

做了一些更改尝试此代码 如果您有任何错误,请随时提出

<!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>