flforlife Posted April 15, 2008 Share Posted April 15, 2008 I'm trying to multple records in one table but i can't figure it out. <tr> <td><input name="entry[0][place]" type="text" /></td> <td><input name="entry[0][team]" type="text" /></td> <td><input name="entry[0][wl]" type="text" /></td> <td><input name="entry[0][gb]" type="text" /></td> <td><input name="entry[0][pcage]" type="text" /></td> <td><input name="entry[0][rs]" type="text" /></td> <td><input name="entry[0][ra]" type="text" /></td> <td><input name="entry[0][ags]" type="text" /></td> </tr> <tr> <td><input name="entry[1][place]" type="text" /></td> <td><input name="entry[1][team]" type="text" /></td> <td><input name="entry[1][wl]" type="text" /></td> <td><input name="entry[1][gb]" type="text" /></td> <td><input name="entry[1][pcage]" type="text" /></td> <td><input name="entry[1][rs]" type="text" /></td> <td><input name="entry[1][ra]" type="text" /></td> <td><input name="entry[1][ags]" type="text" /></td> </tr> this is add.php code.... foreach($_POST['sql']){ sql = $_POST["entry"][0] = array("place"=>"...", "team"=>"..."); sql = $_POST["entry"][1] = array("place"=>"...", "team"=>"..."); $result = mysql_query($sql); } I get an error message! Is it the foreach loop or should I use for loop? Link to comment https://forums.phpfreaks.com/topic/101161-solved-insert-multple-records-in-a-table/ Share on other sites More sharing options...
Barand Posted April 15, 2008 Share Posted April 15, 2008 try <?php if (isset($_POST['btnSub'])) { $sql = "INSERT INTO mytable (place,team, wl, gb, pcage, rs, ra, ags) VALUES\n "; foreach ($_POST['entry'] as $data) { $dataArray[] = "('" . join ("','", $data) . "')"; } $sql .= join (",\n", $dataArray); echo '<pre>', $sql, '</pre>'; // view query } ?> <form method='post' enctype='multipart/form-data'> <table> <tr> <td><input name="entry[0][place]" type="text" /></td> <td><input name="entry[0][team]" type="text" /></td> <td><input name="entry[0][wl]" type="text" /></td> <td><input name="entry[0][gb]" type="text" /></td> <td><input name="entry[0][pcage]" type="text" /></td> <td><input name="entry[0][rs]" type="text" /></td> <td><input name="entry[0][ra]" type="text" /></td> <td><input name="entry[0][ags]" type="text" /></td> </tr> <tr> <td><input name="entry[1][place]" type="text" /></td> <td><input name="entry[1][team]" type="text" /></td> <td><input name="entry[1][wl]" type="text" /></td> <td><input name="entry[1][gb]" type="text" /></td> <td><input name="entry[1][pcage]" type="text" /></td> <td><input name="entry[1][rs]" type="text" /></td> <td><input name="entry[1][ra]" type="text" /></td> <td><input name="entry[1][ags]" type="text" /></td> </tr> </table> <input type='submit' name='btnSub' value='Submit'> </form> Link to comment https://forums.phpfreaks.com/topic/101161-solved-insert-multple-records-in-a-table/#findComment-517450 Share on other sites More sharing options...
flforlife Posted April 15, 2008 Author Share Posted April 15, 2008 no it didn't work. I think its just the database. ..... here is the snippet database..... CREATE TABLE major ( id INT NOT NULL AUTO_INCREMENT, place VARCHAR(65) NOT NULL DEFAULT '', team VARCHAR(65) NOT NULL DEFAULT '', wl VARCHAR(65) NOT NULL DEFAULT '', gb VARCHAR(65) NOT NULL DEFAULT '', pcage VARCHAR(65) NOT NULL DEFAULT '', rs VARCHAR(65) NOT NULL DEFAULT '', ra VARCHAR(65) NOT NULL DEFAULT '', ags VARCHAR(65) NOT NULL DEFAULT '', PRIMARY KEY(id) ); input form............ <h1>Major</h1> <form method='post' enctype='multipart/form-data' action="add.php"> <table> <tr> <td>PLACE</td> <td>TEAM</td> <td>WIN-LOSE</td> <td>GB</td> <td>%AGE</td> <td>RUNS SCORED</td> <td>RUNS ALLOWED</td> <td>AVERAGE GAME SCORE</td> </tr> <tr> <tr> <td><input name="entry[0][place]" type="text" /></td> <td><input name="entry[0][team]" type="text" /></td> <td><input name="entry[0][wl]" type="text" /></td> <td><input name="entry[0][gb]" type="text" /></td> <td><input name="entry[0][pcage]" type="text" /></td> <td><input name="entry[0][rs]" type="text" /></td> <td><input name="entry[0][ra]" type="text" /></td> <td><input name="entry[0][ags]" type="text" /></td> </tr> <tr> <td><input name="entry[1][place]" type="text" /></td> <td><input name="entry[1][team]" type="text" /></td> <td><input name="entry[1][wl]" type="text" /></td> <td><input name="entry[1][gb]" type="text" /></td> <td><input name="entry[1][pcage]" type="text" /></td> <td><input name="entry[1][rs]" type="text" /></td> <td><input name="entry[1][ra]" type="text" /></td> <td><input name="entry[1][ags]" type="text" /></td> </tr> </table> <input type='submit' name='btnSub' value='Submit'> </form> add.php................. <?php //add fourteam data $hostname = 'localhost'; $username = 'root'; $password = ''; //add later $database = 'test'; $mysql = mysql_connect($hostname, $username, $password); mysql_select_db($database); if (isset($_POST['btnSub'])) { $sql = "INSERT INTO major (place, team, wl, gb, pcage, rs, ra, ags) VALUES\n "; foreach ($_POST['entry'] as $data) { $dataArray[] = "('" . join ("','", $data) . "')"; } $sql .= join (",\n", $dataArray); echo '<pre>', $sql, '</pre>'; // view query } /* $_POST["entry"][0] = array("place"=>"", "team"=>"$entry"); $_POST["entry"][1] = array("place"=>"$entry", "team"=>"$entry"); */ /* foreach($_POST['place'] as $place => $value) { $sql="INSERT INTO major (place) VALUES ('$_POST[place]')"; } */ /* foreach($_POST['place'] as $place => $value) { $sql = "insert into majors ($ $result = mysql_query($sql); } */ /* foreach($_POST['id'] as $idx => $val) { $id = $val; $place = $_POST['place'][$idx]; $team = $_POST['team'][$idx]; $wl = $_POST['wl'][$idx]; $sql = "insert into majors (id,place,team,wl) values ('$id','$place','$team','$wl')"; $result = mysql_query($sql); } // EndForEach segment */ header("location:ftadmin.php"); echo "1 record added"; mysql_close($mysql) ?> Done. <a href="ftoutput.php">Back</a> frustrated!!!1 ??? Link to comment https://forums.phpfreaks.com/topic/101161-solved-insert-multple-records-in-a-table/#findComment-517459 Share on other sites More sharing options...
GingerRobot Posted April 15, 2008 Share Posted April 15, 2008 The idea wasn't that you add the code Barand provided to the beginning of your script, it was a replacement. The only thing i see missing is that the query that is built doesn't get executed. Try this: <?php if (isset($_POST['btnSub'])) { $sql = "INSERT INTO mytable (place,team, wl, gb, pcage, rs, ra, ags) VALUES\n "; foreach ($_POST['entry'] as $data) { $dataArray[] = "('" . join ("','", $data) . "')"; } $sql .= join (",\n", $dataArray); echo '<pre>', $sql, '</pre>'; // view query mysql_query($sql) or die(mysql_error()); } ?> If you have any problems, try providing us with the output of the above - the query is being printed to the screen to help debug any errors. Also, when you post code/output please place it inside tags. Link to comment https://forums.phpfreaks.com/topic/101161-solved-insert-multple-records-in-a-table/#findComment-517466 Share on other sites More sharing options...
flforlife Posted April 15, 2008 Author Share Posted April 15, 2008 Ok there was no error. I guess this is my first doing arrays this way! Thanks it works!!! Link to comment https://forums.phpfreaks.com/topic/101161-solved-insert-multple-records-in-a-table/#findComment-517468 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.