<?php
include 'db.php';
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$stnam = $_POST['stName'];
$stage = $_POST['stAge'];
$stdob = $_POST['stDob'];
$pic=($_FILES['photo']['name']);
mysqli_query("INSERT INTO test (name, age, dob, photo) VALUES ('$stnam', '$stage', '$stdob', 'pic')");
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
我一直在尝试使用phpmyadmin将图像文件上传到sql数据库我在执行它时使用了上面的代码,文件很好地进入了目录,但数据值没有插入到mysql中。有人可以帮忙吗?
答案 0 :(得分:1)
<强> 更改 强>
1)在插入查询中将 $ 置于 pic 之前。
2)并在insert query
内保留if()
。因为,即使文件未移动到所需的文件夹,也会插入行。
3) mysqli_query
中没有使用连接变量。它需要2个参数。 [我假设$conn
为连接变量。根据您的连接变量替换它。]有关详细信息,请单击mysqli_query
更新代码
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){
mysqli_query($conn, "INSERT INTO test (name, age, dob, photo) VALUES ('$stnam', '$stage', '$stdob', '$pic')");
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
} else {
echo "Sorry, there was a problem uploading your file.";
}