Problem of php insert into

Discussion in 'PHP' started by youlichika, Nov 1, 2010.

  1. #1
    1. I want to insert something into database. They all with time and date, the time type is like 'day month year time'
    26 October 2010, 08:16
    29 October 2010, 13:45
    31 October 2010, 1:02 pm
    30 October 2010, 11:54 am
    Some time and date are 24-hour, some are 12-hour.
    How to use PHP code transform them to a database time type? for that I can run PHP mysql query be sorted by time?

    2. I do not want to insert duplicate data, I use below code, but failed. How to solve? Thanks.

    mysql_query("INSERT INTO table (name,title,date) VALUES ('".$name."','".$title."','".$date."')" where title not in (select title from table) and name not in (select name from table));
    
    HTML:
     
    youlichika, Nov 1, 2010 IP
  2. kai555

    kai555 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi

    look at the date() and time() functions to format your date.
    and strtotime() function to get a timestamp to be used by the above to functions
     
    kai555, Nov 1, 2010 IP
  3. KingOle

    KingOle Peon

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Date is a keyword for mysql and so it's probably throwing an error because of that. Change date to `date` with ticks to avoiod this problem.

    
    mysql_query("INSERT INTO table (name,title,`date`) VALUES ('".$name."','".$title."','".$date."')" where title not in (select title from table) and name not in (select name from table));
    
    Code (markup):
     
    KingOle, Nov 1, 2010 IP
  4. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    Thanks KingOle, I tried `date`, but it also failed. when i use
    mysql_query("INSERT INTO table (name,title,`date`) VALUES ('".$name."','".$title."','".$date."')");
    Code (markup):
    it worked.
    use
    mysql_query("INSERT INTO table (name,title,`date`) VALUES ('".$name."','".$title."','".$date."') where title not in (select title from table) and name not in (select name from table)" );
    Code (markup):
    it failed, it can not insert anyone, if I empty my database.

    And... do you know the first question? How to transform my time to a database time type?
     
    youlichika, Nov 1, 2010 IP
  5. lomb

    lomb Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sounds like you have not reinstated the fields after dropping your database thats why you cant add the fields.

    As for time add a column called itime then use this code

    $itime=time();

    Then use SQL sort by to order you database.
     
    lomb, Nov 1, 2010 IP
  6. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    could you add
    or die(mysql_error())
    PHP:
     
    themullet, Nov 1, 2010 IP
  7. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    Yes,
    $db = mysql_connect("localhost","root","root") or dir("can not connect Mysql Server");
    mysql_select_db("table",$db) or dir("can not connect Mysql Server");
    I think I write a wrong code...
     
    youlichika, Nov 2, 2010 IP