PHP Insert multiple rows

Hi all,
I am new to PHP. I need to insert Insert multiple rows at the same time. this is the code

 $x=0;
while($x<$num){
 mysql_data_seek($result, $x);
$row= mysql_fetch_array($result);
$username=$row[1];
$password=$row[2];
$query1="INSERT INTO Purchses (Item_id, Item_name) Values ('".$username."', '".$password ."')";
$result=mysql_query($query1);
mysql_close();
$x=$x+1;
}
khalid11Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Avinash ZalaWeb ExpertCommented:
Don't do mysql_close();

Hope this helps
Addy
0
Avinash ZalaWeb ExpertCommented:
It should be done after while loop if you want.

Hope this helps
Addy
0
neeraj523Commented:
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

khalid11Author Commented:
I think I miss something here.

I have close the mysql after the loop but still.
0
theodorejsalvoCommented:
Move the mysql_close line to where I have it now.  This is what everyone meant by "outside the while" loop.

HTH
$x=0;
while($x<$num){
  mysql_data_seek($result, $x);
  $row= mysql_fetch_array($result);
  $username=$row[1];
  $password=$row[2]; 
  $query1="INSERT INTO Purchses (Item_id, Item_name) Values ('".$username."', '".$password ."')"; 
  $result=mysql_query($query1);
  $x=$x+1;
}
mysql_close();

Open in new window

0
khalid11Author Commented:
I have moved the mysql_close line outside the loop but still not working. It insert just one row and the other rows is empty.
0
theodorejsalvoCommented:
In your while loop, you're reading a row from a result array, but then when you call the insert, you save the result again as $result.

Thus, after you run it once, you are trying to pull data from a result array that only has the status of your last insert.

Change $result to $result1 on line 8 (or just recopy this new code)

Let me know if it works.
$x=0;
while($x<$num){
  mysql_data_seek($result, $x);
  $row= mysql_fetch_array($result);
  $username=$row[1];
  $password=$row[2]; 
  $query1="INSERT INTO Purchses (Item_id, Item_name) Values ('".$username."', '".$password ."')"; 
  $result1=mysql_query($query1); // Note change HERE
  $x=$x+1;
}
mysql_close();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
khalid11Author Commented:
Once I run the code. It insert one record, and the other records also insert it but as empty row.
0
theodorejsalvoCommented:
I think we crossed paths on posting.  The empty rows are because your reassign $result to the mysql_query($query1);

Change it to $result1 and it should then work.
0
khalid11Author Commented:
Thank you : )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.