复制空行在文件上传时输入mysql

时间:2011-06-24 12:35:43

标签: php mysql image file-upload

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

<?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

是否有任何理由从上面的代码中发生 - 它相当简陋,但考虑到为实现这项工作所带来的痛苦和挣扎,我真的无法面对重新开始!!!

提前感谢您的帮助。

JD

1 个答案:

答案 0 :(得分:1)

据我所知,当表单首次加载时,您的数据库代码正在执行,但尚未上传文件。所以你需要在

中移动与数据库相关的代码
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>