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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.