Php 插入数据库mysqli prepared语句
我正在尝试将图像上载到我的服务器,并将其名称和路径保存到我的数据库中。将文件上载到“我的服务器”起作用,但将数据保存到数据库中不起作用。Php 插入数据库mysqli prepared语句,php,mysql,file-upload,mysqli,prepared-statement,Php,Mysql,File Upload,Mysqli,Prepared Statement,我正在尝试将图像上载到我的服务器,并将其名称和路径保存到我的数据库中。将文件上载到“我的服务器”起作用,但将数据保存到数据库中不起作用。 图像表 image_id int(11) image_name varchar(64) image_path varchar(64) 上传脚本 // Upload image to the server $target_dir = "./path/to/image/folder/"; $ta
图像表
image_id int(11)
image_name varchar(64)
image_path varchar(64)
上传脚本
// Upload image to the server
$target_dir = "./path/to/image/folder/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
echo "Die Datei ist keine Bilddatei.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Bitte nenne die Datei um, diese Datei existiert bereits.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Diese Datei ist größer als 5MB und kann daher nicht hochgeladen werden.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Ausschließlich JPG, JPEG, PNG & GIF Dateien können hochgeladen werden.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Ein Fehler ist aufgetreten. Die Datei konnte nicht hochgeladen werden.";
// if everything is ok, try to upload file
} else {
========================= DATABASE PART ========================
// create a database connection
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// prepare and bind
$stmt = $conn->prepare("INSERT INTO images (image_id, image_name, image_path) VALUES (?, ?, ?)");
$stmt->bind_param("iss", $image_id, $image_name, $image_path);
// set parameters and execute
$image_id = '';
$image_name = $target_file;
$image_path = $target_dir;
$stmt->execute();
$stmt->close();
$conn->close();
==================================================================
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
header("Location:http://homepage.com");
} else {
echo "Während des Hochladens der Bilddatei ist ein Fehler aufgetreten. Bitte versuche es erneut.";
}
}
文件完全按照应该的方式上载。
数据库保持为空,没有错误消息。
var\u dump on
$image\u name
和$image\u path
返回正确的值。我错过了什么?
我将非常感谢任何形式的帮助。谢谢大家! 而不是:
// prepare and bind
$stmt = $conn->prepare("INSERT INTO images (image_id, image_name, image_path) VALUES (?, ?, ?)");
$stmt->bind_param("iss", $image_id, $image_name, $image_path);
// set parameters and execute
$image_id = '';
$image_name = $target_file;
$image_path = $target_dir;
$stmt->execute();
尝试:
代码执行的顺序是从上到下。
现在,您可以在尚未设置变量的情况下调用变量。在连接、准备、绑定或执行时没有错误处理。没有办法知道出了什么问题,而且你似乎是把马车放在马之前,你应该得到有关它的通知。更像是
下车
“我想也许一个有经验的眼睛可以马上发现我的错误。”-不是吗?这可以解释为。编辑:@SuperDJ谢谢你的回答!现在,至少我得到了一条错误消息,我可以处理(对非对象调用成员函数bind_param())。
// set parameters and execute
$image_id = '';
$image_name = $target_file;
$image_path = $target_dir;
// prepare and bind
$stmt = $conn->prepare("INSERT INTO images (image_id, image_name, image_path) VALUES (?, ?, ?)");
$stmt->bind_param("iss", $image_id, $image_name, $image_path);