Problem with Postgres UPDATEs on table
| From: | William Moore | Date: | Wed, 14 Jun 2000 00:16:21 +0000 |
| Subject: | Problem with Postgres UPDATEs on table | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-394@lists.php.net to get a copy of this message | ||
Hello,
I was wondering if someone can correct my logic here, because this
is competely baffeling me. Basiclly what I have is a class called
UserData (shown below). The problem I am having is that when I call the
methood update( ) for the class, it updates every row in the table, even
though I specified a WHERE clause.
class UserData {
function UserData($uid) {
...
..
}
function update($vars) {
$Keys = array_keys($vars);
$Values = array_values($vars);
$buf = '';
for($i=0; $i < count($Keys); $i++) {
if($this->$Keys[$i] != $Values[$i]) {
$buf = $buf . "$Keys[$i]='$Values[$i]',";
}
}
if($buf) {
$buf[strlen($buf)-1] ='';
$sql = "UPDATE user_table SET $buf WHERE uid=". $this->uid;
$dbh = pg_connect(DBSTRING);
pg_exec($sql);
print $sql;
}
}
}
I have a .php file called user_update.php that has the variable $uid
passed to it. I call $wch = new UserData($uid) which loads the properties
of the users profile from a postgres database. This works. When I call
$wch->update($HTTP_POST_VARS) from that script, it works like I want, all
the proper fields are updated to the database, but the entire table is
updated so all rows have the same data. If I cut&past the generated sql
statement and run it in psql it works. If I run a statement like
pg_exec("UPDATE user_table SET name=$wch->name WHERE uid=$wch->uid")
directly from user_update.php, it works like you would expect. It appears
that when the UPDATE statement is made in a function, it updates the
entire table. Am I missing something obvious here, or am I trying to do
something that is not possible?