Best way to insert multiple entries into mysql table

Hi all i am wondering how do insert multiple rows into a mysql table for example i have 5 fields of email text filed and user fields and i want to post the values of them into a table but not sure exactly how this is what i have

<table>
<form method="post" action="">
<?php
 for($i=5; $i>=1; $i--) {
 ?>
 <tr>
		<th><input type="text" name="user<?php echo"".$i."";?>" value="user<?php echo "".$i."";?>"></th>
		<th><input type="text" name="email<?php echo "".$i."";?>" value="email<?php echo "".$i."";?>"></th>
	</tr>
 <?
 }
 ?>
 <tr>
 	<th><input type="submit" name="dp" value="Display"></th>
 </tr></form>
 <?php
 error_reporting(E_ALL);
 if(isset($_POST['dp']))
 {
 	//
	for($y=$i; $y>=1; $y--)
	 {
		echo"
		<tr>
			<th>".$user[$y]."</th>
			<th>".$email."</th>
		</tr>";
 	}
	//
	$user=$_POST['user'];
	$email=$_POST['email'];
	
 }
 else
 {
 	echo "Didnt press buton";
 }
 ?>
 </table>

That is my attempt i have been playing around with it can anyone help me please?

Thanks,William


<table>
<form method="post" action="">
<?php
for($i=5; $i>=1; $i--) {
?>
<tr>
        <th><input type="text" name="user<?php echo"".$i."";?>" value="user<?php echo "".$i."";?>"></th>
        <th><input type="text" name="email<?php echo "".$i."";?>" value="email<?php echo "".$i."";?>"></th>
    </tr>
<?php
}
?>
<tr>
    <th><input type="submit" name="dp" value="Display"></th>
</tr></form>
<?php
error_reporting(E_ALL);
if(isset($_POST['dp']))
{
    //
    $i=5;
    for($y=$i; $y>=1; $y--)
     {
        echo"
        <tr>
            <th>".$_POST['user'.$y]."</th>
            <th>".$_POST['email'.$y]."</th>
        </tr>";
    }
    //


}
else
{
    echo "Didnt press buton";
}
?>
</table>

Well first you need to establish a connection to the database. Once you have accomplished that you use the following if you connection object is a pdo instance. Furthermore, you shouldn’t place POST or REQUEST variables directly into sql before running some type of validation on them. This is less of a concern when binding, but you should still at least check the input against a basic pattern or number of characters.


$sql = 'INSERT INTO users (user,email) VALUES (?,?)';

$user;
$email;

if($stmt=$pdo->prepare($sql)) {
  $stmt->bindParam(1,$user,PDO::PARAM_STR);
  $stmt->bindParam(2,$email,PDO::PARAM_STR);

  foreach($users as $data) {
    $user = $data['user']
    $email = $data['email'];
    $stmt->execute();
  }
}

the best way to insert multiple rows into a table is not with a loop, because that produces multiple INSERT statements, which are executed sequentially –

INSERT INTO users (username,email) VALUES ( ‘curly’, ‘woowoo’ );
INSERT INTO users (username,email) VALUES ( ‘larry’, ‘heymoe’ );
INSERT INTO users (username,email) VALUES ( ‘moe’, ‘whyioughta’ );
INSERT INTO users (username,email) VALUES ( ‘shemp’, ‘eebeebeeb’ );
INSERT INTO users (username,email) VALUES ( ‘joe’, ‘ohyou’ );
INSERT INTO users (username,email) VALUES ( ‘curlyjoe’, ‘geethanks’ );

instead, the best way is to make only one call to the database –

INSERT INTO users (username,email) VALUES
( ‘curly’, ‘woowoo’ )
,( ‘larry’, ‘heymoe’ )
,( ‘moe’, ‘whyioughta’ )
,( ‘shemp’, ‘eebeebeeb’ )
,( ‘joe’, ‘ohyou’ )
,( ‘curly joe’, ‘geethanks’ )
;

i dont use connection object programming i just using the simple connecting and inserting multiple values into mysql table

with this part here how can i make it repeat this way instead of doing it with multiple insert in queries?

INSERT INTO users (username,email) VALUES
( ‘curly’, ‘woowoo’ )
,( ‘larry’, ‘heymoe’ )
,( ‘moe’, ‘whyioughta’ )
,( ‘shemp’, ‘eebeebeeb’ )
,( ‘joe’, ‘ohyou’ )
,( ‘curly joe’, ‘geethanks’ )
;

any ideas?


$inserts = array();
foreach($records as $record ) {
   $inserts[] = 
   '(\\''.mysql_real_escape_string($record['user']).'\\',\\''.mysql_real_escape_string($record['email']).'\\')';
}

$sql = 'INSERT INTO users (username,email) VALUES '.implode(',',$inserts).';';

mysql_query($sql); 

This is the error i got from the code you gave me above

Notice: Undefined variable: records in C:\Program Files\xampp\htdocs\wikclanscriptsv2\ est.php on line 33

Warning: Invalid argument supplied for foreach() in C:\Program Files\xampp\htdocs\wikclanscriptsv2\ est.php on line 33

Notice: Undefined variable: insert in C:\Program Files\xampp\htdocs\wikclanscriptsv2\ est.php on line 43
INSERT INTO members (username,password,email,rank,recruiter,disabled) VALUES ;

the code i have now is

					?><table> 
				<form method="post" action=""> 
				<?php 
				for($i=5; $i>=1; $i--) 
				{ 
				?> 
				<tr> 
						<th><input type="text" name="user<?php echo"".$i."";?>" value="user<?php echo "".$i."";?>"></th> 
						<th><input type="text" name="email<?php echo "".$i."";?>" value="email<?php echo "".$i."";?>"></th> 
						<th><select name="rank<?php echo "".$i."";?>">
							<option value="1">Rank 1</option>
							<option value="2">Rank 2</option>
							<option value="3">Rank 3</option>
							</select></th>
							<th><input type="text" name="pass<?php echo "".$i."";?>" value="pass<?php echo "".$i."";?>"></th>    
					</tr> 
				<?php 
				} 
				?> 
				<tr> 
					<th><input type="submit" name="dp" value="Display"></th> 
				</tr></form> 
				<?php 
				error_reporting(E_ALL); 
				if(isset($_POST['dp'])) 
				{ 
					//
					$recruiter="test";
					$disabled="1";
					$inserts = array();
					foreach($records as $record )
					{
					  $inserts[] = 
					   '('.mysql_real_escape_string($record['user']).'
					   ,'.mysql_real_escape_string($record['email']).'
					   ,'.mysql_real_escape_string($record['rank']).'
					   ,'.mysql_real_escape_string($record['pass']).'
					   ,'.$recruiter.'
					   ,'.$disabled.')';
					}
					echo $insert;
					$sql = 'INSERT INTO members (username,password,email,rank,recruiter,disabled) VALUES '.implode(',',$inserts).';';
					 echo $sql;
					//
				}