Hello:
Hopefully someone has some insite on how I can optimize the query in the following code.
I have a list of checkboxes on an html form that is dynamic, each of them is defined as such:
<input type="checkbox" name="files[]" value="<?echo "$file_id";?>">
Below is the code on submitting of the form:
if ($approve) {
$file_num_rows = count($files);
for ($i=0; $i < $file_num_rows; $i++) {
//Select file if it has been flagged in the database.
$sql = "SELECT file_id FROM acct_app WHERE file_id='$files[$i]'";
$result = @mysql($sql);
$app = @mysql_result($result, 0, "file_id");
//Prepare the query.
$app_files = "file_id=$files[$i]";
$file_app_id = $file_app_id . $app_files . " OR ";
$file_app_id = substr($file_app_id, 0, -3);
//Insert flag if the file hasn't been flagged.
if ($g_app == "acct" && $files[$i] != $app) {
$sql = "INSERT INTO acct_app (file_id, acct) VALUES('$files[$i]','$acct_name')";
$result = @mysql($sql);
}
}
}
It seems to me that the SELECT query isn't very efficient because it is in the FOR loop. So if I had 20 checkboxes checked I would have the run the SELECT query through each checkbox checked totaling 20. I'm worried that if the database were to get large this would take some time to run. I don't know the best way to re-write this so the SELECT query is outside the FOR loop. Does anyone have any insite on how to optimize this better or if I am going about this in the right approach?
Thanks,
Troy