再一次是我。我保证这次之后我不会再打扰你了!:)
我有这个评分系统,它将允许用户对一篇文章进行评分。它在一定程度上起作用,但问题是它没有更新数据库中的数据,我不知道为什么。如有任何帮助,我们将不胜感激。:)
// 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>";
}
?>
谢谢大家!:)
中继该线路
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>";
}
您应该使用$_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>";
}
php 中没有$_SUBMIT超全局变量
使用$_POST
或$_GET
阅读此处
如果您仍然有问题,请使用mysql_error()
检查错误
mysql_query ("UPDATE vote SET total= total+$voted, votes = votes+1 WHERE id = $id")
or die(mysql_error());
或者回显查询并在本地数据库中运行它。
然后与我们分享您的错误。
不要使用mysql_,因为它们已被取消缓存。*
首先:使用$_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,更好的参数验证和转义是强制性的。