I want to clear specific row values back to null, I have a prototype for loop that I would like to use. However I am afraid that it will delete everything in the table. The database has columns: user, password, correct, wrong, and game1, game2, game3…..game16. I want to clear the columns that start with game, but nothing else. It seems simple to do, however I cannot seem to make it work. Can someone help me understand?
$vals=array();
$sql="DELETE ";
for ($i=1; $i<=16; ++$i) {
if (isset(game$i)) { continue; }
if ($i != 1) {
$sql .= ",";
}
$sql .= "game$i=:game$i";
$vals[":game$i"]="game$i";
"game$i FROM user WHERE user = * ";
}
$statement=$db->prepare($sql);
$statement->execute($vals);
Setting a value to
NULLis anUPDATE, and different than removing the record from the database (DELETE)To prevent having to dynamically create you SQL Statement, it looks like you can (and probably should) just write out the whole SQL Script.
FROM user AS umakes an alias for user, so I can reference the table with the single characteru. You see this happen when I specify the UPDATE u (using the alias). W3Schools probably does a better job explaning SQL Aliases than I will.You do not need (and should not have) a
WHEREclause if you want theUPDATEto affect all the users, this is implied by not having aWHERE