Stu Parker Stu Parker - 4 years ago 85
PHP Question

having trouble with sql

been almost a year since i have had to muck around with databases, and as a self taught starter, am having a bit of trouble.
Below is the part of code that is not working, above this is the successful connection to server.
From here it is failing to update and i am not sure why.
On my system, i do have a working PDO connection string if you feel that it should be done in PDO.
At this point i am poking holes in the air.
Appreciate your time.

$stmt = $conn->prepare("UPDATE bfl SET gamerid="?", name="?", age="?",location"?", email="?", console="?"WHERE team="?" ");



$stmt->bindParam(1, $gamerid);
$stmt->bindParam(2, $name);
$stmt->bindParam(3, $age);
$stmt->bindParam(4, $location);
$stmt->bindParam(5, $email);
$stmt->bindParam(6, $cosole);
$stmt->bindParam(7, $team);

$stmt->execute(array($gamerid,$name,$age,$location,$email,$co‌​nsole,$team);

Answer Source
  1. That mysql syntax is the insert syntax, not update.
  2. Use PDO not mysql_*.
  3. Use parameterized queries, not concatenated.
  4. Placeholders don't get quoted.
  5. Bind or bind in the execute, don't do both.
  6. Don't put parenthesis around the whole inner query.

Put together you get:

$stmt = $dbh->prepare('UPDATE bfl
                       SET gamerid=?,
                       name=?,
                       age=?,
                       location?,
                       email=?,
                       console=?
                       WHERE team=?');
$stmt->execute(array($gamerid,$name,$age,$location,$email,$co‌​nsole, $team));

Also look at using error reporting, http://php.net/manual/en/pdo.error-handling.php, this will help you in the future.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download