Jump to content

[SOLVED] Loop help


shage

Recommended Posts

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);
    }
}

Link to comment
https://forums.phpfreaks.com/topic/117573-solved-loop-help/
Share on other sites

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);
}

Link to comment
https://forums.phpfreaks.com/topic/117573-solved-loop-help/#findComment-604717
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/117573-solved-loop-help/#findComment-604737
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/117573-solved-loop-help/#findComment-604767
Share on other sites

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
)

Link to comment
https://forums.phpfreaks.com/topic/117573-solved-loop-help/#findComment-604777
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.