Search code examples
phpmysqlsimple-html-dom

PHP Simple HTML DOM Parser array to database


So the problem is, when uploading to mysql the database is only uploading the last value in the array.

                        if (substr($avsnitt["serier"], 0, 16) === 'http://random'){

                                        // Create DOM from URL or file
                            $html = file_get_html($avsnitt["serier"]);


                            $array_title = array();
                            $array_link = array();

                            foreach($html->find('div[class=entry]') as $element){ 

                                foreach ($element->find('a') as $text) {
                                    $array_title[] = $text->plaintext;

                                }
                                foreach ($element->find('a') as $test) {
                                    $array_link[] = $test->href;
                                }


                                    $count_name = count($array_title);

                                    for($i=0; $i<$count_name; $i++){
                                        $_array_title  = mysql_escape_string($array_title[$i]);
                                        $_array_link  = mysql_escape_string($array_link [$i]);

                                        print_r($_array_title);
                                        print_r($_array_link);

                                        $sql2 = "INSERT INTO episodes (name, ID, link) VALUES ('" . @$_array_title. "','" . @$avsnitt["ID"] . "', '" . @$_array_link . "');";
                                        mysqli_query($CON, $sql2);
                                    }


                                }    
                        }

i'm new to php mysql so i dont relly know how arrays is uploaded to mysql, fast answers will be appreciated thanks.


Solution

  • problem was the database ID was primery key only allowing one withe a specific ID