简体   繁体   English

输入MySQL php后更新

[英]Update after a input mysql php

I got a point system where i want people to get 1 more point after a input/form. 我有一个积分系统,我希望人们在输入/填写表格后再获得1分。 until now i use this code: 直到现在我都使用此代码:

<?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');
}
}
?>

but this code doesnt seem to update/add 1 but insted it replace the number there with 1. so if i have 5 and i run this code it replace with 1 so then it says 1. How can i fix so it add/update? 但是这段代码似乎没有更新/添加1,但是插入它替换为1。所以,如果我有5,并且运行此代码,它将替换为1,所以它说1。 so if one have 3 points and run the code he get 4 points insted off getting repacec: 因此,如果一个人获得3分并运行代码,那么他获得repacec可以得到4分:

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

you don't need to use a variable $points . 您不需要使用变量$points use the following UPDATE query, 使用以下UPDATE查询,

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

be warned that the query above is vulnerable with SQL Injection , please read the article below to protect from it, 请注意,上面的查询容易受到SQL Injection攻击,请阅读以下文章以防止受到该攻击,

My only guess is that $points isn't set. 我唯一的猜测是没有设置$ points。 Can you do a echo $points; 你能做个回声$ points吗? just before the query? 在查询之前?

I don't see where you define $points but if it's only to increment by 1 我看不到您在哪里定义$points但如果只增加1

How about 怎么样

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

That way, you don't need to get the initial points in a variable 这样,您无需获取变量中的初始点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM