将图片路径保存到MySQLtable时常犯的错误
Common mistakes commited in saving image path to a MySQL table
我正在做一个 php 项目。在其中一个网页中,我想为用户保留文件上传选项。
这是我的用户输入表单
<?php
$db=mysqli_connect('localhost','test','test123','study')
?>
<form form action="" method="post" enctype="multipart/form-data">
<label for="file_array[0]">Photo :</label>
<input type="file" name="file_array[0]" accept=".jpeg" required />
<input type="file" name="file_array[1]" accept=".jpeg" required />
<label for="file_array[0]">ID Proof :</label>
<input type="submit" value="Upload All">
</form>
<?php
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
$v1=rand(1111,9999);
$v2=rand(1111,9999);
$v3=$v1.$v2;
$v3=md5($v3);
$upload_directory="uploads/";
$TargetPath=$v3.$name_array[$i];
if(move_uploaded_file($tmp_name_array[$i],
$upload_directory.$TargetPath))
{
$sql = "INSERT INTO customers (imagepath)
VALUES ("$upload_directory.$TargetPath")";
echo $name_array[$i]." upload is complete<br>";
}
else {
echo "failed to ".$name_array[$i]."
<br>";
}
}
}
?>
问题是,图片成功上传到目录,但是图片路径没有保存到数据库中。
任何我在代码中做错的建议。
在上面的代码中,您没有执行查询。它只是保存在 $sql 中。参考以下代码:
if ($db->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
在数据库中保存图像路径它也取决于数据类型,因为 well.it 将是文本以保存任意长度的数据。
我正在做一个 php 项目。在其中一个网页中,我想为用户保留文件上传选项。 这是我的用户输入表单
<?php
$db=mysqli_connect('localhost','test','test123','study')
?>
<form form action="" method="post" enctype="multipart/form-data">
<label for="file_array[0]">Photo :</label>
<input type="file" name="file_array[0]" accept=".jpeg" required />
<input type="file" name="file_array[1]" accept=".jpeg" required />
<label for="file_array[0]">ID Proof :</label>
<input type="submit" value="Upload All">
</form>
<?php
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
$v1=rand(1111,9999);
$v2=rand(1111,9999);
$v3=$v1.$v2;
$v3=md5($v3);
$upload_directory="uploads/";
$TargetPath=$v3.$name_array[$i];
if(move_uploaded_file($tmp_name_array[$i],
$upload_directory.$TargetPath))
{
$sql = "INSERT INTO customers (imagepath)
VALUES ("$upload_directory.$TargetPath")";
echo $name_array[$i]." upload is complete<br>";
}
else {
echo "failed to ".$name_array[$i]."
<br>";
}
}
}
?>
问题是,图片成功上传到目录,但是图片路径没有保存到数据库中。 任何我在代码中做错的建议。
在上面的代码中,您没有执行查询。它只是保存在 $sql 中。参考以下代码:
if ($db->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
在数据库中保存图像路径它也取决于数据类型,因为 well.it 将是文本以保存任意长度的数据。