shage Posted July 31, 2008 Share Posted July 31, 2008 Im trying to loop and insert into mysql results but i only get 1 result to the db, even if i have 1000 lines to import, any help would be wonderful. mysql_connect("", "", "") or die(mysql_error()); mysql_select_db("poster") or die(mysql_error()); $blogs = $_POST['blogs']; $breakblogs = explode("\n", $_POST['blogs']); $count = count($breakblogs); for ($i=0; $i<=$count; $i=$i+1) { $shage = $breakblogs[$i]; $shage2 = explode("|", $shage); if($shage2[0]==''){ break; } $query = "INSERT INTO blogs (url, posturl, user, password, type, cat) VALUES ('$shage2[0]', '$shage2[1]', '$shage2[2]', '$shage2[3]', '$shage2[4]', '$shage2[5]')"; mysql_query($query); } } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 31, 2008 Share Posted July 31, 2008 comment out your loop, and right before it, do this: echo count($breakblogs); Quote Link to comment Share on other sites More sharing options...
shage Posted July 31, 2008 Author Share Posted July 31, 2008 echos 10 which is amount of lines im trying to send Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 31, 2008 Share Posted July 31, 2008 Try without the if statement, and see what happens: foreach ($breakblogs as $shage){ $shage2 = explode("|", $shage); $query = "INSERT INTO blogs (url, posturl, user, password, type, cat) VALUES ('$shage2[0]', '$shage2[1]', '$shage2[2]', '$shage2[3]', '$shage2[4]', '$shage2[5]')"; mysql_query($query); } Quote Link to comment Share on other sites More sharing options...
shage Posted July 31, 2008 Author Share Posted July 31, 2008 submits still just 1 line Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 Your poor database! Why not do it all in a single query? <?php mysql_connect( 'localhost', 'root', '' ); $sampleData = <<<HEREDOC some|data|to|insert|here|foobar some|data|to|insert|here|foobar some|data|to|insert|here|foobar some|data|to|insert|here|foobar some|data|to|insert|here|foobar HEREDOC; $query = "INSERT INTO `blogs` (`url`, `posturl`, `user`, `password`, `type`, `cat`) VALUES "; # Break data by lines $pieces = explode( "\n", $sampleData ); # Create a placeholder for our rows $rows = array(); # Loop through lines foreach( $pieces as $piece ) { # Break data by pipes $cols = explode( '|', $piece ); # Verify the first pipe is populated, or skip if ( $cols[0] == '' ) continue; # Sanitize and quote foreach ( $cols as $key => $col ) $cols[$key] = "'". mysql_real_escape_string( $col ) ."'"; # Implode, and add to rows array $rows[] = '('. implode( ', ', $cols ) .')'; } # Append imploded rows array to query $query .= implode( ', ', $rows ); # Output query, make sure it's good echo $query; ?> You may want to verify that count( $cols ) == the number of rows you expect... depending on data source. Quote Link to comment Share on other sites More sharing options...
shage Posted July 31, 2008 Author Share Posted July 31, 2008 looks alot better but still only doing one of the lines Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 looks alot better but still only doing one of the lines Then there's something wrong with your source data. The data provided above works perfect on my system. Have you tried simply echo'ing the query? Quote Link to comment Share on other sites More sharing options...
shage Posted July 31, 2008 Author Share Posted July 31, 2008 ill try now, its all submitted by a list this is what the query is INSERT INTO `blogs` (`url`, `posturl`, `user`, `password`, `type`, `cat`) VALUES ('some', 'data', 'to', 'insert', 'here', 'foobar'), ('some1', 'data', 'to', 'insert', 'here', 'foobar'), ('some2', 'data', 'to', 'insert', 'here', 'foobar'), ('some3', 'data', 'to', 'insert', 'here', 'foobar'), ('some4', 'data', 'to', 'insert', 'here', 'foobar') for test purpose Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 The query worked fine in my test database. Inserted rows: 5 (Query took 0.0582 sec) SQL query: INSERT INTO `blogs` (`url`, `posturl`, `user`, `password`, `type`, `cat`) VALUES ('some', 'data', 'to', 'insert', 'here', 'foobar'), ('some1', 'data', 'to', 'insert', 'here', 'foobar'), ('some2', 'data', 'to', 'insert', 'here', 'foobar'), ('some3', 'data', 'to', 'insert', 'here', 'foobar'), ('some4', 'data', 'to', 'insert', 'here', 'foobar') Table structure CREATE TABLE `blogs` ( `url` varchar(50) NOT NULL, `posturl` varchar(50) NOT NULL, `user` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `type` varchar(50) NOT NULL, `cat` varchar(50) NOT NULL ) Quote Link to comment Share on other sites More sharing options...
shage Posted July 31, 2008 Author Share Posted July 31, 2008 i think it hates me Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 What version of MySQL do you have? Though it'd have to be ancient to not support multiple inserts in a single query. Quote Link to comment Share on other sites More sharing options...
shage Posted July 31, 2008 Author Share Posted July 31, 2008 thank you it worked i messed up on the id key, you were right and dead on, again thank you and you are the man Quote Link to comment 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.