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 insert array into database

Discussion in 'PHP' started by ingilizdili, Aug 28, 2011.

  1. #1
    Hello;
    I want to turn paragraphs into sentences which I do with
    $parag = $_POST['parag'];
    
    $sent = explode(".", $parag);
    PHP:

    Now the problem is how to insert these sentences into separate mysql rows.
    I used the following
    mysql_query("INSERT INTO metin_cum
    (sent) VALUES('$sent[0]'), ('$sent[1]'), ('$sent[2]'), ('$sent[3]'), ('$sent[4]'), ('$sent[5]'), ('$sent[6]'), ('$sent[7]'), ('$sent[8]'), ('$sent[9]'), ('$sent[10]'), ('$sent[11]'), ('$sent[12]'), ('$sent[13]'), ('$sent[14]'), ('$sent[15]'), ('$sent[16]'), ('$sent[17]'), ('$sent[18]'), ('$sent[19]'), ('$sent[20]') ")
    or die(mysql_error());
    PHP:
    I need a code which will know how many new rows will be needed for each new paragraph. What if I want to insert a paragraph or page with 22 sentences?
    Please help.
     
    Solved! View solution.
    ingilizdili, Aug 28, 2011 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    
    $sqlquery="INSERT INTO metin_cum
    (sent) VALUES'";
    for($i=0;$i<count($sent)-1;$i++)
    {
     $sqlquery.="('".$sent[$i]."'),"
    }
    $sqlquery.="('".$sent[count($sent)]."')";
    mysql_query($sqlquery)or die(mysql_error());
    
    PHP:
     
    ssmm987, Aug 28, 2011 IP
  3. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry but that doesn't seem to work
     
    ingilizdili, Aug 28, 2011 IP
  4. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #4
    True

    $sqlquery.="('".$sent[count($sent)]."')";
    PHP:
    Should be

    $sqlquery.="('".$sent[count($sent)-1]."')";
    PHP:
    If it still doesn't work, at least provide some information about what doesn't work. (Echo the sqlquery variable)
     
    ssmm987, Aug 28, 2011 IP
  5. Ciprian

    Ciprian Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Try to echo $sqlquery and post it here. Might be some errors in there. Usually small.
     
    Ciprian, Aug 28, 2011 IP
  6. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well, here is all I can get
    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 ''('Then he went to the gallows, sat beneath them and waited until the evening'),' at line 2
    HTML:
    The paragraph is posted and exploded. No problem here. Below is the whole code
    
    $parag = $_POST['parag'];
    
    
    $sent = explode(".", $parag);
    
    include('connect.php');
    
    $sqlquery="INSERT INTO metin_cum
    (sent) VALUES'";
    for($i=0;$i<count($sent)-1;$i++)
    {
     $sqlquery.="('".$sent[$i]."'),";
    }
    $sqlquery.="('".$sent[count($sent)-1]."')";
    mysql_query($sqlquery)or die(mysql_error());
    
    
    
    PHP:
     
    Last edited: Aug 28, 2011
    ingilizdili, Aug 28, 2011 IP
  7. #7
    What about replacing this:

    $sqlquery = "INSERT INTO metin_cum (sent) VALUES'";
    PHP:
    with this:

    $sqlquery = "INSERT INTO metin_cum (sent) VALUES";
    PHP:
    Notice I removed the apostrophe (single quote) after VALUES.
     
    Ciprian, Aug 29, 2011 IP
  8. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Now it works. Thank you very much.
     
    ingilizdili, Aug 29, 2011 IP
  9. Ciprian

    Ciprian Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #9
    You're welcome!
     
    Ciprian, Aug 29, 2011 IP
  10. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    This time I am having an issue with the quotation marks. The parts of the sentences following a quotation mark (") or a (') are omitted.
     
    ingilizdili, Sep 1, 2011 IP
  11. Ciprian

    Ciprian Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #11
    Paste the echoed SQL query here, so we can debug it.
     
    Ciprian, Sep 1, 2011 IP
  12. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Here is an example;
    Input text:
    A father had two sons. The older son was clever and could do everything. But the younger one was stupid and could not understand or learn anything. When people saw him, they said, “He will be a burden on his father.”
    When there was something to do, the older son did it. However, when the father asked him to go somewhere at night, he would feel afraid and say, “No, father. I can’t go there. It makes me shudder.”

    Mysql output:
    A father had two sons
    The older son was clever and could do everything
    But the younger one was stupid and could not understand or learn anything
    When people saw him, they said,
    However, when the father asked him to go somewhere at night, he would feel afraid and say,
    It makes me shudder
    I can


    And there are some empty rows.
    And here is the echo $sql output:
    (' A father had two sons'),(' The older son was clever and could do everything'),(' But the younger one was stupid and could not understand or learn anything'),(' When people saw him, they said, “He will be a burden on his father'),('” When there was something to do, the older son did it'),(' However, when the father asked him to go somewhere at night, he would feel afraid and say, “No, father'),(' I can’t go there'),(' It makes me shudder'),('” '),('')
     
    ingilizdili, Sep 1, 2011 IP
  13. Ciprian

    Ciprian Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #13
    Well, if you have a query like this:

    mysql_query($yourqueryhere);
    Code (markup):
    Try adding addslashes() to it;

    mysql_query(addslashes($yourqueryhere));
    Code (markup):
    This will escape all single and double quotes.
     
    Ciprian, Sep 1, 2011 IP
  14. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #14
    What code is putting in the single quotes and parentheses around the commas? That's one problem.

    Another one seems to be your lack of understanding of when to use which quotation mark (single or double) in PHP - they're not the same, and if you're creating a SQL string they have to be correct or you're telling MySQL something you don't want to.

    Edit:
    I missed that this is an explode. Still, you have to clean up the string because your single and double quotes are being thrown into the wrong places. mysql_real_escape_string() will help, but you still have to make sure that you're ending up with the SQL string you want.
     
    Rukbat, Sep 1, 2011 IP
  15. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Well, the problem has been solved but in an indirect way. First, I edited the input and replaced the (") and (') marks with "{" and "}" marks, which are not often used in texts. The sentences were inserted into the database in this way. When I retrieve the sentences for use, I str_replace them accordingly. Thank you very much for your kind efforts to help.
     
    Last edited: Sep 2, 2011
    ingilizdili, Sep 2, 2011 IP