gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:39:47 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com