PHP PDO UPDATE表无法正常工作

PHP PDO UPDATE表无法正常工作

问题描述:

I'm using the PHP PDO's to insert and update values in a table. 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. It still doesnt work

EDIT: Just found out that customer_id is being returned as 0 froma nother function. Thanks for the help.

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)

example

WHERE Name like '%name%";

Take the quotes out, 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.

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.

I don't believe you need the single quotes around the PDO variables (Examples - :name, :company, etc)

The query fails because you are updating by :name value, which is actually the new value you are trying to set. So no rows are found in order to be updated. To fix that you need to change the where clause and use :old_name.

Also, I recommend to use Primary Keys and numbers if available instead of the field name.