Jump to content

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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