-1

I have to insert single set of data multiple times , say n rows.

INSERT INTO MyTable VALUES ("John", 123, "US");

Can I insert all n rows in a single SQL statement?

here n is dynamic value n is user input , how to make insert query n times , any idea.

$sql = "INSERT INTO `mytable` VALUES(`field1`,`field2`,`date`) VALUES ";
$count = 5;
for($i=0;$i<$coutn;$i++)
{
if($type=="daily")
{
  $st=date('Y-m-d H:i:s', strtotime('+1 day', strtotime('$st')));
}
else if($type=="monthly")
{
  $st=date('Y-m-d H:i:s', strtotime('+30 day', strtotime('$st')));
}

$sql .= " ('john','123','$st' )";
}

is this correct way..

Steve Bals
  • 1,949
  • 6
  • 22
  • 32

1 Answers1

0

You can do it like:

$sql = "INSERT INTO `mytable` VALUES(`field1`,`field2`,`date`) VALUES ";
$count = 5;
$arr = array();
for($i=0;$i<$coutn;$i++)
{
if($type=="daily")
{
  $st=date('Y-m-d H:i:s', strtotime('+1 day', strtotime('$st')));
}
else if($type=="monthly")
{
  $st=date('Y-m-d H:i:s', strtotime('+30 day', strtotime('$st')));
}

$arr[] = " ('john','123','$st' )";
}
$sql .= implode(',', $arr);
Pupil
  • 23,834
  • 6
  • 44
  • 66