我有一个网页,旨在允许用户上传文件和提交评论。然后将文件和相应的注释输入到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);
}
?>有人能看出为什么文件链接按其应有的方式输入到数据库中,但注释却没有?id和时间戳字段也被正确地输入数据库。
发布于 2016-04-27 10:35:22
查看testpost.php文件中的查询参数->
$query = "
INSERT INTO `theshitp_posts`.`test2` (`id`, `comment`, `timestamp`) VALUES (NULL, '$users_comment', CURRENT_TIMESTAMP() );";查询参数中的第一个分号移除这个内部';‘第一个$query=’;“;<--在查询中和
第二个id = NULL <--在主键的情况下是不允许的,对于自动增量它可以是空的,但不是空的。
所以新的查询
$query = " INSERT INTO `theshitp_posts`.`test2` (`id`, `comment`, `timestamp`) VALUES ('', '$users_comment', CURRENT_TIMESTAMP() )";发布于 2016-04-27 10:22:37
您正在尝试在“id”中插入“Null”,该“id”必须是主键,因此此查询不能工作,需要更正。见-SQL Server, can't insert null into primary key field?
https://stackoverflow.com/questions/36886390
复制相似问题