Joining a String then inserting

Discussion in 'PHP' started by jonathandey, Sep 29, 2010.

  1. #1
    Hey need help please.

    I'm trying to join a sting then insert it in to my database here is what I have.

    
    
    $issue = "1";
    $now = time();
    mysql_query("INSERT INTO general (released, issue) VALUES ('$now', '$issue')");
    
    $subject = "test";		
    $message = $_POST['message'];
    $message .= "<br /><br /><img src='/images/elogo.png' />";
    
    $table = "general";
    
    mysql_query("UPDATE $table SET subject = '$subject', message = '$message' WHERE released = '$now'");
    
    PHP:
    The update query doesn't add anything however the inser query does.

    Basically how do I insert the joined string $message?

    Thanks!
     
    jonathandey, Sep 29, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Your concat'd string has single quotes in it, which is breaking the insert.

    You should escape the data, where possible too. Something like this will fix the problem, though you might also want to do this on subject (and ANY other user entered data).

    mysql_query("UPDATE $table SET subject = '$subject', message = '".mysql_real_escape_string($message)."' WHERE released = '$now'");
    PHP:
    As a side note, you might also want to consider using mysql_error to check for errors in tthings
    like this; it might save you a lot of time.


    mysql_query("UPDATE $table SET subject = '$subject', message = '".mysql_real_escape_string($message)."' WHERE released = '$now'") or die(mysql_error());
    PHP:
     
    lukeg32, Sep 29, 2010 IP
  3. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Sir I take my hat off to you!

    That worked a treat thanks for the help and advice!
     
    jonathandey, Sep 29, 2010 IP