【问题标题】:Why are comments not being uploaded to MySQL Database?为什么评论没有上传到 MySQL 数据库?
【发布时间】: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);

标签: php html mysql sql


【解决方案1】:

在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() )";

【讨论】:

  • 我会尝试用空白条目替换 NULL。你在哪里看到 ;在查询中?最后?
  • 是的,在 testpost.php 的末尾 $query=" ; "
  • 谢谢,成功了。剩下的唯一问题是它创建了 2 个单独的数据库条目:一个带有文件链接和一个空白条目用于注释,另一个带有 NULL 条目用于文件链接和一个正确的注释条目。知道如何在数据库的一行而不是 2 行中获取所有这些数据吗?
  • 所以为此你必须执行更新操作而不是第二次插入表中,第二次插入会在表中创建新行..你明白了吗?因此,当您使用 mysql_insert_id() 插入文件名行时,请保持您的 ID 方便...并使用该 ID 更新行..
  • 将 testpost.php 中的 $query 更改为此` $query=" 更新 theshitp_posts.test2 SET comment=$comment, timestamp=CURRENT_TIMESTAMP() where id=".$id;`
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 2019-11-14
    • 2022-01-22
    相关资源
    最近更新 更多