【问题标题】:php not storing the right value into the databasephp没有将正确的值存储到数据库中
【发布时间】:2013-10-04 20:50:17
【问题描述】:

所以我需要 php 来存储 $aid,它应该是用户当前正在评论的文章的 ID,但是它总是将它保存为 0,例如当我在页面 localhost/article 上时。 php?article_id=69 并写 echo $aid;它会回显 69,但是当我想将其存储到数据库中时,它只是将其存储为 0

if (isset($_GET['article_id'])) {

include("mySQLcon.inc.php");
$aid = mysql_real_escape_string($_GET['article_id']);
$query = "SELECT * FROM front_articles WHERE id = '$aid'";
$sql = mysql_query($query); 

}

while ($articles = mysql_fetch_object($sql)) {
?>
<div id="IApage">
<?php
echo '<b>' . $articles->title.'</b>'.'<br>'.
             $articles->message. '<br>';
?>
</div>
<?php
}
if(isset($_COOKIE['user'])){
?>
<div id="article_comment_form">
<form id="arti_com_form" name="article_com_for" method="post" action="article.php">  
    <textarea name="comment" rows="7" cols="50"></textarea><br>
    <input name="com_submit" type="submit" value="Comment"/>
</form>
</div>
<?php
}
if (isset($_POST['com_submit'])) {
  $comment_message = $_POST['comment'];
    $comment_query = mysql_query("INSERT INTO front_article_comments (article_id, poster_id, poster_username, message, date) VALUES ('$aid', '$poster_id', '$username', '$comment_message', NOW())");
}
echo $aid;
?>

【问题讨论】:

  • 您应该避免使用 mysql_ 函数,因为它们现在已被弃用。您需要查找 PDO 或改用 mysqli。
  • 您在辅助上使用 mysql_real_escape_string,我建议将其更改为也强制转换为 (int),因为这将确保数据完整性。 $aid = (int)$_GET['article_id'];黑客只能选择数字,不是sql,所以没问题。
  • 如果有人在他们的评论中添加',您的代码将无法工作。 bobby-tables.com

标签: php mysql post get


【解决方案1】:

如果 article_id 在您的数据库中属于 INT 类型,并且您为其提供 STRING,它将插入为 0。

尝试删除单引号。

此外,如果您打算升级您的 PHP,请尽早习惯使用 PDO,这样您就不必在最终决定升级时返回并更改所有内容。

【讨论】:

  • 为什么不显示错误?我一直以为这种情况会出现错误
  • 那我该怎么保存呢?
  • 无论哪里有这样的代码:'$aid' 将其更改为 $aid
  • @Fuv 关于如何修复它是正确的。没有报错是正常的,这是mySQL的行为
【解决方案2】:

您在数据库中的 id 字段可能为 int 。但是使用单引号会生成id 字符串。

既然不是这样,字符串会自动转换为0。

删除单引号是最好的,我只看到选项!

编辑 也许您应该打开错误报告并查看是否有任何警告/错误?这样我们就能拿到东西了?

【讨论】:

  • 代码回发到 article.php,因此 article_id 的值不会在页面加载之间传递......虽然这可能是一个因素,但 $aid 的值从未根据 OP 设置代码。
  • @ChrisRasco 显然 $aid 已设置,由他成功的 echo $aid 表明
  • 不清楚。重新阅读帖子。 “当我在页面 localhost/article.php?article_id=69 并写 echo $aid; 它会回显 69,但是当我想将它存储到数据库中时,它只是将它存储为 0” 在第一个例如,他在 URL 中手动设置了它。当他提交表单时,他的目标页面是article.php,而不是article.php?article_id=69。如果您转到 article.php,如何以及何时设置 $aid?
  • @ChrisRasco : 我感觉他发布的代码是 article.php 的?
  • 存储为0的原因已经确定,并不是没有设置。
【解决方案3】:

更新:@Kris 为了在我的演示中使用它,我必须将 $poster_id、$username 和 $_COOKIE['user'] 设置为静态值。这些在您的实际代码中设置在哪里?这段代码对我有用,并将数据插入到我的表中:http://codepad.org/Kv9GS4CC

发布后的结果:

> mysql> select * from front_article_comments;
> +----+------------+-----------+-----------------+-------------------+---------------------+
> | id | article_id | poster_id | poster_username | message           | date                |
> +----+------------+-----------+-----------------+-------------------+---------------------+
> |  1 |          1 |         1 | Chris           | This is a comment | 2013-10-04 21:32:00 |
> +----+------------+-----------+-----------------+-------------------+---------------------+

原创: 您的代码回发到 article.php,但没有 article_id 的值。最终结果是 $_GET['article_id'] 为空,这是您设置 $aid 的位置。您还需要转义评论的内容。试试这个:

<?php

if (isset($_GET['article_id'])) {

include("mySQLcon.inc.php");
$aid = (int)$_GET['article_id'];
$query = "SELECT * FROM front_articles WHERE id = ".$aid;
$sql = mysql_query($query); 

}

while ($articles = mysql_fetch_object($sql)) {
?>
<div id="IApage">
<?php
echo '<b>' . $articles->title.'</b>'.'<br>'.
             $articles->message. '<br>';
?>
</div>
<?php
}
if(isset($_COOKIE['user'])){
?>
<div id="article_comment_form">
<form id="arti_com_form" name="article_com_for" method="post" action="article.php?article_id=<?php echo $aid; ?>">  
    <textarea name="comment" rows="7" cols="50"></textarea><br>
    <input name="com_submit" type="submit" value="Comment"/>
</form>
</div>
<?php
}
if (isset($_POST['com_submit'])) {
    $comment_message = mysql_real_escape_string($_POST['comment']);
    $comment_query = mysql_query("INSERT INTO front_article_comments (article_id, poster_id, poster_username, message, date) VALUES (".$aid.", ".$poster_id.", '".$username."', '".$comment_message."', NOW())");
}
echo $aid;
?>

【讨论】:

  • 我认为这是最准确的猜测,因为我也忘了提到 $poster_id 也是数据库中的一个 int,但是它保存了正确的 ID,但是我尝试了你的代码,它没有不行……
  • 我正在创建一个相应的数据库来亲自尝试,而不是做出假设。
  • 编辑了我的帖子并链接到我验证有效的代码。从表单操作中删除 article_id=# 会导致警告,并且我的数据库中没有存储任何值。
【解决方案4】:

尝试更改此行

 $comment_query = mysql_query("INSERT INTO front_article_comments (article_id, poster_id, poster_username, message, date) VALUES ('$aid', '$poster_id', '$username', '$comment_message', NOW())");

进入

 $comment_query = mysql_query("INSERT INTO front_article_comments (article_id, poster_id, poster_username, message, date) VALUES ($aid, '$poster_id', '$username', '$comment_message', NOW())");

如你所见,我去掉了 $aid 周围的引号,因为我猜它是一个整数。

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2014-04-23
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多