简单的评级系统;获取当前条目的ID并发布到表格

时间:2012-05-03 06:01:30

标签: php mysql forms

我有一个显示mysql条目数据的页面,具体取决于用户点击的链接($ pagename)..我想知道如何创建一个非常基本的评级系统,它将包含一个带有下拉列表的表单选项为1到5,当用户提交此值时,它会将数据发布到页面上当前条目的相应ID。

   <?php
$pagename = $_GET['name'];
$sql = "SELECT * FROM tblCocktail WHERE name = '$pagename' LIMIT 1";
/*$sql = sprintf(%sql, mysql_real_escape_string($pagename));*/
$result = mysql_query($sql);
if(!$result) {
    // error occured
}
$data = mysql_fetch_assoc($result);
echo "<p class=\"paratitle\">".$data["name"]."&nbsp;&nbsp;&nbsp;&nbsp;</p>";
echo "<p class=\"paratitle3\">".$data["howto"]."&nbsp;&nbsp;&nbsp;&nbsp;</p>";
echo "<p class=\"paratitle2\">".$data["ingredient1"]."&nbsp;&nbsp;&nbsp;&nbsp;</p>";
echo "<p class=\"paratitle3\">".$data["quantity1"]."&nbsp;&nbsp;&nbsp;&nbsp;</p>";
echo "<p class=\"paratitle2\">".$data["ingredient2"]."&nbsp;&nbsp;&nbsp;&nbsp;</p>";
echo "<p class=\"paratitle3\">".$data["quantity2"]."&nbsp;&nbsp;&nbsp;&nbsp;</p>";
echo "<p class=\"paratitle2\">".$data["ingredient3"]."&nbsp;&nbsp;&nbsp;&nbsp;</p>";
echo "<p class=\"paratitle3\">".$data["quantity3"]."&nbsp;&nbsp;&nbsp;&nbsp;</p>";
echo "<p class=\"dateadded\">".$data["dateadded"]."&nbsp;&nbsp;&nbsp;&nbsp;</p>";
?>
                        </div>
                    </div>
                    <div id="cont2">
                        <div id="contentwrap">
                    <form method="POST" action="addrating.php" >
           <input type="hidden" name="cocktailID" value="<?=$data["id"]?>">
           <select id="ratinglevel" name="ratinglevel">
              <option></option>
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
            </select>
            <input type="submit" value="submit" />
            </form>

addrating.php:

<?php
mysql_select_db("mwheywood", $con);

//insert cocktail details
$sql="INSERT INTO tblRating (cocktailID, value, counter)
VALUES
('$_POST[id]','$_POST[ratinglevel]','1'";

if (!mysql_query($sql,$con))
  {
  die('Error: you fail at life' . mysql_error());
  }
echo "<p>Thanks for voting</p>"

?>

我希望保存评级的表格通过“cocktailID”链接到上述代码中回显的数据。

和“tblRating”的表结构是:ratingID,cocktailID,value,counter ..

因此,我希望选项值保存到相应的“cocktailID”,在“值”字段中,并将“1”发布到计数器字段。

- 感谢任何帮助-matt

1 个答案:

答案 0 :(得分:1)

只需在发送表格时包含当前ID

           <form method="POST" action="addrating.php" >
           <input type="hidden" name="cocktailID" value="<?=$data["id"]?>">
           <select id="ratinglevel" name="ratinglevel">
              <option></option>
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
            </select>
            </form>

然后,这将返回ID以及表格结果,届时您将获得鸡尾酒ID

    if (isset($_POST['cocktailID']) && isset($_POST['ratinglevel']))
        $sql = "INSERT INTO tblRating SET cocktailID = ".mysql_real_escape_string($_POST['cocktailID']).", value = ".mysql_real_escape_string($_POST['ratinglevel']).",counter = 1";
        $result = mysql_query($sql);
        if(!$result) {
            // error occured
        }
    }