Combining fields, then splitting them when sending to database

Discussion in 'PHP' started by andreyp, Mar 22, 2011.

  1. #1
    Hi all,

    I have a PHP form that sends data to live DB and I have combined first and last name fields into 1 field (Full Name). However when the data is send to database I need to split the Full Name into x_C1Firstname and x_C1Lastname fields so they are stored properly in the DB.

    So for example, if user enters John Smith as Full Name and presses Submit, I need to add "John" to x_C1Firstname and "Smith" to x_C1Lastname.

    Any advice is appreciated.

    Thanks
     
    andreyp, Mar 22, 2011 IP
  2. AlesiBoss

    AlesiBoss Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use two fields or do it with explode() fucntion
     
    AlesiBoss, Mar 23, 2011 IP
  3. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #3
    preg_split or explode, right. They both return an array and you need to access each index separately. Instead, you can list them in variables.

    
    list($x_C1Firstname , $x_C1Lastname) = preg_split('/\s/', $_POST['x_C1Firstname']);
    
    Code (markup):
     
    Vooler, Mar 23, 2011 IP
  4. andreyp

    andreyp Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    thank you. got it sorted.
     
    andreyp, Mar 23, 2011 IP