-1

I'm trying to upload multiple columns at once with this code:

    function updateMultiple($idElement,$arrayFieldValues=""){

        $toSave=$arrayFieldValues?$arrayFieldValues:$_POST;

        foreach($toSave as $field => $value) {
                    if (strpos($field, 'save_') === 0) {
                        $field = str_replace('save_', '', $field);
                        $updateString .= $field."='".addslashes($value)."', ";
                    }
                }
        $updateString = substr_replace($updateString ,"",-2);

       $query="UPDATE ".$this->tab."
            SET ".$updateString.", lastUser='".$usrId."'
            WHERE ".$colName." = '".$idElement."'";
            $this->execute($query);
   }

However I get this error:

PDOStatement::execute() [pdostatement.execute]: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET Country='England', FirstName='John', LastName='Smith', lastUser='10' ' at line 2

But to me it all seems right!

4

4 回答 4

1

Following from Tim's answer, you should be using Prepared Statements. Look at the documentation for examples and further documentation. In summary, instead of $query="UPDATE ".$this->tab." SET ".$updateString.", lastUser='".$usrId."' WHERE ".$colName." = '".$idElement."'"; use:

$query = 'UPDATE '.$this->tab.' SET lastUser = :user WHERE '.$colName.' = :colvalue'; 
$pdo->execute(array(:user => $usrId, :colvalue => $idElement));

You may further want to use transactions, but that's an exercise left to the reader.

于 2013-01-16T23:28:39.527 回答
0

You're missing a . between UPDATE and SET. Hmm, maybe. In any case, the SQL being sent to PDO is incorrect. It's missing the UPDATE keyword. Check the comment.

于 2013-01-16T23:21:06.690 回答
0

you need to provide a value for each column in your sql statement. try this code:

$query="UPDATE ".$this->tab." SET ".$updateString."='yourString' , lastUser='".$usrId."'
        WHERE ".$colName." = '".$idElement."'";
于 2013-01-16T23:28:15.893 回答
0

try this

$query="UPDATE news SET title='$title', date='$date', body='$body' WHERE newsID=$ID 
于 2013-12-10T13:36:41.053 回答