1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to save several sentences to MySQL separately from each other?

Discussion in 'PHP' started by FokeBox, Mar 24, 2013.

  1. #1
    Hello all! I use a form where I enter some text consisting of several sentances! How can I save each sentance to MySQL separately?
    I use this php script, but it saves all text that was entered to the form

    $query = "INSERT INTO mytable (table1) VALUES ('$textform')" ;
    Thx for help!
    FokeBox, Mar 24, 2013 IP
  2. crazyblogger

    crazyblogger Active Member

    Messages:
    433
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    63
    #2
    By separately you mean in new line? If that's the case, you can use php built in function called nl2br which converts new line in text field to break tags.
    Use it like this,
    $text= nl2br($text);
    now put this into the database. To echo out the formatted text use following

    echo nl2br($text);
    crazyblogger, Mar 24, 2013 IP
  3. FokeBox

    FokeBox Active Member

    Messages:
    399
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    65
    #3
    No no ... I mean each sentence to new row in the mysql table. I heard that I need to use explode function! But how? I have no idea!
    FokeBox, Mar 24, 2013 IP
  4. crazyblogger

    crazyblogger Active Member

    Messages:
    433
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    63
    #4
    Here is a simple example that might work.

    $mytext = "this a first sentence. This is second. This is third";
    $onesentence = explode(".", $mytext);
    echo $onesentence[0];
    echo $onesentence[1];
    echo $onesentence[2];

    In your case you will store $onesentence[0] and other two in database row.
    crazyblogger, Mar 24, 2013 IP
  5. FokeBox

    FokeBox Active Member

    Messages:
    399
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    65
    #5
    I know this simple examples, but I need help how to make it with query
    Here it is my query which saves all that was in the input form:
    $query = "INSERT INTO mytable (table1) VALUES ('$textform')" ;
    So could you help me?
    FokeBox, Mar 24, 2013 IP
  6. crazyblogger

    crazyblogger Active Member

    Messages:
    433
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    63
    #6
    loop through the exploded array and insert it into the table. I think that's not too hard.
    crazyblogger, Mar 24, 2013 IP
  7. FokeBox

    FokeBox Active Member

    Messages:
    399
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    65
    #7
    Well I am just beginner and started to learn php! So could you help me?
    FokeBox, Mar 24, 2013 IP
  8. crazyblogger

    crazyblogger Active Member

    Messages:
    433
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    63
    #8
    PHP:
    1. $mytext = "this a first sentence. This is second. This is third";
    2. $onesentence = explode(".", $mytext);
    3.  
    4. foreach($onesentence as $sentence) {
    5. mysql_query("INSERT INTO table tablecol value '$sentence'");
    6.  
    7. }
    crazyblogger, Mar 24, 2013 IP
  9. FokeBox

    FokeBox Active Member

    Messages:
    399
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    65
    #9
    PHP:
    1. $form1 = explode(". ", $mytext);
    2. $form2 = explode(". ", $mytext2);
    3. $query = "INSERT INTO enru (table1, table2) VALUES ('$mytext', '$mytext2')" ;
    4. $result = mysql_query( $query ) or die( mysql_error() ) ;
    So I did so, but it does not work!
    FokeBox, Mar 24, 2013 IP