0

我有一个显示 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..

因此,我希望将选项值保存到“value”字段中相应的“cocktailID”,并将“1”张贴到 counter 字段。

- 任何帮助表示赞赏 -matt

4

1 回答 1

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
        }
    }
于 2012-05-03T06:19:21.203 回答