gpt4 book ai didi

php - 输入mysql php后更新

转载 作者:行者123 更新时间:2023-11-29 14:08:54 24 4
gpt4 key购买 nike

我有一个积分系统,我希望人们在输入/表单后再获得 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."'";

最佳答案

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

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

请注意,上面的查询容易受到 SQL 注入(inject)的攻击,请阅读下面的文章以防范它,

关于php - 输入mysql php后更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13841613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com