Trying to concatenate 2 fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bobf
    New Member
    • Feb 2008
    • 11

    Trying to concatenate 2 fields

    I am trying to create a fullname out of first and last name fields. Am I better to do this inside of constructing the PHP $query string? My current method isn't working for me.


    [HTML] <tr>
    <td class="label">F irst name:</td>
    <td class="field">
    <input type="text" name="first_nam e" size="20" maxlength="50" value="<?php if (isset($_POST['first_name'])) { echo htmlChars($_POS T['first_name']); } ?>">
    </td>
    </tr>
    <tr>
    <td class="label">L ast name:</td>
    <td class="field">
    <input type="text" name="last_name " size="20" maxlength="50" value="<?php if (isset($_POST['last_name'])) { echo htmlChars($_POS T['last_name']); } ?>">
    </td>
    </tr>
    <tr>
    <td class="field" name="fullname" value="<?php $first_name. " ".last_name ?>">
    </td>
    </tr>
    [/HTML]
    My $query string is constructed
    [PHP]
    ..........
    $query .= "'" . prepareData($_P OST['fullname']) . "'" . ',';
    $query .= "'" . prepareData($_P OST['first_name']) . "'" . ',';
    $query .= "'" . prepareData($_P OST['last_name']) . "'" . ',';
    [/PHP]

    I would appreciate some clues.
    Last edited by bobf; Mar 3 '08, 09:20 PM. Reason: added php string
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    [php]
    $_fullname = $_firstName . " " . $_lastName;
    [/php]
    I dont see what the problem is with your code.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      You have 2 errors in 1 statement[php] <td class="field" name="fullname" value="<?php $first_name. " ".last_name ?>">[/php]1. last_name is a variable so prefix it with a dollar, e.g. $last_name.

      2. you must echo the variables, otherwise you'll get nothing.

      So the statement must be[php] <td class="field" name="fullname" value="<?php echo $first_name. " ".$last_name?>" >[/php]Ronald

      Comment

      • bobf
        New Member
        • Feb 2008
        • 11

        #4
        Thank you gentlemen.
        I had 2 replies, the first suggested:-
        Code: (php)
        $_fullname = $_firstName . " " . $_lastName;
        (/php)
        The 2nd I copied
        Code: (php)
        <td class="field" name="fullname" value="<?php echo $first_name. " ".$last_name?>" >
        (/php)
        I had missed the $ on last_name.
        Am I right in thinking that the 1st should be included in my $query string? If so can I just drop it in the middle?
        The 2nd I tried but it did not work for me. I have checked my spelling and it appears OK.
        I have to own up that this is not my own script but it has been generated by a routine. I have noticed that where they have included echo in other parts they have used a lot more qoutes and brackets:-
        Code: (php)
        { echo htmlChars($_POS T['last_name']); }

        Comment

        • mageswar005
          New Member
          • Mar 2008
          • 72

          #5
          Originally posted by bobf
          Thank you gentlemen.
          I had 2 replies, the first suggested:-
          Code: (php)
          $_fullname = $_firstName . " " . $_lastName;
          (/php)
          The 2nd I copied
          Code: (php)
          <td class="field" name="fullname" value="<?php echo $first_name. " ".$last_name?>" >
          (/php)
          I had missed the $ on last_name.
          Am I right in thinking that the 1st should be included in my $query string? If so can I just drop it in the middle?
          The 2nd I tried but it did not work for me. I have checked my spelling and it appears OK.
          I have to own up that this is not my own script but it has been generated by a routine. I have noticed that where they have included echo in other parts they have used a lot more qoutes and brackets:-
          Code: (php)
          { echo htmlChars($_POS T['last_name']); }


          <?php
          $firstname="hel lo";
          $lastname="worl d";
          $result=$firstn ame.$lastname;
          echo $result;
          ?>

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by bobf
            Am I right in thinking that the 1st should be included in my $query string? If so can I just drop it in the middle?
            You have not shown how the querystring should look like and what it is you are selecting or inserting or updating. In order to see what the query string should look like, you'd better show the entire query string.

            Ronald

            Comment

            • bobf
              New Member
              • Feb 2008
              • 11

              #7
              Sorry Guys
              I think I had you all going the wrong way.
              I realised I had the information I just needed to post it to MySQL
              Code: (php)
              $query .= "'" . prepareData($_P OST['first_name']) . " " .prepareData($_ POST['last_name']) ."'" . ',';
              $query .= "'" . prepareData($_P OST['first_name']) . "'" . ',';
              $query .= "'" . prepareData($_P OST['last_name']) . "'" . ',';
              (/php)

              Resolved it for me.
              Thank you for your time and patience.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Glad it is solved. See you again next time.

                Ronald

                Comment

                • honasito

                  #9
                  concatenating variables

                  $month=$_POST['month'];
                  $day=$_POST['day'];
                  $year=$_POST['year'];
                  $age=$month." ".$day." ".$year;

                  Comment

                  Working...