hi, i'm having problem getting my desired ID, M001 can anyone please help?? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Name</title> </head> <body> <?php if (isset($_POST['submit'])){ $con = mysql_connect("127.0.0.1","root","2614352") or trigger_error("Connection Failed: ".mysql_error($con), E_USER_ERROR); mysql_select_db("cbfsms", $con); $_POST = array_map('trim', $_POST); if (empty($_POST['MemName'])){ echo "Enter Name..."; } elseif (empty($_POST['MemAdd'])){ echo "Enter Address..."; } elseif (empty($_POST['MemContact'])){ echo "Enter Contact..."; } elseif (empty($_POST['MemEmail'])){ echo "Enter Email..."; } else { $_POST = array_map('mysql_real_escape_string', $_POST); $sql="INSERT INTO member (MemName, MemAdd, MemContact, MemEmail) VALUES ('{$_POST['MemName']}','{$_POST['MemAdd']}','{$_POST['MemContact']}','{$_POST['MemEmail']}')"; if (mysql_query($sql,$con)){ echo "1 record added"; } else { trigger_error("Query Failed: ".mysql_error($con), E_USER_ERROR); } mysql_close($con); } } ?> <form method="post"> Name: <input name="MemName" type="text" /><br /> <br /> Address: <input name="MemAdd" type="text" /><br /> <br /> Email: <input name="MemEmail" type="text" /><br /> <br /> Contact: <input name="MemContact" type="text" /><br /> <br /> <input name="clear" type="button" value="clear" /> <input name="submit" type="submit" value="submit" /> <input name="cancel" type="button" value="cancel" /></form> </body> </html>
simply you can't "auto id" as you put it starts at 1 and increases each time a record is added, you can't change that. However if you really want it you can make the id field of the table varchar or something like it, then you will need to add your own routine to create and insert the desired id. Or do something of similar sort.
I could, you want me to detail it here or by IM or what? I will tell you the most simple way possible. first you need to create another column in the table ideally named "desired_id" to store the desired id. then in your code, once you have inserted the data, take the normal id (say 1) check the number of digits in it, append it to the string 'M' or 'M0' or 'M00' and update the mysql table. if you need more details feel free to PM.
well post it up here so that we can share it out with others if they had the same problem like me next timeXD thanks in advance
Your better off letting mysq take care of id's using integers, and just formatting them for display to the customer if you need any characters in them. eg: id's = 1,2,3,4,5 then display them with something like id = M'. str_pad($id, 4, "0", STR_PAD_LEFT); so you get something like M0001 M0002 etc. then if you are getting the record back form the DB, strip of the M and typecast the number to remove the leading zeros. eg: $id = (int)(substr($id, 1)); they look like M0001 etc, but are really just 1,2,3 etc, and you get mysql to do all the hard work of maintaining the correct auto incremented ids.
well I did above, if you need more details you need to ask where exactly you are stuck, also phpSiteMinder's idea is good.