What I'm I missing in this update function?

here is my update function inside my DB.PHP class and calling it from index below. can anyone see a mistake? I;m not showing any syntax error, no warnings, nothing. it jsut doesnt’ want to work?

public function update($table, $id, $fields){
				$set = '';
				$x=1;
				foreach($fields as $name => $value){
					$set .= "{name} = ?";
					if($x < count($fields)){
						$set .=', ';
						}
						$x++;
					}
				
				$sql="UPDATE {$table} SET{$set} WHERE id={$id}";
				if(!$this->query($sql, $fields)->error()){
					return true;
					
					}
					return false;
				}		



<?php require_once'core/init.php';

$userInsert = DB::getInstance()->update('associates', 648, array(
'username'=> 'morgan',
'password'=>'newpassword'
));



?>

Just a shot in the dark, but isn’t there supposed to be a space after SET in your SQL query?

1 Like

Webmachine might have found the problem.
When you’re debugging a query, it’s a good idea to echo out the $sql and check if the query turned out the way you intended. And to copy and paste it in PHPMyAdmin (or whatever DBM you’re using) and see if it does what you want it to do.

1 Like

good eye… Yes I noticed that missing space too and corrected it but still not working.

var_dump ($userInsert) = bool(false)

Did you try what guido2004 suggested?

yes the query works when I put it in manually in phpMyadmin

What is your $this->query doing?

i’ve played around with it a bit but if I echo my query while it’s running I am getting this:

UPDATE associates SET {name} = ?, {name} = ? WHERE id=655

which looks fine to me.

Really? I would think you would have field names there.
Are you intending to set the foreach KEY $name as the field?

$set .= "{$name} = ?";

no, field names should be replacing {$name}. that’s what I don’t get???

I tried to kill the script before $sql and I am getting {$name}= ? as a result on the page.
it should read → password = ? (I removed all other fields for simplicity of testing)

public function update($table, $id, $fields){
				$set = '';
				$x=1;
				
				foreach($fields as $name => $value){
					$set .= "{name} = ?";
					if($x < count($fields)){
						$set .=', ';
						}
						$x++;
					}
				die($set);
				$sql="UPDATE {$table} SET {$set} WHERE id={$id}";
				echo $sql;
				if(!$this->query($sql, $fields)->error()){
					return true;
					
					}
					return false;
				}
<?php require_once'core/init.php';

if(Session::exists('success')){
	echo Session::flash('success');
	}
$userInsert = DB::getInstance()->update('associates', 655, array(
'password'=>'vigaro'
));

is the script I use to call the function. I have a similar script for “inserting” and it works perfectly. so I know the DB::getInstance() function works perfectly.

Looks like you have a typo:

$set .= "{name} = ?";

should be

$set .= "{$name} = ?";
2 Likes

you gotta be kidding me!!! Danmmmmmmmm. It’s as dumb as forgeting a semi colon at the end of the line and working and re-working the code and not seeing it.

someone come please and kick me on the chin lol…

Thank you fretburner… I don’t even have to test it, I can see that it was the problem. lol.

1 Like

Ya…

None of us saw it. Until you posted the result of the echoing of the query string. That’s why it’s such a good debugging tactic :wink:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.