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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles