我有一个评论框,它有5个字段,一个是姓名、电子邮件、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);
}
?>
其次,我的文章总是保存0。我的页面id是.php?id=49,是49,但如果我在那个页面上发表评论,它会保存我的文章id始终为0。
articleid&rate两者在数据库中都是INT,我在数据库中也将它们用作TEXT,但不起作用
我想我发现了你的问题。将表单声明更改为:
<form action="manage_comments.php" method="post">
删除行:
<input type='hidden' name='articleid' id='articleid' value='<?php echo $_GET["id"]; ?>' />
Then 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;
希望它能起作用。
当您发布执行中的表单时:manage_comments.php所以"id"就丢失了!
在行动中,你可以放:manage_comments.php?id=<?php=$_GET[id]?>
或提交后表格使用$_POST[articleid]
而不是$_GET['id']
我认为这是
$post_rate=(isset($_post['rate'])$_POST['rate']:";
$articleid=(int)isset($_POST['articleid']);