【问题标题】:Rating on Radio button is not stored未存储单选按钮上的评级
【发布时间】:2013-12-16 04:34:00
【问题描述】:

我有一个comment BOX,里面有5个字段,一个是name,email,RATE,comment,art​​icleid。

Rate 字段是一个单选类型,它有 5 个单选按钮,其值为 1,2,3,4,5。如果有人点击我的产品评分,它应该将评分值保存在数据库中。我在数据库中使用 RATE 作为 INT,它在其中存储 0,如果我在数据库中使用 RATE 作为 TEXT,它在数据库中存储“on”。它不存储像 1,2,3,4,5 这样的评级值。

我的表单代码

<form action="manage_comments.php" method="post">
<span class="rating">
<input type="radio" class="rating-input" id="rate" name="rate" value="1">
<label for="rating-input-1-5" class="rating-star"></label>

<input type="radio" class="rating-input" id="rate" name="rate" value="2">
<label for="rating-input-1-4" class="rating-star"></label>

<input type="radio" class="rating-input" id="rate" name="rate" value="3">
<label for="rating-input-1-3" class="rating-star"></label>

<input type="radio" class="rating-input" id="rate" name="rate" value="4">
<label for="rating-input-1-2" class="rating-star"></label>

<input type="radio" class="rating-input" id="rate" name="rate" value="5">
<label for="rating-input-1-1" class="rating-star"></label>

</span>

<input type='hidden' name='articleid' id='articleid' value='<?php echo $_GET["id"]; ?>' />
<input type="submit" name="submit" value="Publish Now"></p>

</form>

我的 php 代码

<?php
if( $_POST ) 
{
$con = mysql_connect("localhost","asfi","asfi");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("aw-tech", $con);

$post_rate =  (isset($_POST['rate'])) ? $_POST['rate'] : '';

$articleid =  (int)isset($_GET['id']);
if(!is_numeric($articleid))
die('invalid article id');

$sql="INSERT INTO `aw-tech`.`comment` (cid, name, email, website, comment, timestamp,      rate, articleid) VALUES (NULL, '$_POST[name]', '$_POST[email]', '$_POST[website]', '$_POST[comment]', CURRENT_TIMESTAMP, '$post_rate', ".$articleid.")";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Comment Saved";

mysql_close($con);
} 
?>

其次,我的 articleid 总是保存 0.. 我的页面 id 是 .php?id=49 ,它是 49 但如果我在那个页面上发表评论,它会保存我的文章 ID 总是 0。

articleid & rate 在数据库中都是 INT,我在数据库中也将它们用作 TEXT 但没有用

【问题讨论】:

  • 创建print_r($_POST); 并说明打印的结果是否符合预期。
  • 无效关闭也显示了数据库结构。
  • @Nomi,我修复了这个问题 .. 仍然没有帮助

标签: php mysql database radio-button


【解决方案1】:

我想我发现了你的问题。将表单声明更改为:

<form action="manage_comments.php" method="post">

删除行:

<input type='hidden' name='articleid' id='articleid' value='<?php echo $_GET["id"]; ?>' />

然后 PHP 代码来自:

$post_rate =  (isset($_POST['rate'])) ? $_POST['rate'] : '';
$articleid =  (int)isset($_GET['id']);

到:

$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : 0;
$articleid = (int)isset($_GET['id']) ? $_GET['id'] : 0;

对于调试,在 $sql 变量之后添加它并在此处插入评论:

print_r($_GET);
print_r($_POST);
echo $sql;

希望它有效。

【讨论】:

  • 我试过你的代码,但没有任何效果......它仍然在数据库中保存 0 和 articleid。
  • @user3076307, “并在此处插入评论”请在此处输入信息或提供指向您正在编辑的页面的链接..
【解决方案2】:

当您发布表单时,您输入:manage_cmets.php 所以“id”会丢失!

您可以将其付诸实践:manage_comments.php?id=&lt;?php=$_GET[id]?&gt;

或 提交表格后 使用$_POST[articleid] 而不是$_GET['id']

【讨论】:

  • @user3076307,您是否阅读了有关问题的 cmets?给他们写一个答复。
【解决方案3】:

我觉得应该是

$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : '';

$articleid = (int)isset($_POST['articleid']);

【讨论】:

  • 这不起作用,它保存1作为任何ID,它没有保存正确的ID,我使用了GET
  • 这很奇怪。正如我在我的电脑上测试的那样,它总是给出正确的 ID。我认为您可以再试一次并使用 POST
  • 我试过了,还是不行。如果您可以将您的代码通过电子邮件发送给我,那么我可以根据您的代码修复我的代码。 PrinceAsfi@gmail.com
  • 代码和你的一模一样,只是上面的行不同,我只是通过 echo 测试了 $articaleid 而不是保存到数据库。你能检查你的查询是否正确吗?我认为 $_POST['name'] 不是 $_POST[name]。确保您的查询是正确的
猜你喜欢
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
相关资源
最近更新 更多