Home  >  Article  >  Backend Development  >  yii update operation with parameters, update failed, very strange

yii update operation with parameters, update failed, very strange

WBOY
WBOYOriginal
2016-09-21 14:13:111116browse

Newbie, using yii framework 1.1, today when practicing the CRUD update operation of database DAO, I encountered a problem, please look at the code:

yii update operation with parameters, update failed, very strange
It means that I want to update the record with name value lishenglong in the database table to smart. The database table record is as follows:

yii update operation with parameters, update failed, very strange
That is, batch update, the following 4 rows of records are updated, but this is not the case.
The execution result is that only the first row is updated. What is the situation? ?
I turned on the log to view the executed sql statement. The statement is as follows:

yii update operation with parameters, update failed, very strange

yii update operation with parameters, update failed, very strange

I found the reason. When binding parameters, my update condition of name='lishenglong' was overwritten. My update condition was name='lishenglong', but when parsed, it became name='smart'. , name='smart' is actually the new value I want to update, not the update condition. Am I clear? Please also ask the aunt who has been through the pit to tell me the reason and give me the solution. I am very grateful~

Reply content:

Newbie, using yii framework 1.1, today when practicing the CRUD update operation of database DAO, I encountered a problem, please look at the code:

yii update operation with parameters, update failed, very strange
It means that I want to update the record with name value lishenglong in the database table to smart. The database table record is as follows:

yii update operation with parameters, update failed, very strange
That is, batch update, the following 4 rows of records are updated, but this is not the case.
The execution result is that only the first row is updated. What is the situation? ?
I turned on the log to view the executed sql statement. The statement is as follows:

yii update operation with parameters, update failed, very strange

yii update operation with parameters, update failed, very strange

I found the reason. When binding parameters, my update condition of name='lishenglong' was overwritten. My update condition was name='lishenglong', but when parsed, it became name='smart'. , name='smart' is actually the new value I want to update, not the update condition. Am I clear? Please also ask the aunt who has been through the pit to tell me the reason and give me the solution. I am very grateful~

View the update method of system.db.CDbCommand in the Yii1.1 Class Reference Manual. The method prototype is as follows:

<code>public integer update(string $table, array $columns, mixed $conditions='', array $params=array ( ))</code>

There is a special explanation for $params

<code>Do not use column names as parameter names here. They are reserved for $columns parameter.</code>

This means don’t use field names as placeholder names.
The solution is

<code>'name=:cname',array(':cname'=>'lishenglong')</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn