首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么没有将评论上传到MySQL数据库?

问为什么没有将评论上传到MySQL数据库?
EN

Stack Overflow用户
提问于 2016-04-27 09:55:48
回答 2查看 52关注 0票数 0

我有一个网页,旨在允许用户上传文件和提交评论。然后将文件和相应的注释输入到MySQL数据库中。目前,如果有人试图这样做,文件将上传,链接将按其应有的方式插入到数据库中。但是,该注释从未输入到数据库中。该网页有以下代码:

代码语言:javascript
复制
<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提供给

代码语言:javascript
复制
<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包含以下内容:

代码语言:javascript
复制
<?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包含

代码语言:javascript
复制
<?
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和时间戳字段也被正确地输入数据库。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-27 10:35:22

查看testpost.php文件中的查询参数->

代码语言:javascript
复制
$query = "
  INSERT INTO `theshitp_posts`.`test2` (`id`, `comment`, `timestamp`) VALUES (NULL, '$users_comment', CURRENT_TIMESTAMP() );";

查询参数中的第一个分号移除这个内部';‘第一个$query=’;“;<--在查询中和

第二个id = NULL <--在主键的情况下是不允许的,对于自动增量它可以是空的,但不是空的。

所以新的查询

代码语言:javascript
复制
$query = " INSERT INTO `theshitp_posts`.`test2` (`id`, `comment`, `timestamp`) VALUES ('', '$users_comment', CURRENT_TIMESTAMP() )";
票数 3
EN

Stack Overflow用户

发布于 2016-04-27 10:22:37

您正在尝试在“id”中插入“Null”,该“id”必须是主键,因此此查询不能工作,需要更正。见-SQL Server, can't insert null into primary key field?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36886390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档