MySQL; Is it possible to have letters in an auto increment field??

Discussion in 'MySQL' started by tyler_durden, Apr 24, 2007.

  1. #1
    I am stuck in an odd dilemma. I have a script that I am trying to modify for use in a CMS, unfortunately the CMS does not allow me to use a field name as a number, which the script uses.

    I have a field in the table named 'ID', which auto increments. I want to add 'ID_' at the beginning of each new entered ID field so I can work around the above problem. The script uses the ID # as a field name, so instead of name="15", it will be shown as name="id_15". I hope that makes sense.

    Is this possible in MySQL? If not, i'm thinking there has to be a way in the PHP code when posting the info, that it adds it somehow?
     
    tyler_durden, Apr 24, 2007 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    You can't do that...

    echo "ID_" . $field_id;
    PHP:
     
    SoKickIt, Apr 24, 2007 IP
  3. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    echo "ID_" . $field_id;
    Code (markup):
    I have implemented this, unfortunately the form i'm using is now trying to modify the "ID_15" record, which of course does not exist.

    Am I SOL, and have to redesign this program completely?
     
    tyler_durden, Apr 24, 2007 IP
  4. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
     if($bought > 0){
          array_push($ar, $iid . "=" . $bought);
          $query = "insert into purchaseHistory (iid, username, quantity, boughtDate) values (" . 
            $iid . ", '" . $myusername . "', " . $bought . ", NOW())";
          $rs = mysql_query($query) or die("Could not query2: " . mysql_error());
        }         
      }
    Code (markup):
    Here is some code - the $iid is the actual variable changed. Is there anyway in the code above to strip the "ID_" out of the $iid variable before posting, so the actual number is just inserted?

    I know it seems odd, but it is the only workaround I can think of.
     
    tyler_durden, Apr 24, 2007 IP
  5. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #5
    There are many ways to do that. Here's one:

    $iid = substr($iid, 3);
    PHP:
     
    SoKickIt, Apr 24, 2007 IP
  6. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks SoKickIt. I found this solution to work after googling it.

    $iid = preg_replace('/[^0-9]/','',$iid);
    Code (markup):
    I know nothing about security w/PHP - is this just as good as the code you posted?

    Thanks again for everything!
     
    tyler_durden, Apr 24, 2007 IP
  7. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #7
    That would remove all non-numeric characters so that's exactly what you need ;)
     
    SoKickIt, Apr 24, 2007 IP