dflow Posted March 18, 2010 Share Posted March 18, 2010 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 More sharing options...
scvinodkumar Posted March 18, 2010 Share Posted March 18, 2010 if u want id to be auto-increment then alter the table to auto-increment Link to comment https://forums.phpfreaks.com/topic/195667-insert-multiple-rows/#findComment-1028133 Share on other sites More sharing options...
dflow Posted March 18, 2010 Author Share Posted March 18, 2010 if u want id to be auto-increment then alter the table to auto-increment it is but is the syntax correct im getting errors Link to comment https://forums.phpfreaks.com/topic/195667-insert-multiple-rows/#findComment-1028225 Share on other sites More sharing options...
dflow Posted March 19, 2010 Author Share Posted March 19, 2010 if u want id to be auto-increment then alter the table to auto-increment it is but is the syntax correct im getting errors bump Link to comment https://forums.phpfreaks.com/topic/195667-insert-multiple-rows/#findComment-1028434 Share on other sites More sharing options...
scvinodkumar Posted March 19, 2010 Share Posted March 19, 2010 You should get the values like this, $_POST['imagename'][$i], not $_POST['imagename[]'] $sql = "INSERT INTO image_list (ProductID, imagename) VALUES ('6666',$_POST['imagename'][$i]]) "; Link to comment https://forums.phpfreaks.com/topic/195667-insert-multiple-rows/#findComment-1028451 Share on other sites More sharing options...
Psycho Posted March 19, 2010 Share Posted March 19, 2010 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); Link to comment https://forums.phpfreaks.com/topic/195667-insert-multiple-rows/#findComment-1028454 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.