我有一个网页,旨在允许用户上传文件并提交评论。然后应将文件和相应的注释输入MySQL数据库。目前,如果有人试图这样做,则上传文件并将链接插入到数据库中。但是,注释永远不会进入数据库。该网页包含以下代码:
$('.edit').click(function (e) {
$(e.target).prev().toggle();
});
文件uploadform.php由
给出<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>
文件uploader.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>
文件testpost.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!";
}
}
?>
任何人都可以看到为什么文件链接正在输入数据库,但是评论不是吗? id和timestamp字段也正确输入到数据库中。
答案 0 :(得分:1)
查看testpost.php文件中的查询参数 - &gt;
$query = "
INSERT INTO `theshitp_posts`.`test2` (`id`, `comment`, `timestamp`) VALUES (NULL, '$users_comment', CURRENT_TIMESTAMP() );";
查询参数中的第一个分号是删除这个内部&#39 ;;&#39; first $ query =&#34;;&#34 ;; &lt; - 在查询和
中第二id
= NULL&lt; - 在主键的情况下不允许,自动增量可以为空但不是NULL
所以新查询
$query = " INSERT INTO `theshitp_posts`.`test2` (`id`, `comment`, `timestamp`) VALUES ('', '$users_comment', CURRENT_TIMESTAMP() )";
答案 1 :(得分:0)
您正试图将Null插入&#39; id&#39;必须是主键,因此此查询无效,您需要更正它。见 - SQL Server, can't insert null into primary key field?