【发布时间】:2016-04-27 09:55:48
【问题描述】:
我有一个网页旨在允许用户上传文件并提交评论。然后应将文件和相应的注释输入到 MySQL 数据库中。目前,如果有人尝试这样做,则会上传文件并将链接插入到数据库中。但是,评论永远不会输入到数据库中。网页代码如下:
<div style="padding-left: 225px; padding-top: 15px; padding-bottom: 15px; background-color: #754f00; border-style: solid; border-weight: 4px; border-color: black;">
<div class="uploadbox" style='margin-left: 100px'>
<? include('uploadform.php') ?>
<? include('uploader.php'); ?>
</div>
<center>
<? include('testpost.php'); ?>
</center>
</div>
<br>
<hr>
<br>
<center>
<? include('feed.php'); ?>
</center>
文件uploadform.php由哪里给出
<form enctype="multipart/form-data" method="POST">
Comment:<br />
<textarea name='comment' id='comment'></textarea><br />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Submit" />
</form>
文件 uploader.php 包含以下内容:
<?php
if( $_POST ){
// Where the file is going to be placed
$target_path = "uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path .time() .basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The <a href=" . $target_path . ">file</a> has been uploaded! <br /> LINK: " . $target_path;
$con = mysql_connect("localhost","theshitp_user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("theshitp_posts", $con);
$query = "
INSERT INTO `theshitp_posts`.`test2` (`file`) VALUES ( '$target_path' );";
mysql_query($query);
echo "<p style='color: grey;'><b>Thank you for your Comment!</b></p>";
mysql_close($con);
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
并且文件 testpost.php 包含
<?
if( $_POST )
{
$con = mysql_connect("localhost","theshitp_user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("theshitp_posts", $con);
$users_comment = $_POST['comment'];
$users_comment = mysql_real_escape_string($users_comment);
$query = "
INSERT INTO `theshitp_posts`.`test2` (`id`, `comment`, `timestamp`) VALUES (NULL, '$users_comment', CURRENT_TIMESTAMP() );";
mysql_query($query);
echo "<p style='color: grey;'><b>Thank you for your Comment!</b></p>";
mysql_close($con);
}
?>
谁能看到为什么文件链接按应有的方式输入到数据库中,而 cmets 却没有? id 和 timestamp 字段也被正确输入到数据库中。
【问题讨论】:
-
而不是 '$users_comment' 尝试 $users_comment
-
感谢您的回复。不幸的是,我已经尝试过进行更改。它没有用。
-
print $_POST['comment'] - 如果你能看到文字,请告诉我
-
我已经做到了。使用该代码打印注释。
-
注释掉这一行然后看看会发生什么? $users_comment = mysql_real_escape_string($users_comment);