gpt4 book ai didi

php - 尝试在单独的行中上传 2 个文件时,数据库没有插入数据,文件也没有上传到设置的目录中,有任何可能的修复吗?

转载 作者:行者123 更新时间:2023-11-29 16:46:30 27 4
gpt4 key购买 nike

我得出的结论是我的最后一个问题不太清楚。我试图将两个文件上传到 MYSQL 数据库中的 2 个不同行,但是当我执行代码时,文件不会上传,也不会注册它们的路径。该查询根本不执行,它甚至不插入不是文件路径的数据。无论如何,这是变量、数组和查询的代码:

if (isset($_POST['bsw'])) {
$name1 = str_replace("<","&lt;",$_POST['name']);
$name = mysqli_real_escape_string($conn, $name1);

$powers1 = str_replace("<","&lt;",$_POST['powers']);
$powers = mysqli_real_escape_string($conn, $powers1);

$weaknesses1 = str_replace("<","&lt;",$_POST['weaknesses']);
$weaknesses = mysqli_real_escape_string($conn, $weaknesses1);

$about1 = str_replace("<","&lt;",$_POST['about']);
$about = mysqli_real_escape_string($conn, $about1);

$available1 = str_replace("<","&lt;",$_POST['available']);
$available = mysqli_real_escape_string($conn, $available1);

$demoFiletype1 = str_replace("<","&lt;",$_POST['filetype']);
$demoFiletype = mysqli_real_escape_string($conn, $demoFiletype1);

$iconDir = "files/uploads/bsw/icons/";
$iconName = basename($_FILES['icon']['name']);
$iconTemp = $_FILES['icon']['tmp_name'];
$iconError = $_FILES['icon']['error'];
$iconType = explode('.', $iconName);
$iconActualType = strtolower(end($iconType));
$iconAllowed = array('png', 'jpeg', 'jpg', 'gif');

$owner = $user->data['username'];
$ownerId = $user->data['user_id'];

$allowTypes = array('jpg','png','jpeg','gif');

$demoDir = "files/uploads/bsw/demos/";
$demoName = basename($_FILES['demo']['name']);
$demoTemp = $_FILES['demo']['tmp_name'];
$demoError = $_FILES['demo']['error'];
$demoType = explode('.', $demoName);
$demoActualType = strtolower(end($demoType));
$demoAllowed = array('swf', 'mp4');

if (empty($name) || empty($powers) || empty($weaknesses) || empty($about) || empty($iconName)){
echo '<p style="color: red; margin-left: 10px; margin-top: 5px;">Please Fill in All Fields</p>';
} else {
if(!in_array($iconActualType, $iconAllowed)) {
if($iconError === 1) {
echo '<p style="color: red; margin-left: 10px; margin-top: 5px;">Invalid filetype, only PNG, JPEG, JPG and GIF are allowed.</p>';
} else {
$iconNameNew = uniqid('', true).".".$iconActualType;
$iconDestination = __DIR__ . "files/uploads/bsw/icons/" . $iconNameNew;
$iconUpload = move_uploaded_file($iconTemp, $iconDestination);
if(!in_array($demoActualType, $demoAllowed)) {
if($demoError === 1) {
echo '<p style="color: red; margin-left: 10px; margin-top: 5px;">Invalid filetype, only SWF and MP4 are allowed.</p>';
} else {
$demoNameNew = uniqid('', true).".".$demoActualType;
$demoDestination = __DIR__ . "files/uploads/bsw/demos/" . $demoNameNew;
$demoUpload = move_uploaded_file($demoTemp, $demoDestination);
$fileUpload = mysqli_real_escape_string($iconUpload, $demoUpload);
if ($fileUpload) {
$sql = ("INSERT INTO
bsw
(`bsw_name`, `bsw_about`, `bsw_points`, `bsw_demo_location`, `bsw_power`, `bsw_weaknesses`, `bsw_icon_location`, `bsw_availability`, `bsw_owner`, `bsw_owner_id`, `bsw_type`)
VALUES
('$name', '$about', '0', '$demoDestination', '$powers', '$weaknesses', '$iconDestination', '$available', '$owner', '$ownerId', '$demoFiletype')");
mysqli_query($conn, $sql) or die("Error : ".mysqli_error($conn));
print_r($_FILES);
} else {
echo "Error uploading files, please try again later.";
}
}
}
}
}
}
}

这是表格:

<form action="" method="post" enctype="multipart/form-data">
<input style="margin-left: 10px; margin-top: 5px;" type="text" name="name" placeholder="BSW Name"><br>
<textarea style="margin-left: 10px; margin-top: 5px; width: 200px; height: 80px;" type="text" name="powers" placeholder="BSW Powers (seprated by commas)"></textarea>
<br>
<textarea style="margin-left: 10px; margin-top: 5px; width: 200px; height: 80px;" type="text" name="weaknesses" placeholder="BSW Weaknesses (seprated by commas)"></textarea>
<br>
<textarea style="margin-left: 10px; margin-top: 5px; width: 250px; height: 80px;" type="text" name="about" placeholder="BSW About (seprated by commas)"></textarea>
<br>
<strong>Are you currently available?</strong>
<select name="available">
<option name="yes">yes</option>
<option name="no">no</option>
</select>
<br><br>
<strong>Icons bigger than 100x100 will be rescaled</strong><br>
<input style="margin-left: 10px; margin-top: 5px;" type="file" name="icon"><br>
<strong>Demonstration (Only supports swf and mp4 filetypes): </strong><br>
<input style="margin-left: 10px; margin-top: 5px;" type="file" name="demo"><br><br>
<strong>Demo Filetype (Select none if you do not have a demo)</strong><br>
<select name="filetype">
<option name="mp4">mp4</option>
<option name="swf">swf</option>
<option name="none">none</option>
</select>
<br><br>
<button style="margin-left: 10px; margin-top: 5px;" type="submit" name="bsw">Submit BSW</button><br>
</form>

感谢任何及所有帮助!

编辑:虽然对准备好的语句的帮助很大,我真的很感激,但由于文件上传脚本没有首先执行,查询仍然没有被执行,所以我仍然需要帮助,因为我对此感到非常困惑。

最佳答案

就像 JNevill 所说,由于您使用的是 mysqli,您可能应该使用绑定(bind)参数:

$sql = $connection->prepare("插入 bsw (bsw_name、bsw_about、bsw_points、bsw_demo_location、bsw_power、 bsw_弱点、bsw_icon_location、bsw_availability、bsw_owner、 bsw_owner_id、bsw_type) 值(?,?,?,?,?,?,?,?,?,?,?)");

$sql->bind_param("sssssssssis", $name, $about, '0', $demoDestination, $powers, $weaknesses, $iconDestination, $available, $owner, $ownerId, $demoFiletype);

$sql->execute();

//bind_param中“s”代表“字符串类型”,“i”代表“整数类型”。通过查看你的变量,我假设它们是按这个顺序排列的类型。注意引号或缺少引号。

关于php - 尝试在单独的行中上传 2 个文件时,数据库没有插入数据,文件也没有上传到设置的目录中,有任何可能的修复吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072696/

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