optimizing multiple table inserts

admin

Administrator
Staff member
I have a multiple table database accepting input from a form. The basic code works OK, but I'm wondering if there is a better/faster way to do it.

Currently, I do a check if the input already exists, insert it if it doesn't, or use the existing id if it does. I do this select and insert step individually for each table.

Such as:
// see if city is already in table, add if not

$cityresult = mysql_query("SELECT cityId, city FROM city WHERE city =
'$city'") or die("city select failed");

if (mysql_num_rows($cityresult)==0) {
//empty result set, so enter city data
$sql5 = mysql_query("INSERT INTO city (city) VALUES ('$city')") or
die("city insert error");
$cityId = mysql_insert_id();
if ($sql5) {
echo("city insert worked\\n");
}
}
else {
// city already in table
while ($cityN = mysql_fetch_array($cityresult)) {
$cityId = $cityN["cityId"];
$city = $cityN["city"];
}
}

This works fine, but seems to take FOREVER. (There are 11 tables to check).
IS there a better way to do this?
 
Back
Top