我正在制作一个网站,该网站通过提交表单收集用户评论,然后将它们全部输出到一个页面上,在该页面上他们可以从 1 到 10 进行评级。我在评分系统部分遇到问题。
我有一个带有单选按钮的表单,用于通过“SendRating.php”通过邮寄发送值。我还想发送一个以前使用过的变量,它是与特定评论和特定评级相关的“id”。我曾尝试使用隐藏输入来执行此操作,但我似乎无法获得正确的语法,并且无论我尝试什么,我似乎都遇到了错误。
我将向您展示我拥有的两个 php 文件:
<?php
$con=mysqli_connect("host","me","pwd","base");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query= "SELECT id, comment, date_made, rating FROM table ORDER BY id DESC LIMIT 15";
$result=mysqli_query($con, $query);
while ($row=mysqli_fetch_array($result))
{
$date=$row['date_made'];
$id=$row['id'];
$num_id=intVal($id);
$current=$row['rating'];
echo '<div id="text" onpaste="return false"; oncut="return false"; readonly>';
echo '<div id="text2">';
echo "Submitted: ";
echo date('l jS F Y H:i:s ',strtotime($date));echo ' '; echo ' ';
echo ' '; echo "No: "; echo $row['id'];
echo '</div>';
echo '<br>';
echo nl2br($row['comment']);
echo '<br>'; echo '<br>';
echo '</a>';
//FOOT
echo '<div >';
echo '<form id="radio" name="input_rating" action="SendRating.php" method="post" >';
echo '<input type="hidden" name="id" value="'$id'" >';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="1">1';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="2">2';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="3">3';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="4">4';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="5">5';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="6">6';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="7">7';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="8">8';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="9">9';
echo '<input id="radio" type="radio" name="rating" onclick="this.form.submit()" value="10">10';
echo ' '; echo ' '; echo ' '; echo "Rating: ";
echo ' ';
echo "$current";
echo '</form>';
echo '</div>';
//END FOOT
echo '</div>';
echo '<p></p>';
}
?>
表单操作由以下处理(或者如果一切正常):
<?php
$con2=mysqli_connect("host","me","pwd","base");
// Check connection
if (mysqli_connect_errno($con2))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$value=$_POST['rating'];
$id=$_POST['id'];
mysqli_query ($con2, "UPDATE table SET totalraters = totalraters + 1 WHERE id='$id' ");
mysqli_query ($con2, "UPDATE table SET ratingsum = ratingsum + '$value' WHERE id='$id' ");
$query2 = mysqli_query ($con2, "SELECT FROM table ratingsum, totalraters WHERE id = '$id' ");
$result2 = mysqli_query($con2, $query2);
$row2 =mysqli_fetch_array($result2);
$ratingsum=$row2['ratingsum'];
$totalraters=$row2['totalraters'];
$current = $ratingsum/$ratingtotal;
mysqli_query ($con2, "INSERT INTO table rating VALUE '$current' WHERE id='$id' ");
include ('/index.html')
?>
包含页面应将用户带回到更新评级的起始页面。
ratingsum 是给定评分的总和,totalraters 是评分的人数。$current 应该是给出当前评级的平均值。
我目前遇到的问题是
echo '<input type="hidden" name="id" value="'$id'" >';
我似乎无法将 id 从一个地方获取到另一个地方以用于进一步的查询。我收到此行的语法错误。感激地收到帮助,并相信我已经尝试了所有方法,但没有正确的解决方案!请使用程序,因为我是新手,根本无法理解面向对象……这让我很头疼;-)