簡體   English   中英

輸入MySQL php后更新

[英]Update after a input mysql php

我有一個積分系統,我希望人們在輸入/填寫表格后再獲得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 Injection攻擊,請閱讀以下文章以防止受到該攻擊,

我唯一的猜測是沒有設置$ points。 你能做個回聲$ points嗎? 在查詢之前?

我看不到您在哪里定義$points但如果只增加1

怎么樣

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

這樣,您無需獲取變量中的初始點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM