gpt4 book ai didi

php - 文件上传时,重复的空行输入到 mysql 中

转载 作者:行者123 更新时间:2023-11-29 14:47:57 24 4
gpt4 key购买 nike

我几天来一直在努力将工作上传到我网站的管理部分,而且快到了......!我的数据库中有一个名为 test 的表,有 4 个字段 - id (int)、title (varchar)、desc (varchar) 和 photo (varchar) - photo 字段代表服务器上图像的来源。我的代码是:

<?php include 'dbc.php'; page_protect();

if(!checkAdmin()) {header("Location: login.php");
exit();
}

$host = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF']));
$path = rtrim($login_path, '/\\');

foreach($_GET as $key => $value) {
$get[$key] = filter($value);
}

foreach($_POST as $key => $value) {
$post[$key] = filter($value);
}
?>

<?php

$target = "images/test/";
$target = $target . basename( $_FILES['photo']['name']);

$title = mysql_real_escape_string($_POST['title']);
$desc = mysql_real_escape_string($_POST['desc']);
$pic = "images/test/" .(mysql_real_escape_string($_FILES['photo']['name']));

mysql_query("INSERT INTO `test` (`title`, `desc`, `photo`) VALUES ('$title', '$desc', '$pic')") ;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>

<form enctype="multipart/form-data" action="uploader.php" method="POST">
Title: <input type="text" name="title"><br>
Description: <input type="text" name = "desc"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form>

由于某种原因,当该行输入 mysql 时,会插入重复的空行,因此表看起来像:

ID              Title                Desc                   Photo 
15 images/test/
16 test title test description images/test/test1.jpg

上面的代码中发生这种情况有什么原因吗?它相当初级,但考虑到为使其正常工作而付出的痛苦和努力,我真的无法重新开始!!!

预先感谢您的帮助。

京东

最佳答案

据我所知,当第一次加载表单时,您的数据库代码正在执行,但尚未上传文件。因此,您需要将数据库相关代码移至内部

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))

完整代码:

<?php include 'dbc.php'; page_protect();

if(!checkAdmin()) {header("Location: login.php");
exit();
}

$host = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF']));
$path = rtrim($login_path, '/\\');

foreach($_GET as $key => $value) {
$get[$key] = filter($value);
}

foreach($_POST as $key => $value) {
$post[$key] = filter($value);
}
?>

<?php
if($_FILES['photo']) //check if we uploading a file
{
$target = "images/test/";
$target = $target . basename( $_FILES['photo']['name']);

$title = mysql_real_escape_string($_POST['title']);
$desc = mysql_real_escape_string($_POST['desc']);
$pic = "images/test/" .(mysql_real_escape_string($_FILES['photo']['name']));
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
mysql_query("INSERT INTO `test` (`title`, `desc`, `photo`) VALUES ('$title', '$desc', '$pic')") ;

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else
{
echo "Sorry, there was a problem uploading your file.";
var_dump($_FILES); //for debug purposes
}
}
?>

<form enctype="multipart/form-data" action="uploader.php" method="POST">
Title: <input type="text" name="title"><br>
Description: <input type="text" name = "desc"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form>

关于php - 文件上传时,重复的空行输入到 mysql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6467886/

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