desired auto ID

Discussion in 'PHP' started by yuyue_hirakiseira, May 20, 2010.

  1. #1
    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 />
    &nbsp;&nbsp;&nbsp; <input name="clear" type="button" value="clear" />&nbsp;&nbsp;&nbsp;
    <input name="submit" type="submit" value="submit" />&nbsp;&nbsp;&nbsp;
    <input name="cancel" type="button" value="cancel" /></form>

    </body>

    </html>
     
    yuyue_hirakiseira, May 20, 2010 IP
  2. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #2
    What errors are you seeing? What exactly are you trying to do? Please give more information...
     
    mfscripts, May 20, 2010 IP
  3. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i want my ID to be M001 but i get 1
    how can i get the desire auto ID
     
    yuyue_hirakiseira, May 20, 2010 IP
  4. kaleshwar

    kaleshwar Peon

    Messages:
    43
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    kaleshwar, May 20, 2010 IP
  5. kaleshwar

    kaleshwar Peon

    Messages:
    43
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    double post
     
    kaleshwar, May 20, 2010 IP
  6. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if that's the way, could you please teach me how to do it???
    thanks in advance
     
    yuyue_hirakiseira, May 20, 2010 IP
  7. kaleshwar

    kaleshwar Peon

    Messages:
    43
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    kaleshwar, May 21, 2010 IP
  8. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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
     
    yuyue_hirakiseira, May 23, 2010 IP
  9. phpSiteMinder

    phpSiteMinder Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    phpSiteMinder, May 23, 2010 IP
  10. kaleshwar

    kaleshwar Peon

    Messages:
    43
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    well I did above,
    if you need more details you need to ask where exactly you are stuck, also phpSiteMinder's idea is good.
     
    kaleshwar, May 23, 2010 IP