vikrantmohite Posted June 10, 2012 Share Posted June 10, 2012 I am not able to insert data into table. I have some data in array. If I use echo array[index] I can see the data. But when I try to insert the data in database I am getting blank data in table. Can you please help me guys. Quote Link to comment https://forums.phpfreaks.com/topic/263939-data-insertion/ Share on other sites More sharing options...
Pikachu2000 Posted June 10, 2012 Share Posted June 10, 2012 The problem is on line 41. Quote Link to comment https://forums.phpfreaks.com/topic/263939-data-insertion/#findComment-1352637 Share on other sites More sharing options...
vikrantmohite Posted June 10, 2012 Author Share Posted June 10, 2012 The problem is on line 41. Which line?? Quote Link to comment https://forums.phpfreaks.com/topic/263939-data-insertion/#findComment-1352639 Share on other sites More sharing options...
insidus Posted June 10, 2012 Share Posted June 10, 2012 We need to see the code that you use for making the array, and the code for when you insert into the database table, else we can't really help. Quote Link to comment https://forums.phpfreaks.com/topic/263939-data-insertion/#findComment-1352641 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2012 Share Posted June 10, 2012 I rather thought the problem was line 42 (i.e. http://en.wikipedia.org/wiki/42_Puzzle#42_Puzzle ) The reason for Pikachu2000's answer is that you posted no relevant information upon which to tell you what is wrong in your code, so you got an accurate as possible answer (i.e. a guess) based on the information supplied. Programming is an exact science. Computers only do exactly what their code and data tells them to do. We cannot tell you what your code and data are doing without seeing all the code and data needed to reproduce the problem. We are not standing right next to you and don't know what your code and data is. Quote Link to comment https://forums.phpfreaks.com/topic/263939-data-insertion/#findComment-1352646 Share on other sites More sharing options...
vikrantmohite Posted June 10, 2012 Author Share Posted June 10, 2012 We need to see the code that you use for making the array, and the code for when you insert into the database table, else we can't really help. index.php <?php # don't forget the library include('simple_html_dom.php'); # this is the global array we fill with article information $trains = array(); # passing in the first page to parse, it will crawl to the end # on its own getArticles('http://www.makemytrip.com/railways/mumbai_sawantwadi_s-01013-train.html'); function getArticles($page) { global $trains, $con; $html = new simple_html_dom(); $html->load_file($page); $con = mysql_connect("localhost","root",""); mysql_select_db("temp",$con); /* foreach($html->find('table tr.segonroute1') as $e) { //$details[] = $e->children(1)->outertext; //echo $e->children(1)->outertext; } */ $route = $html->find('table tr.segonroute1'); foreach($route as $key) { $trains[]=array( $key->children(1)->plaintext, $key->children(2)->plaintext, $key->children(3)->plaintext, $key->children(4)->plaintext, $key->children(5)->plaintext, $key->children(6)->plaintext ); //mysql_query("INSERT INTO trains (station_name, arrives, departs, halt_time, distance, train_no) VALUES ('".$key->children(1)->outertext.""); } } foreach($trains as $item) { echo $item[0]; $s = (string) $item[0]; echo " ".$item[1]; echo " ".$item[2]; echo " ".$item[3]; echo " ".$item[4]; echo " ".$item[5]."<br/>"; //mysql_query("INSERT INTO trains (station_name, arrives, departs, halt_time, distance, train_no) VALUES ('$item[0]','$item[1]','$item[2]','$item[3]','$item[5]',01013"); $q="INSERT INTO trains (station_name) VALUES ('$s')"; mysql_query($q,$con); } ?> simple_html_dom.php is attached here . 18569_.php Quote Link to comment https://forums.phpfreaks.com/topic/263939-data-insertion/#findComment-1352649 Share on other sites More sharing options...
vikrantmohite Posted June 10, 2012 Author Share Posted June 10, 2012 Can anyone suggest me what I am doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/263939-data-insertion/#findComment-1352662 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2012 Share Posted June 10, 2012 What exactly is the query? Since you are not escaping the data being put into it, it might have special sql characters that are breaking the sql syntax. Echo the $q variable and post the result. Is your query failing due to an error of some kind? For debugging purposes, change your mysql_query line of code to - mysql_query($q,$con) or die("Query failed: $q<br />Error: " . mysql_error($con)); Quote Link to comment https://forums.phpfreaks.com/topic/263939-data-insertion/#findComment-1352692 Share on other sites More sharing options...
vikrantmohite Posted June 11, 2012 Author Share Posted June 11, 2012 What exactly is the query? Since you are not escaping the data being put into it, it might have special sql characters that are breaking the sql syntax. Echo the $q variable and post the result. Is your query failing due to an error of some kind? For debugging purposes, change your mysql_query line of code to - mysql_query($q,$con) or die("Query failed: $q<br />Error: " . mysql_error($con)); Thanks PFMaBiSmAd I got the solution I was getting a lot of white spaces while inserting data so data was not getting inserted into the table. I used trim() to remove white spaces and now everything is workin fine. Thanks to all for your support and response. Quote Link to comment https://forums.phpfreaks.com/topic/263939-data-insertion/#findComment-1352842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.