Insert 'time' mysql problem

Discussion in 'MySQL' started by Amilo, May 27, 2007.

  1. #1
    I am having problems inserting times in to a mysql database.

    I can enter a time through cpanel without problems so i know the database is ok.

    My problem is when using a php form,code is below:

    insert.php

    mysql_select_db("mydatabase_name", $con);
    
    $sql="INSERT INTO db (event ,location ,town ,date ,time ,text ,type ,)
    VALUES
    ('$_POST[event]','$_POST[location]','$_POST[town]','$_POST[date]','$_POST[time]','$_POST[text]','$_POST[type]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
    Code (markup):
    insert.html:

    <html>
    <body>
    
    <form action="insert.php" method="post">
    event: <input type="text" name="event" />
    location: <input type="text" name="location" />
    town: <input type="text" name="town" />
    date: <input type="text" name="date" />
    time: <input type="text" name="time" />
    text: <input type="text" name="text" />
    type: <input type="text" name="type" />
    <input type="submit" />
    </form>
    
    </body>
    </html>
    Code (markup):
    This is the error I get:

    Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'text ,type ,) VALUES ('smith','church','manchester','02-09-2007','18' at line 1
     
    Amilo, May 27, 2007 IP
  2. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #2
    Mysql date type is Y-m-d, you have to convert form data_field into correct format.

    
    $new_date_array = split("-", $_POST['date']); // current date is d-m-Y
    $new_date = "{$new_date_array[2]}-{$new_date_array[1]}-{$new_date_array[0]}";
    
    PHP:
    You can now safely insert $new_date in your table.
     
    gibex, May 28, 2007 IP
  3. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Could someone show me the code inserted in to my code.
    I cant get it to work ?

    I also imputed the date as yyyy/mm/dd and it still chocked...
     
    Amilo, May 28, 2007 IP
  4. Clark Kent

    Clark Kent Guest

    Messages:
    122
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    the problem is "comma" after fieldname type: " ,text ,type ,)"
    delete that comma (,) or write a field name after that.
     
    Clark Kent, May 28, 2007 IP