Tonight, while working on the coding for a new database, a thought struck me about another one that I've been using (call it Program A) that I think might have a flawed INSERT command. For the new db I'd taken the framework from an old one (call it Program B for clarity), complete with auto_increment in the data and so, although there is only 1 item in the table, its ID number is 93. Based on the code (see below) I used in Program A, the INSERT would go into #2 and not #94, and so several dozen records latter 93 would either be over written or cause an error (not sure which), as it is assuming that I'll never delete a whole row from the table.
So, my question is, what is the right way to get the value for $dragon_id -- namely, the next "auto_increment" number?
PHP Code://get the information
$dragons_query = "SELECT dragon_id FROM clutch_dragons";
$dragons_result = mysql_query($dragons_query) or die(database_error("02010"));
$num_dragons = mysql_num_rows($dragons_result);
$dragon_id = $num_dragons + 1;
$addquery = "INSERT INTO clutch_dragons VALUES (".$dragon_id.", ".$eggs_id.", ".$clutch_id.", ".$weyrs_id.", '".$dragon_color."', '".$dragon_name."', '".$dragon_rider."', '".$dragon_status."', '".$owner_name."', ".$dragon_loc_now.")";
$addresult = mysql_query($addquery) or die(database_error("02012".mysql_error()));
echo "<p>Updated! I hope!</p>";


LinkBack URL
About LinkBacks



Reply With Quote
Bookmarks