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?
 
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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
neeraj523Commented:
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.