Jump to content

insert multiple rows


dflow

Recommended Posts

id like to insert multiple rows

each row should be auto incremented when inserted

for example into an image gallery table imageID,ProductID,imagename

 

your help with the syntax

 

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>insert multiple records</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="">
  
  


<?php require_once('Connections/international.php'); ?>

<?php
for ($i=1; $i<=3; $i++)
  {
  echo '<input name="imagename[]"';
  echo 'type="text"';
  echo '/>' . $i .' <br />';
  }
?>
<?php
mysql_select_db($database_international, $international);
for ($i=1; $i<=3; $i++)
  {
  
$sql = "INSERT INTO image_list (ProductID, imagename)
VALUES
('6666',$_POST['imagename[]'])
";
  }
?>
<label>
    <input type="submit" name="insert" id="insert" value="Submit" />
  </label></form>
<?php


mysql_query( $sql, $international);
echo "Data Inserted!";

?>


</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/195667-insert-multiple-rows/
Share on other sites

I *think* $_POST['imagename'] is supposed to be an array of values?

 

Also, the correct format for an INSERT query with multiple records is like this:

INSERT INTO tableName (field1, field2)
VALUES ('value1a', 'value1b'), ('value2a', 'value2b'), ('value3a', 'value3b') 

 

Plus you were not encasing the imageame value in single quotes in the query.

 

I think what you want to do is something like this:

$values = array();
foreach ($_POST['imagename'] as $imagename)
{
  $values[] = "('6666', '{$imagename}')";  
}

$sql = "INSERT INTO image_list (ProductID, imagename)
        VALUES " . implode(', ', $values);
mysql_query( $sql, $international);

Archived

This topic is now archived and is closed to further replies.

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