简体   繁体   English

使用PDO的MySql查询中的增量值

[英]Increment value in a MySql query using PDO

Why this code is not working (the value of $artInfo2['phoneState'] is 1): 为什么此代码不起作用($ artInfo2 ['phoneState']的值为1):

 if($artInfo2['phoneState']==1)
   {
        $bdd->prepare("UPDATE `articulos` SET `telVisto` = `telVisto` + 1 WHERE `ID` ='".$_GET['id']."' ");

        $verPorTodos=$salerInfo2['phone'];
   }

I'm using a PDO conection that works good because it works good with other queries. 我使用的PDO连接效果很好,因为它可以与其他查询一起使用。 Here is my data base conection that I use on te top of my page : 这是我在页面顶部使用的数据库连接:

$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=lacajota', 'root', '', $pdo_options);

And yes I want to update telVisto incrementing 1 for each time the code is run. 是的,我想每次运行代码时更新telVisto递增1。

Thank you 谢谢

Step 1: Sanitize your input. 步骤1:清理您的输入。 Never concatenate a query with $_GET['something'] , because I can delete your whole database like that. 永远不要用$_GET['something']连接查询,因为这样可以删除整个数据库。
Step 2: Handle errors properly. 步骤2:正确处理错误。
Step 3: prepare() doesn't execute the query. 步骤3: prepare()不执行查询。 It returns a Prepared Statement. 它返回一个准备好的语句。 http://php.net/manual/en/pdo.prepare.php You still need to execute it. http://php.net/manual/zh/pdo.prepare.php您仍然需要执行它。

$stmt = $bdd->prepare("UPDATE `articulos` SET `telVisto` = `telVisto` + 1 WHERE `ID` =:id ");
$stmt->execute(array(':id' => $_GET['id']));

Step 4. If nothing works, can you check if you even have a row to update? 步骤4.如果没有任何效果,您可以检查是否还有要更新的行吗? Does the query 请问查询

SELECT * FROM `articulos` WHERE `ID` = (insert your ID here manually)

return anything? 退还什么? Maybe the problem is that you don't work with the correct data? 也许问题是您使用的数据不正确?

what you were doing wrong is that you passed the old amount of telVisto as a string (telVisto='5'+1), and you can't add a number to a string. 您做错了什么是您将旧的telVisto作为字符串传递了(telVisto ='5'+ 1),并且无法在字符串中添加数字。 ;) ;)

And also check that column name 'ID' is either capital letter or small letter. 还要检查列名“ ID”是大写字母还是小写字母。

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

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