gpt4 book ai didi

php - 在mysql中上传图片并使用php上传到特定目录

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

这是我的 upload.php 代码,我想上传一个目录中的图像并将其路径保存到 mysql 数据库中。

图像已成功上传到目录,但其条目未插入数据库。

  • 上传.php
<?php

require("connect.php");

if (isset($_FILES["userfile"]) && !empty($_FILES["userfile"])) {
$image = $_FILES['userfile']['tmp_name'];
$imageName = $_FILES['userfile']['name'];
$about = $_POST['about'];
$title = $_POST['title'];
$place = $_POST['place'];
$date = $_POST['date'];
$time = $_POST['time'];
$link = $_POST['link'];
$details = $_POST['details'];
$con = $_POST['con'];
$email = $_POST['email'];
$number = $_POST['number'];
$len = count($image);
$path = "admin/news/";
for ($i = 0; $i < $len; $i++) {
if (isset($imageName[$i]) && $imageName[$i] !== NULL) {
if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
mysqli_query($con,"insert into tblnews (newsid, about, date_of_event, time, link, event_place, title, details, image, date, contactperson, email, number) values('','$about','$date','$time','$link','$place','$title','$details','$imageName[1]',NOW(),'$con','$email','$number')");
echo"<script>alert('The news had been successfuly uploaded.')
window.location='index.php?pg=homepage'</script>";
}
}
}
}
?>
  • HTML 格式
<form enctype="multipart/form-data" method="post" action="upload.php">
<table>
<tr>
<th>About</th>
<td>
<select name="about">
<option disabled selected>Select</option>
<option value="Taal">Taal Volcano</option>
<option value="Malarayat">Mt. Malarayat Forest Reserve</option>
<option value="vip">Verde Island Passage</option>
</select>
</td>
</tr>
<tr>
<th>Title</th>
<td><input type="text" name="title" required ></input></td>
</tr>
<tr>
<th>Event's Place</th>
<td><input type="text" name="place" required ></input></td>
</tr>
<tr>
<th>Event Date</th>
<td><input type="date" name="date" required></input></td>
</tr>
<tr>
<th>Event Time</th>
<td><input type="time" name="time"></input></td>
</tr>
<tr>
<th>Image</th>
<td><input name="userfile[]" type="file" multiple='multiple' />
</td>
</tr>
<tr>
<th>Link on Facebook</th>
<td><input type="text" name="link"></input></td>
<td><span>Go to the facebook page of the event and copy the url and paste it in the textbox.</span></td>
</tr>
<tr>
<th>Details</th>
<td><textarea name="details" style="height:250px;" ></textarea></td>
</tr>
<tr>
<th>Contact Person</th>
<td><input type="text" name="con" required ></input></td>
</tr>
<tr>
<th>Email</th>
<td><input type="email" name="email" required ></input></td>
</tr>
<tr>
<th>Number</th>
<td><input type="text" name="number" ></input></td>
</tr>
<tr>
<th></th>
<td><input type="submit" name="add_news" value="Upload"></input></td>
</tr>
</table>
</form>

最佳答案

尝试使用 prepared statement而不是将 variable 直接插入到您的查询中。

正如@ajaykumartak 指出的那样,newsid 可能是一个自动递增键,并且不需要在插入时设置

修改您的代码:

if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
$stmt = mysqli_stmt_init($con);
$query = <<<END
insert into tblnews (
about,
date_of_event,
time,
link,
event_place,
title,
details,
image,
date,
contactperson,
email,
number
)
values(?,?,?,?,?,?,?,?,?,?,?,?);
END;
mysqli_stmt_prepare($stmt,$query);
mysqli_stmt_bind_param($stmt,'ssssssssssss',
$about, $date, $time, $link,
$place, $tile, $details, $imageName[$i],
$date, $contactperson, $email, $number
);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}

当然你也应该检查函数调用的返回值来验证它们是否正确执行,因为我只是推断你的表结构。您需要使用 mysqli_error 检查错误:

echo ( mysqli_error($con) );

关于php - 在mysql中上传图片并使用php上传到特定目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29293956/

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