...

View Full Version : Problem with auto_increment



tanhaha_how
01-11-2007, 04:35 PM
hi,i hav a problem that is regarding the auto_increment....

first of all....i hav used --->
$query = "Select * from tblpatient order by Patient_ID desc limit 1";
$newPatientID = $row["Patient_ID"] + 1;
i hav oso set it to auto_increment.

it can display the the ID(last_ID + 1),for example, the last record is 10.after i deleted the last record(by using DELETE statement),the new ID should show 11,but it displays 10 again......

after i click the ADD button,inside the record will show 11....

that means what the new_ID shows is not match with the ID that i inserted...???

why???thanks u very much

actually i hav faced the same problem by using MS SQL last time,then i used IDEN_CURRENT to solve all these problems.....but i'm not familiar with mysql.....hope somebody to help me....thanks!!:o

guelphdad
01-11-2007, 05:04 PM
don't worry about gaps in your auto incremented sequence, it shouldn't matter. if you are using the value to insert into another table then you should use last_insert_id() to fetch the value.

See this article (http://guelphdad.wefixtech.co.uk/sqlhelp/lastinsertid.shtml) for clarification.

tanhaha_how
01-12-2007, 01:56 AM
ooo....i hav the only 1 table....which is tblpatient....then at the new_patientRecord.php,has a textbox to display the new patient ID which is ReadOnly...but the value show inside the textbox is diff with the value stall inside the database....heres my code (new_patientRecord.php):



<?php
$query = "Select * from tblpatient order by Patient_ID desc limit 1";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

$newPatientID = $row["Patient_ID"] + 1;
?>
<form name="form" action="insert_patientRecords.php" method="POST" onSubmit=" return validate(this)">

<td width="108" align="left" valign="Middle">Patient ID:</td>

<td width="173" align="Left" valign="Middle">
<input type="text" readonly="yes" size="10" maxlength="5" name="Patient_ID" style="font-size:10px; font-family:Verdana; width:40px;" value="<?php echo $newPatientID; ?>"></td>



(insert_patientRecord.php)



<?php
include 'connStr.php';

$query ="";

if ($_POST["actionMode"]=="Create"){

$upperSex = strtoupper($_POST["Sex"]);

$query = "Insert into tblpatient(Patient_ID, Treatment_Date, Name, Sex, ICNO, Age, DOB, Address, Postcode, Race, Language_Spoken, Telephone_house, Telephone_office, Handphone, Allergy, Email)values('".$_POST["Patient_ID"]."', '".$date1."', '".$_POST["nam"]."', '".$upperSex."', '".$_POST["ICNO"]."', '".$_POST["Age"]."', '".$date."', '".$_POST["Address"]."', '".$_POST["Postcode"]."', '".$_POST["Race"]."', '".$_POST["Language_Spoken"]."', '".$_POST["Telephone_house"]."', '".$_POST["Telephone_office"]."', '".$_POST["Handphone"]."', '".$_POST["Allergy"]."', '".$_POST["Email"]."');";

$result = mysql_query($query);

mysql_query("COMMIT");
}

echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";

echo "<meta http-equiv=\"REFRESH\" content=\"0; URL=patientRecords.php\">";
?>


now,the only problem is the new patient ID show in textbox is not match with the value after i stall into the database.....somemore i dunno how to use last_insert_id()....can u pls show me inside my code????
thanks for help.....

Linark
01-12-2007, 07:33 AM
Try:

File: insert_patientRecord.php

include 'connStr.php';

$query ="";

if ($_POST["actionMode"]=="Create"){

$upperSex = strtoupper($_POST["Sex"]);

$query = "Insert into tblpatient('', Treatment_Date, Name, Sex, ICNO, Age, DOB, Address, Postcode, Race, Language_Spoken, Telephone_house, Telephone_office, Handphone, Allergy, Email)values('".$_POST["Patient_ID"]."', '".$date1."', '".$_POST["nam"]."', '".$upperSex."', '".$_POST["ICNO"]."', '".$_POST["Age"]."', '".$date."', '".$_POST["Address"]."', '".$_POST["Postcode"]."', '".$_POST["Race"]."', '".$_POST["Language_Spoken"]."', '".$_POST["Telephone_house"]."', '".$_POST["Telephone_office"]."', '".$_POST["Handphone"]."', '".$_POST["Allergy"]."', '".$_POST["Email"]."');";

$result = mysql_query($query);

$patientID = mysql_insert_id()

mysql_query("COMMIT");
}

echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";

echo "<meta http-equiv=\"REFRESH\" content=\"0; URL=patientRecords.php\">";

Basically you can just call mysql_insert_id (http://www.php.net/mysql_insert_id)() after a insert query.

tanhaha_how
01-12-2007, 10:22 AM
ooo...okok...

but then how to pass the value of $PatientID to new_patientRecords.php???
can u pls show me the code?????

thanks a lot!!!!:thumbsup: :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum