dino_angelides
asked on
Autoincrement with prefix and update after deletion
Hi, I have a table with some fields and 2 of them are:
ID int(10) PK AI
RefNum varchar(50)
I want the reference number to start with the prefix RE-001 and I want everytime i insert a new record to increment it accordingly
e.g
ID RefNum
1 RE-001
2 RE-002
3 RE-003
How can this be done, i am aware that auto increment can be done on INT but maybe there is another way
ID int(10) PK AI
RefNum varchar(50)
I want the reference number to start with the prefix RE-001 and I want everytime i insert a new record to increment it accordingly
e.g
ID RefNum
1 RE-001
2 RE-002
3 RE-003
How can this be done, i am aware that auto increment can be done on INT but maybe there is another way
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, for reference, here is the MySQL table.
CREATE TABLE `tbltest` (
`ID` int(10) NOT NULL auto_increment,
`refNum` varchar(50) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `tbltest`
--
INSERT INTO `tbltest` (`ID`, `refNum`) VALUES
(1, 'RE-001'),
(2, 'RE-002');
ASKER
well, let me try this one later :)
ASKER
I did not get a chance to try that yet.....
my insert code, which derived from dreamweaver insert behavior is below in the snippet....where should I add the code you provided?
also there is a textbox of the Reference Number which is readonly
<input name="refNumber" type="text" class="style811" id="refNumber" size="50" maxlength="50" readonly="readonly" />
and I want to display the next available reference number in there so that the user cannot change it!!
my insert code, which derived from dreamweaver insert behavior is below in the snippet....where should I add the code you provided?
also there is a textbox of the Reference Number which is readonly
<input name="refNumber" type="text" class="style811" id="refNumber" size="50" maxlength="50" readonly="readonly" />
and I want to display the next available reference number in there so that the user cannot change it!!
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
$insertSQL = sprintf("INSERT INTO listings (id, datePosted, featured, listingType, refNum, contactName, contactPhone, contactMobile, contactEmail, region, area, address, price, priceRequested, beds, baths, parking, propType, coveredArea, pool, furnished, deeds, postedBy, deliveryDate, mapLocation, briefComments, comments, image1, image2, image3, image4, image5, image6, image7, image8) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['datePosted'], "text"),
GetSQLValueString(isset($_POST['featured']) ? "true" : "", "defined","'Yes'","'No'"),
GetSQLValueString($_POST['listingType'], "text"),
GetSQLValueString($_POST['refNumber'], "text"),
GetSQLValueString($_POST['contactName'], "text"),
GetSQLValueString($_POST['contactPhone'], "text"),
GetSQLValueString($_POST['contactMobile'], "text"),
GetSQLValueString($_POST['contactEmail'], "text"),
GetSQLValueString($_POST['region'], "text"),
GetSQLValueString($_POST['area'], "text"),
GetSQLValueString($_POST['address'], "text"),
GetSQLValueString($_POST['price'], "text"),
GetSQLValueString($_POST['sellerPrice'], "text"),
GetSQLValueString($_POST['bedrooms'], "text"),
GetSQLValueString($_POST['bathrooms'], "text"),
GetSQLValueString($_POST['parking'], "text"),
GetSQLValueString($_POST['propertyType'], "text"),
GetSQLValueString($_POST['coveredArea'], "text"),
GetSQLValueString($_POST['pool'], "text"),
GetSQLValueString($_POST['furnished'], "text"),
GetSQLValueString($_POST['deeds'], "text"),
GetSQLValueString($_POST['postedBy'], "text"),
GetSQLValueString($_POST['deliveryDate'], "text"),
GetSQLValueString($_POST['maplocation'], "text"),
GetSQLValueString($_POST['commentsBrief'], "text"),
GetSQLValueString($_POST['comments'], "text"),
GetSQLValueString($_POST['image1'], "text"),
GetSQLValueString($_POST['image2'], "text"),
GetSQLValueString($_POST['image6'], "text"),
GetSQLValueString($_POST['image6'], "text"),
GetSQLValueString($_POST['image5'], "text"),
GetSQLValueString($_POST['image6'], "text"),
GetSQLValueString($_POST['image7'], "text"),
GetSQLValueString($_POST['image8'], "text"));
mysql_select_db($database_RealEstateOnlineCy, $RealEstateOnlineCy);
$Result1 = mysql_query($insertSQL, $RealEstateOnlineCy) or die(mysql_error());
$insertGoTo = "/RealEstateOnline/admin/addListingSuccess.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
Here's the docu to trigger: http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html