auto incresement ID

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

  1. #1
    hi there, i having problem in auto increment in my sql database.
    i've tried it but i didn't get error or any data into my database.
    here's the code that i've tried
    i actually wanted my ID to be M001 but the database couldn't allow me to have the varchar for auto increment
    please help!
    thanks in advance

    <!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('mysql_real_escape_string', $_POST);

    $sql="INSERT INTO member (null,MemName, MemAdd, MemContact, MemEmail)
    VALUES
    ('{$_POST[MemID]}','{$_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 11, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    get the latest post and increment it ;)
     
    gapz101, May 11, 2010 IP
  3. lorkan

    lorkan Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I cant see why your first thing should be null...
    $sql="INSERT INTO member (null,MemName, MemAdd, MemContact, MemEmail)
    PHP:
    You still want it to come into the MemID i suspect. so do this:
    $sql="INSERT INTO member (MemID, MemName, MemAdd, MemContact, MemEmail)
    PHP:
    as far as i can see... you have not specified what $_POST['MemID'] is... so therefore... if you use an increment value in the db (as you wrote, it cant be a varchar - must be an init):

    $sql="INSERT INTO member (MemID ,MemName, MemAdd, MemContact, MemEmail)
    VALUES
    ('','{$_POST['MemName']}','{$_POST['MemAdd']}','{$_POST['MemContact']}','{$_POST['MemEmail']}')";
    PHP:
    Note: the first value (that goes into MemID) is empty - meaning that it does not say what it is - this enables your db to have an auto increment number there. Unfortunately, you will not be able to have an M before. I cant see why that should be necessary, otherwise, when you output your MemID value you can always add an M before it - like this:
    echo 'M'.$row['MemID'];
    PHP:
    Good Luck!
     
    lorkan, May 11, 2010 IP
  4. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i've tried the auto increment like what u had wrote but still can't...
    it inserted null into ID field and in the ID field, i used integer for auto increment
    but still can't
    it didn't write into the database nor show any error messages.
    Thanks for the guidances...
     
    yuyue_hirakiseira, May 11, 2010 IP
  5. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Your field should be INT, so give it an int with length variable say, 11. under extras, make it auto_increment (if youre in your phpmyadmin). You don't need to insert a value when you insert new records, it will insert it automatically, and make it 1+ the previous record.
     
    Erind, May 11, 2010 IP
  6. live.co.uk

    live.co.uk Banned

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hay you have an avatar like my one
     
    live.co.uk, May 11, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    <!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> 
    PHP:
     
    danx10, May 11, 2010 IP
  8. lorkan

    lorkan Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    yes, even better!
     
    lorkan, May 11, 2010 IP
  9. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yes it works!!!
    but i wanted my MemID to be M001...
    do you hav any idea on it?


     
    yuyue_hirakiseira, May 11, 2010 IP