[PHP]
<?php
$db = mysql_connect(" localhost", "root","");
mysql_select_db ("sistem bank soalan",$db);
$result = mysql_query("SE LECT * FROM soalan",$db);
$count_emp=mysq l_num_rows($res ult);
if ($count_emp==0)
$id=1;
else
$id=$count_emp+ 1;
echo($id)
?>
[/PHP]
Hi
I attempted to generate ID for my records. It seems that
they really work well when adding record into database. HOwever
problem arises when I tried to delete data that located in the middle
Based on the codes above, the $count_emp variable will return the number of records exist in the database.
The initial value for $id is 0. If there are 10 records for example in the database,($coun t_emp=10), the new value of id would be 11(because $id=$count_emp+ 1).But whenever I want to delete in the middle record, the value of $count_emp will become 9. The new record then will take the last id value which is 10 and this record surely fail to be inserted into the database as there's already exist record with id=10.
Those code might explain you to understand my problem much better. I also need codes that enable the new entry record to replace the location of the deleted record (meaning that the new inserted record would take the id value of the deleted record) and the next new record would then be inserted normally.
<?php
$db = mysql_connect(" localhost", "root","");
mysql_select_db ("sistem bank soalan",$db);
$result = mysql_query("SE LECT * FROM soalan",$db);
$count_emp=mysq l_num_rows($res ult);
if ($count_emp==0)
$id=1;
else
$id=$count_emp+ 1;
echo($id)
?>
[/PHP]
Hi
I attempted to generate ID for my records. It seems that
they really work well when adding record into database. HOwever
problem arises when I tried to delete data that located in the middle
Based on the codes above, the $count_emp variable will return the number of records exist in the database.
The initial value for $id is 0. If there are 10 records for example in the database,($coun t_emp=10), the new value of id would be 11(because $id=$count_emp+ 1).But whenever I want to delete in the middle record, the value of $count_emp will become 9. The new record then will take the last id value which is 10 and this record surely fail to be inserted into the database as there's already exist record with id=10.
Those code might explain you to understand my problem much better. I also need codes that enable the new entry record to replace the location of the deleted record (meaning that the new inserted record would take the id value of the deleted record) and the next new record would then be inserted normally.
Comment