0

我有一个积分系统,我希望人们在输入/表格后再获得 1 分。直到现在我使用这个代码:

<?php
session_start();
//=============Configuring Server and Database=======
$host        =    'localhost';
$user        =    'root';
$password    =    '';
//=============Data Base Information=================
$database    =    'login';

$conn        =    mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
mysql_select_db($database,$conn) or die('Database Information is not correct');

//===============End Server Configuration============

//=============Starting Registration Script==========


$username    =    mysql_real_escape_string($_POST['txtusername']);

//=============To Encrypt Password===================

//============New Variable of Password is Now with an Encrypted Value========

if(isset($_POST['btnRegister'])) //===When I will Set the Button to 1 or Press Button to register
{
$query = mysql_query("SELECT * FROM quiz WHERE username='$username'");
if(mysql_num_rows($query) > 5 ){
echo "Sorry but you can only take a quize once:S";

}else{

$insert = "UPDATE `users` SET `points` = '".$points."' +1 WHERE `username` = '".$username."'";

 mysql_query($insert); 



mysql_query ("insert into quiz(username)values('$username')");
header('location: succes.php');
}
}
?>

但是这段代码似乎没有更新/添加 1,但它用 1 替换了那里的数字。所以如果我有 5 并且我运行这段代码,它会替换为 1,那么它会说 1。我该如何修复它添加/更新?因此,如果一个人有 3 分并运行代码,他会在获得 repacec 后获得 4 分:

$insert = "UPDATE `users` SET `points` = '".$points."' +1 WHERE `username` = '".$username."'";
4

3 回答 3

1

你不需要使用变量$points。使用以下UPDATE查询,

$insert = "UPDATE `users` SET `points` = (`points` + 1) WHERE `username` = '".$username."'";

请注意,上面的查询很容易受到攻击SQL Injection,请阅读下面的文章以保护它,

于 2012-12-12T14:12:38.837 回答
0

我看不出你在哪里定义$points,但如果它只是增加 1

怎么样

$insert = "UPDATE `users` SET `points` = `points`+1 WHERE `username` = '".$username."'";

这样,您不需要在变量中获取初始点

于 2012-12-12T14:14:07.077 回答
0

我唯一的猜测是 $points 没有设置。你能做一个 echo $points; 就在查询之前?

于 2012-12-12T14:13:37.123 回答