PHP concatenate fields

Discussion in 'PHP' started by thorin, May 10, 2007.

  1. #1
    In mySql, can the default value be a combination of other field values ?

    I have three fields (enter via a PHP page), for the year,month and day called :-

    fldYear
    fldMonth
    fldDay

    These are all selected via drop down boxes (to restrict users inputting the wrong date format)and post to a mySql table, but when I display the table on another page, I want to show only the correct date (which will be sortable).

    Does anyone know of a good way to combine these three fields into the one new date field ?
     
    thorin, May 10, 2007 IP
  2. e39m5

    e39m5 Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Of course, you just need to combine the variables with PHP before you insert them into the database.

    First, I would set your column type to DATE. This uses a YYYY-MM-DD format. Then you can use:

    
    $date = $_POST['fldYear'] . '-' . $_POST['fldMonth'] . '-' . $_POST['fldDay'];
    
    PHP:
    Be sure that your drop down values for month and day are set to 01, 02, etc, and not just 1, 2, etc.

    e39m5
     
    e39m5, May 10, 2007 IP
  3. thorin

    thorin Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you, I now have the following section of code :-

    $date = $_POST['fldYear'] . '-' . $_POST['fldMonth'] . '-' . $_POST['fldDay'];
    
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && $word_ok=="yes") {
      $insertSQL = sprintf("INSERT INTO catches (fldYear, fldMonth, fldDay, name, pegno, nofishca, specie, largewght, catchwght) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                           GetSQLValueString($_POST['fldYear'], "int"),
                           GetSQLValueString($_POST['fldMonth'], "int"),
                           GetSQLValueString($_POST['fldDay'], "int"),
                           GetSQLValueString($_POST['name'], "text"),
                           GetSQLValueString($_POST['pegno'], "int"),
                           GetSQLValueString($_POST['nofishca'], "int"),
                           GetSQLValueString($_POST['specie'], "text"),
                           GetSQLValueString($_POST['largewght'], "int"),
                           GetSQLValueString($_POST['catchwght'], "int"));
    
      mysql_select_db($database_roughamlake, $roughamlake);
      $Result1 = mysql_query($insertSQL, $roughamlake) or die(mysql_error());
    
      $insertGoTo = "catch.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
    PHP:
    My field name in the mySql table is called datefished, and is set to DATE format, as I am new to PHP, I am unsure how to update the datefished field in the table, can you help ?
     
    thorin, May 10, 2007 IP
  4. asfi

    asfi Peon

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes you can do this by setting your data field using DATE format
     
    asfi, May 10, 2007 IP
  5. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #5
    alter table table_name change 'field_name' 'field_name' field_type;

    or using phpmyadmin or whatever mysql interface you have.
     
    gibex, May 10, 2007 IP