再一次是我。我会保证在这之后我已经完成了一段时间了!!)
我有这个评级系统,允许用户评价文章。它有点工作,但问题是它不更新数据库中的数据,我不知道为什么。任何帮助,将不胜感激。 :)
// Connects to your Database
mysql_connect("URL", "username", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
if(isset($_submit['voted'])) {
mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM vote") or die(mysql_error());
//Now we loop through all the data
while($ratings = mysql_fetch_array( $data ))
{
//This outputs the sites name
Echo "Name: " .$ratings['name']."<br>";
//This calculates the sites ranking and then outputs it - rounded to 1 decimal
$current = $ratings['total'] / $ratings['votes'];
Echo "Current Rating: " . round($current, 1) . "<br>";
//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item
Echo "Rank Me: ";
Echo "<a href='index.php?site=kumu'?mode=vote&voted=1&id=".$ratings['id'].">1</a> | "; //The HREF was ".$_SERVER['PHP_SELF']." before
Echo "<a href='index.php?site=kumu'?mode=vote&voted=2&id=".$ratings['id'].">2</a> | ";
Echo "<a href='index.php?site=kumu'?mode=vote&voted=3&id=".$ratings['id'].">3</a> | ";
Echo "<a href='index.php?site=kumu'?mode=vote&voted=4&id=".$ratings['id'].">4</a> | ";
Echo "<a href='index.php?site=kumu'?mode=vote&voted=5&id=".$ratings['id'].">5</a><p>";
}
?>
谢谢你们!:)
答案 0 :(得分:1)
撤消此行
if(isset($_submit['voted'])) {
mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id");
echo "Your vote has been cast <p>";
}
有了这个
if(isset($_POST['voted'])) {
mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id");
echo "Your vote has been cast <p>";
}
答案 1 :(得分:0)
php
中有 No $ _SUBMIT 超全局变量使用$_POST
或$_GET
如果您仍然使用mysql_error()
mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id")
or die(mysql_error());
或回显您的查询并在本地数据库中运行它。
然后与我们分享您的错误。
不要使用mysql _ 因为它们被删除了。*
答案 2 :(得分:0)
你应该使用$_GET
//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
if(isset($_GET['voted'])) {
mysql_query ("UPDATE vote SET total= total+$_GET['voted'], votes = votes+1 WHERE id = $_GET['id']");
Echo "Your vote has been cast <p>";
}
答案 3 :(得分:0)
首先:使用$_GET['voted']
代替$_submit['voted']
。
此外,您构建的链接是错误的。改变
Echo "<a href='index.php?site=kumu'?mode=vote&voted=1&id=".$ratings['id'].">1</a> | "; //The HREF was ".$_SERVER['PHP_SELF']." before
Echo "<a href='index.php?site=kumu'?mode=vote&voted=2&id=".$ratings['id'].">2</a> | ";
Echo "<a href='index.php?site=kumu'?mode=vote&voted=3&id=".$ratings['id'].">3</a> | ";
Echo "<a href='index.php?site=kumu'?mode=vote&voted=4&id=".$ratings['id'].">4</a> | ";
Echo "<a href='index.php?site=kumu'?mode=vote&voted=5&id=".$ratings['id'].">5</a><p>";
到
Echo "<a href=index.php?site=kumu&mode=vote&voted=1&id=".$ratings['id'].">1</a> | "; //The HREF was ".$_SERVER['PHP_SELF']." before
Echo "<a href=index.php?site=kumu&mode=vote&voted=2&id=".$ratings['id'].">2</a> | ";
Echo "<a href=index.php?site=kumu&mode=vote&voted=3&id=".$ratings['id'].">3</a> | ";
Echo "<a href=index.php?site=kumu&mode=vote&voted=4&id=".$ratings['id'].">4</a> | ";
Echo "<a href=index.php?site=kumu&mode=vote&voted=5&id=".$ratings['id'].">5</a><p>";
<强>更新强>
替换声明:
if(isset($_submit['voted'])) {
mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
使用:
if(isset($_GET['voted']) && is_numeric($_GET['voted'])) {
mysql_query ("UPDATE vote SET total= total+ " . $_GET['voted'] . ", votes = votes+1 WHERE id = " . $_GET['id']);
Echo "Your vote has been cast <p>";
}
当然,为了防止任何SQLInjection,必须使用更好的参数验证和转义。