-1

我收到了将图像路径插入数据库而不是图像文件本身的某些建议。但我仍然有问题,因为它根本不起作用。如果我的代码是这样的,我可以知道我的 SQL 表应该如何设置吗?也请指出我的错误。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php

session_start();

$dbhost = 'localhost';
$dbuser = 'root';
$dbpassword = '';

$dbconnect = mysql_connect($dbhost, $dbuser, $dbpassword) or die("gg");
$dbselect = mysql_select_db('uploadimg', $dbconnect) or die("gg1");

mysql_close($dbconnect);

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload Image Path Into SQL Database</title>
</head>

<body>

<?php

//getting the filename of the image file.
$filename = $_FILES["image"]["name"];

//directory name to be stored.
$path = "C:/xampp/htdocs/Test/images";

//uploading the image file with the image file name into the directory.
    if(move_uploaded_file($_FILES["image"]["tmp_name"],$path."/".$filename)) {

//if the image is stored success into the directory then we are going to store into database.

//the real path with the filename.

        $mysql_path = $path."/".$filename;

//sql query to be executed.
        $sql = "INSERT INTO uploadimg(filename,path) VALUES ('$filename','$mysql_path')";

//executing the query.
        if(mysql_query($sql)) {

            echo 'path inserted into database';

        }

        else {

            echo 'path not inserted into database';

        }

    }

    else {

        echo 'file not uploaded';

    }
}

?>

<form method="POST" enctype="multipart/form-data">
      File:
      <input type="file" name="image"> <input type="submit" value="Upload" />
</form>


</body>
</html>
4

1 回答 1

1

好吧,您的代码中有3个错误:

  1. 之后有一个额外的右大括号:

    别的 {

    echo 'file not uploaded';
    

    }

  2. 在你的 mysql_query() 函数中,你只提供了你的查询参数 $sql。但是对于 mysql_query() 函数,您还需要提交连接变量。(在本例中为 $dbconnect)。因此,您的声明将是:

    如果(mysql_query($sql,$dbconnect)){

  3. 您将在打开数据库连接后立即关闭它。所以用于在数据库中插入值的 php 代码将不起作用。因此,您必须在所有数据库工作完成后添加您的 mysql_close() 函数,在本例中为:

    别的 {

          echo 'file not uploaded';
    
      }
    

    mysql_close($dbconnect);

    ?>

*附加说明:我注意到您使用“uploadimg”作为数据库和表的名称。确保两者具有相同的名称!

希望有帮助!!干杯

于 2013-07-19T06:39:11.923 回答