Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 请教一下更新语句,次数增加疑问!

请教一下更新语句,次数增加疑问!

Jun 23, 2016 pm 01:24 PM

select * from user where names='test';:??sql语句
update user SET num=$nums where names = '$test';:??sql增加次数语句
114:??$nums数据库里的储存次数+1
113:??$row['num']数据库里的储存次数
1:??$row['id']user表里的ID
test:??$row['names']数据库里的test数据

这个是names值的访问点击次数。奇怪的是一个链接点击的话,次数是每次增加2,而这个链接新窗口打开运行,却只增加一次,刷新同样增加一次

这是什么原因呀?哪里出错了?以下是查询更新语句和创建表的内容。

    $test = $_GET['names'];
    $sql = "select * from user where names='$test'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    $nums = $row['num']+1;
    mysql_query("update user SET num=$nums where names = '$test'");
    或者:mysql_query("update user SET num=$num+1 where names = '$test'");

    CREATE TABLE IF NOT EXISTS `user` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `names` varchar(15) NOT NULL DEFAULT '',
    `num` tinyint(1) unsigned NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`),
    UNIQUE KEY `names` (`names`)
    ) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;
    INSERT INTO `user` (`id`, `names`, `num`) VALUES
    (1, 'test', 0);

当前页面 - href="?names=test"
点击页面链接,增加两次次数记录,右键链接新窗口打开运行和当前页面刷新只增加一次记录!很是奇怪,到底我哪做错了?


回复讨论(解决方案)

$row = mysql_fetch_array($result);
    $nums = $row['num']+1;
    mysql_query("update user SET num=$nums where names = '$test'");
    或者:mysql_query("update user SET num=$num+1 where names = '$test'");

$nums = $row['num']+1;已经加1了,下边的或者 怎么还加1?【SET num=$num+1】

另外,传进来的值,你不做判断吗?

另外,传进来的值,你不做判断吗?


 $test = $_GET['names'];
这个只是简单的写法,
$nums = $row['num']+1;这个是我想访问一次就增加一次到数据库而已
另外
mysql_query("update user SET num=$num+1 where names = '$test'");
这个应该是写错,是num=num+1
这帖子里的代码我测试过,不考虑注入或其它,只是在正常的情况下是可以的,
就是有一点疑问,这个页面本身上放一个链接,或是通过外部点击链接进入这个?namestest ……这样子的话,num字段是会连续增加1,也就是变成了增加两次了,可是如果是点击这个链接通过 新窗口打开,或是当前页面刷新,num字段就会正常加1次!
是这个不明白为什么另外顺便问一下这个PHP COOKIES的
if (!isset($_COOKIE['visits'])) $_COOKIE['visits'] = 0;
$visits = $_COOKIE['visits'] + 1;
setcookie('visits', $visits, time() + 60); 
这样的代码放在gbk文件中就可以正常运行,但在UTF-8中却不行,这是为什么?如何处理

看你的描述是比较奇怪,可以这样调试。
$test = $_GET['names'];
    $sql = "select * from user where names='$test'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
   file_put_contents('sos.txt', $row['num'].PHP_EOL,FILE_APPEND );
    $nums = $row['num']+1;
   file_put_contents('sos.txt', $nums.PHP_EOL,FILE_APPEND );
    mysql_query("update user SET num=$nums where names = '$test'");
   file_put_contents('sos.txt', 'update'.PHP_EOL,FILE_APPEND );
?>

运行后看看sos.txt,是否3句,如果六句那就表示执行了两次。

看你的描述是比较奇怪,可以这样调试。
^^。


73
74
update
74
75
update
75
76
update
76
77
update
这个是点击访问后的,我连战点了两次!
这个下面的内容是我
73
74
update
74
75
update
75
76
update
76
77
update
77
78
update
78
79
update
一次新窗口打开运行,一次当前页面刷新

然后,我发现了一个更为可笑的事!
我一般都是用FIREFOX火狐浏览器进行测试,今天偶尔用IE测试了一下,结果!在IE8里面,却是不管我怎么新窗口,刷新,当前链接点击,都是只增加一次,很是正常!然后,用火狐再刷新,新窗口,当前链接,奇怪的事发生了,却全部正常了!
然后第二如果又是用IE或是手机等浏览器先访问,就正常,如果第二天用火狐先访问,就又是二次增加!
不明白!本地和服务器都测试过了,就这样!是我哪个地方步子错了?

你只描述了现象,并没有给出测试代码。
这就不好说了,可能是你的代码写的有问题(html 部分)

可以确定你的php没有问题,看看那里调用多次了。
看看firebug network那里的请求

可以确定你的php没有问题,看看那里调用多次了。
看看firebug network那里的请求


firebug , 我真没想到有这个功能,忘记了,这几天又正常……
先结贴吧,感谢大家!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles