在文件上传中重复输入mysql的空行 [英] duplicate empty line entered into mysql on file upload
问题描述
我一直在努力工作,上传到我的网站的管理部分,几乎在那里...!
我的数据库中有一个名为test的表,其中包含4个字段 - id(int),title(varchar),desc(varchar)和photo(varchar) - 照片字段表示服务器上图像的来源。
我的代码是:
<?php include'dbc.php'; page_protect(); $!
$ b 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作为$ key => $ value){
$ get [$ key] = filter($ value);
foreach($ _ POST为$ key => $ value){
$ [$ 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']));
$ b mysql_query(INSERT INTO`test`(`title`,`desc`,`photo`)VALUES('$ title','$ desc','$ pic'));
if(move_uploaded_file($ _ FILES ['photo'] ['tmp_name'],$ target))
{
echoThe file。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传,并且您的信息已被添加到目录;
}
else {
echo抱歉,上传文件时出现问题。
}
?>
< form enctype =multipart / form-dataaction =uploader.phpmethod =POST>
标题:< input type =textname =title>< br>
说明:< input type =textname =desc>< br>
照片:< input type =filename =photo>< br>
< input type =submitvalue =Add>
< / form>
由于某些原因,当行被输入到mysql时,会插入一个重复的空行, :
ID标题描述照片
15 images / test /
16 test title test description images / test /test1.jpg
有没有什么理由是由上面的代码发生的 - 它相当简陋,但给为了得到这个工作,我真的不能再面对起这个痛苦和挣扎了!
感谢您提前给予任何帮助。
JD
据我所见,当第一次加载form时,但没有文件上传呢。所以你们需要把你的数据库相关的代码放在里面。
$ b $ pre $ if(move_uploaded_file($ _ FILES ['photo'] ['tmp_name '],$ target))
全码:
<?php include'dbc.php'; page_protect(); $!
$ b 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作为$ key => $ value){
$ get [$ key] = filter($ value);
foreach($ _ POST为$ key => $ value){
$ [$ key] = filter($ value);
}
?>
<?php
if($ _ FILES ['photo'])//检查是否上传文件
{
$ 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'));
回显文件。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传,并且您的信息已被添加到目录;
}
else
{
echo抱歉,上传文件时出现问题。
var_dump($ _ FILES); //用于调试目的
}
}
?>
< form enctype =multipart / form-dataaction =uploader.phpmethod =POST>
标题:< input type =textname =title>< br>
说明:< input type =textname =desc>< br>
照片:< input type =filename =photo>< br>
< input type =submitvalue =Add>
< / form>
I have been struggling for days to get a working upload onto the admin section of my site and am almost there...! I have a table in my database called test with 4 fields - id (int), title (varchar), desc (varchar) and photo (varchar) - the photo field represents the source of the image on the server. My code is:
<?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>
For some reason when the row is entered into mysql a duplicate empty row is inserted so the table looks like:
ID Title Desc Photo
15 images/test/
16 test title test description images/test/test1.jpg
Is there any reason this is happening from the code above - its fairly rudimentary but given the pain and struggle its taken to get this working I really cant face starting again!!!
Thanks in advance for any help.
JD
as far as I see, your database code is executing when form is first time loading, but no file uploaded yet. So yo'll need to move your database related code inside
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
Whole code:
<?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>
这篇关于在文件上传中重复输入mysql的空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!