简体   繁体   English

PHP PDO UPDATE表不起作用

[英]PHP PDO UPDATE table not working

I'm using the PHP PDO's to insert and update values in a table. 我正在使用PHP PDO插入和更新表中的值。 The insertion works fine. 插入工作正常。 But when i try to update, nothing happens. 但是当我尝试更新时,什么也没发生。 I get no errors, no exceptions. 我没有错误,也没有例外。

the code is 该代码是

   $sql="UPDATE customers SET Name=:name,Company=:company,Address=:address,City=:city,State=:state,Country=:country,MainP=:phonem,CellP=:phonec,Email=:email,Action=:action WHERE CompanyID=:cid";

and the rest: 其余的:

               $stmt = $dbh->prepare($sql);
                 $stmt->bindParam(":name",$name);
                $stmt->bindParam(":company",$company);
                $stmt->bindParam(":cid",$customer_id);
                $stmt->bindParam(":address",$address);
                $stmt->bindParam(":city",$city);
                $stmt->bindParam(":state",$state);
                $stmt->bindParam(":country",$country);
                $stmt->bindParam(":phonem",$main_num);
                $stmt->bindParam(":phonec",$cell_num);
                $stmt->bindParam(":email",$email);
            $stmt->bindParam(":action",$action);
            $stmt->execute();
           echo $stmt->rowCount();

So, is there something I am doing wrong? 那么,我在做错什么吗?

EDIT: Forgot to mention that i am getting a row count of 0. And I've tried without the quotes. 编辑:忘记提及我得到的行计数为0。而且我尝试了不使用引号。 It still doesnt work 它仍然不起作用

EDIT: Just found out that customer_id is being returned as 0 froma nother function. 编辑:刚发现从另一个函数中将customer_id返回为0。 Thanks for the help. 谢谢您的帮助。

Take the quotes out, field = :field 删除引号,field =:field

$sql="UPDATE customers SET Name = :name, Company = :company, Address  = :address, City = :city, State= :state, Country = :country, MainP = :phonem, CellP = :phonec, Email = :email, Action = :action WHERE Name = :name";

Also, Action may be protected, you may need to wrap it in `` 此外,动作可能受到保护,您可能需要将其包装在``

Finally, you're setting the name to the same value as your where clause which is bad, use a primary key. 最后,将名称设置为与where子句相同的值(不好),请使用主键。

Two reasons, one, it won't ever update the row you hoped, second, if someone else had that name you'd update their row's other values. 有两个原因,一是它永远不会更新您希望的行,二是如果其他人使用了该名称,您将更新其行的其他值。

我不认为您需要在PDO变量周围加上单引号(例如-:name,:company等)

The query fails because you are updating by :name value, which is actually the new value you are trying to set. 查询失败,因为您正在通过:name值进行更新,该值实际上是您尝试设置的新值。 So no rows are found in order to be updated. 因此,找不到要更新的行。 To fix that you need to change the where clause and use :old_name . 要解决此问题,您需要更改where子句并使用:old_name

Also, I recommend to use Primary Keys and numbers if available instead of the field name . 另外,我建议使用主键和数字(如果可用)代替字段name

May be there is no data that query can update ,when using strings like name in query ,try giving like & % to be safe(there may be empty spaces if not taken care off while inseting) 可能没有查询可更新的数据,当在查询中使用诸如name之类的字符串时,请尝试给&%提供安全性(如果在插入时不小心,可能会有空格)

example

WHERE Name like '%name%";

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

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