-1

每次上传图片都说上传成功。当我检查我的 comp 上的临时文件夹时,是的,它是存储的,但是当涉及到数据库时,它不会存储我上传的任何图像。

<?php

if($_POST)
{
    $con = mysqli_connect("localhost", "root", "", "pamphotos") or die('error in connection'.mysqli_connect_error());

    $caption = $_POST['caption'];
    $image_cont = 'c://uploads//';
    $image_cont2= 'c://uploads//thmb//';
    $filename = $_FILES["file"]["name"];
    $src = $image_cont . $filename;
    $thumb = $image_cont2. $filename;


    if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
    else
        {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

            if (file_exists("upload/" . $_FILES["file"]["name"]))
            {
                echo $_FILES["file"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"],"c://uploads//" . $_FILES["file"]["name"]);

                    $q = "INSERT into photos(p_desc, p_src, p_thumbsrc) VALUES('$caption', '$src','$thumb')";

                    $stmt = mysqli_prepare($con, $q) or die('error in connection'.mysqli_connect_error());

                        if($stmt)
                        {
                             echo "File succesfully Uploaded.";
                        }

            }
        }

}
4

1 回答 1

0

您正在为您的 INSERT 语句使用prepare(),但您没有execute()它。所以该行没有插入到数据库中。

以下是我将如何编写运行查询的部分:

$stmt = $con->prepare($q);
if ($stmt === false)
{
    trigger_error($con->error, E_USER_ERROR);
}
if ($stmt->execute() === false) {
    trigger_error($stmt->error, E_USER_ERROR);
}
echo "File succesfully Uploaded.";

PS:这与您的问题有点相切,但是您应该使用查询参数而不是在查询中插入变量。您面临 SQL 注入的风险。

于 2013-10-02T16:34:47.903 回答