简体   繁体   English

使用PHP更新MySQL字段的问题

[英]Problem with updating a MySQL field with PHP

I have a query: 我有一个问题:

UPDATE choices SET votes = votes + 1 WHERE choice_id = '$user_choice' 更新选项SET votes = votes + 1 WHERE choice_id ='$ user_choice'

But when I execute it in my script, the votes field is updated twice, so the votes will go from 4 to 6 instead to 5. It doesn't seem that it is getting called twice because I echo out stuff to test this and only get one echo. 但是当我在我的脚本中执行它时,投票字段会更新两次,所以投票将从4变为6而不是5。它似乎没有被调用两次,因为我回应了测试这个的东西而且只是得到一个回声。 Is there a way to have it so PHP will only execute this query once per page "refresh"? 有没有办法让它这样PHP只会在每页“刷新”时执行一次这个查询?

EDIT : Thanks for the responses, I'm using regular MySQL, no MySQLi or PDO. 编辑 :感谢您的回复,我使用的是常规MySQL,没有MySQLi或PDO。 Another thing I found is that when doing the query, it works when you start out with 0 and update to 1, but then after that it goes 3, 5, 7, ... 我发现的另一件事是,在进行查询时,当你从0开始并更新为1时它会起作用,但之后它会变为3,5,7,......

One other option is, if you are using firefox at all and have firbug installed you need to disable your cache. 另一个选择是,如果您使用的是firefox并安装了firbug,则需要禁用缓存。 For some reason firbug results in two calls to the dB. 由于某种原因,firbug导致两次调用dB。 Took weeks to figure this out where I work as QA was getting all kinds of strange results when testing. 花了好几周的时间才弄清楚我在哪里工作,因为QA在测试时会得到各种奇怪的结果。 he was the only one with firebug. 他是唯一一个有萤火虫的人。

There are several SQL interfaces for many different brands of database in PHP. PHP中有许多不同品牌的数据库有几个SQL接口。 You haven't shown the PHP code you use to execute the query, nor have you identified which brand of database you use. 您尚未显示用于执行查询的PHP代码,也没有确定您使用的数据库品牌。

In some SQL interfaces in PHP, creating the statement implicitly executes the SQL. 在PHP的一些SQL接口中,创建语句会隐式执行SQL。 Then you have the opportunity to fetch results (if it was a SELECT statement). 然后你有机会获取结果(如果它是一个SELECT语句)。 If your statement was a SELECT or DELETE, it's likely that no harm was done, though it's unnecessary to execute the statement twice. 如果您的语句是SELECT或DELETE,则可能没有造成任何损害,尽管不必执行两次语句。 If your statement was an INSERT or UPDATE, though, you may find it has taken effect twice. 但是,如果您的语句是INSERT或UPDATE,您可能会发现它已生效两次。

For example, using PDO: 例如,使用PDO:

$pdo = new PDO(...options...);
$stmt = $pdo->query('UPDATE ...'); // executes once
$stmt->execute(); // executes a second time

This answer is an overkill, but you can make sure it's executed twice, by enabling the binary logs in your mysql (remember, overkill!). 这个答案是一种矫枉过正,但你可以通过启用mysql中的二进制日志来确保它被执行两次(记住,矫枉过正!)。 You can then use mysqllog tool to view these files and see if the query was executed twice. 然后,您可以使用mysqllog工具查看这些文件,并查看查询是否执行了两次。

Everybody believes somewhere in your code it's being queried twice :) I'm using such query and it's working perfectly. 每个人都相信你的代码中的某个地方正在被查询两次:)我正在使用这样的查询,它的工作正常。 Another thing, I have a wrapper class for my PEAR_DB object. 另一件事,我有一个PEAR_DB对象的包装类。 If asked for, it can output the queries (and timestamps+stack trace) that were used when rendering the current page. 如果要求,它可以输出在呈现当前页面时使用的查询(以及时间戳+堆栈跟踪)。 I use it to find duplicate queries (like yours) and slow updates. 我用它来查找重复的查询(如你的)和慢速更新。

First you need to make proper query: 首先,您需要进行正确的查询:

UPDATE `choices` SET `votes` = `votes` + 1 WHERE `choice_id` = '$user_choice'

This query must work properly. 此查询必须正常工作。 Maybe you got error in PHP code and this query executes two times? 也许你在PHP代码中出错并且这个查询执行了两次?

You may just need to put brackets around the votes + 1 such as: 您可能只需要在votes + 1周围加上括号,例如:

UPDATE choices SET votes = (votes + 1) WHERE choice_id = '$user_choice';

I might also put a LIMIT 1 at the end. 我也可以在最后加上一个LIMIT 1

Also, have tried running the query outside of your PHP, like through PHPMyAdmin? 另外,尝试在PHP之外运行查询,例如通过PHPMyAdmin? Also, try echoing out your SQL in whole before running it...you may just find an error. 另外,在运行之前尝试回显整个SQL ...您可能只是发现错误。

I just created the a database table to test this query, I must say it is running fine on my side. 我刚刚创建了一个数据库表来测试这个查询,我必须说它运行正常。

UPDATE choices SET votes = ( votes +1 ) WHERE choice_id = '1' UPDATE choices SET votes =(投票+1)WHERE choice_id ='1'

I think the code my be executed twice in your application, can you try and print out the sql as it is being ran. 我认为代码可以在你的应用程序中执行两次,你可以尝试打印出正在运行的sql。 You can also run the outputted sql statement natively against your database. 您还可以针对数据库本机运行输出的sql语句。

To conclude , The sql statement is fine just double your application scripts. 总而言之,sql语句很好,只需将应用程序脚本加倍。

I'm just guessing here but maybe this code is executed once for every resource used on a page? 我只是在这里猜测,但是这个代码可能会对页面上使用的每个资源执行一次吗? So if you have an image or iframe that uses the same code both resources execute it once, resulting in the field being updated twice. 因此,如果您有一个使用相同代码的图像或iframe,则两个资源都会执行一次,从而导致该字段被更新两次。

I had this just last week in my PDO based session handler, my test counter would fire twice because the site logo is also served by php and i was updating the test counter in the auto_prepend_file. 我上周刚刚在基于PDO的会话处理程序中使用了这个,我的测试计数器会触发两次,因为站点徽标也由php提供,我正在更新auto_prepend_file中的测试计数器。

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

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