mysql use CASE to update multiple rows with a for loop -
i need update 360 rows. doesn't work properly. help, please
for ($i = 1; $i <= 360; $i++) { $info = mysql_real_escape_string($_post[$i]); $check = mysql_real_escape_string($_post[''.$i.'check']); $case = "when '$i' '$info'"; } $sql = "update $table set info = case id $case end id in (1-360)"; mysql_query($sql) or die (mysql_error());
there's @ least 1 (not very?) obvious reason why doesn't work - you're replacing contents of $case every iteration of loop. $case .= "when '$i' '$info'\n";
(note dot before equal sign) might further. 1 $i put in single quotes inside query while id columns tend numeric.
i didn't try , run code. if still "doesn't work", should edit question , add more info table structure , error messages.
apart that, do:
$sql = "update $table set\n"; ($i = 1; $i <= 360; $i++) { $info = mysql_real_escape_string($_post[$i]); //$check = mysql_real_escape_string($_post[''.$i.'check']); // what's $check for? $sql .= "info = '$info' id = $i\n"; } mysql_query($sql) or die (mysql_error());
running batch of statements against server not uncommon , might yield better performance looking matching value in 360 line case construct.
Comments
Post a Comment