jbradley04 Posted July 15, 2010 Share Posted July 15, 2010 I am having issues trying to figure this out! I tried using a foreach statement but I am not sure I am getting this! plus its not working!!! Anyways.. this is what I have This is the form <input type="hidden" name="b[]" value="1" /> <td align="center" width="15%"><select name="a[]" ><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></td> <td align="center" width="15%"><select name="c[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></td> <td align="center" width="15%">B: <select name="d[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> S: <select name="e[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> </td> <td align="center" width="8%"><select name="f[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option></td> <td align="center" width="5%"><select name="g[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></td> <td align="center" width="15%">Att: <select name="h[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> S: <select name="i[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> </td> <td align="center" width="8%"><select name="j[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option></select></td> </tr> <input type="hidden" name="b[]" value="2" /> <td align="center" width="15%"><select name="a[]" ><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></td> <td align="center" width="15%"><select name="c[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></td> <td align="center" width="15%">B: <select name="d[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> S: <select name="e[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> </td> <td align="center" width="8%"><select name="f[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option></td> <td align="center" width="5%"><select name="g[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></td> <td align="center" width="15%">Att: <select name="h[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> S: <select name="i[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> </td> <td align="center" width="8%"><select name="j[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option></select></td> </tr> This is receiving the code from the form foreach($_POST['a'] as $row=>$At) { $a =$At; $b =$_POST['b'][$row]; $c =$_POST['c'][$row]; $d =$_POST['d'][$row]; $e =$_POST['e'][$row]; $f =$_POST['f'][$row]; $g =$_POST['g'][$row]; $h =$_POST['h'][$row]; $i =$_POST['i'][$row]; $j =$_POST['j'][$row]; } $addall = "INSERT INTO alphabet (`a`, `b`, `c`, `d`, `e`, `f`, `g`, `h`, `i`, `j`) VALUES ('.$a.','.$b.','.$c.','.$d.','.$e.','.$f.','.$g.','.$h.','.$i.','.$j.')"; I am sure I am way off! But I am looking for each entry to be a new row in mysql table.... So if someone eneters "a" in three different ways they will have three different rows.. I appreciate the help!!! J Quote Link to comment https://forums.phpfreaks.com/topic/207792-insert-multiple-rows-in-mysql-using-php/ Share on other sites More sharing options...
ajicles Posted July 15, 2010 Share Posted July 15, 2010 Ouch you are trying to use HTML syntax in PHP. Um try make the php part in one file. And the html form in another. And use HTML Form to reference the php code. So like this: Try that and see if there are any errors. ~AJ Submit Page: <!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>Untitled Document</title> </head> <body> <form action="submit.php" method="post"> <input type="hidden" name="b[]" value="1" /> <td align="center" width="15%"><select name="a[]" ><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></td> <td align="center" width="15%"><select name="c[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></td> <td align="center" width="15%">B: <select name="d[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> S: <select name="e[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> </td> <td align="center" width="8%"><select name="f[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option></td> <td align="center" width="5%"><select name="g[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></td> <td align="center" width="15%">Att: <select name="h[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> S: <select name="i[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> </td> <td align="center" width="8%"><select name="j[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option></select></td> </tr> <input type="hidden" name="b[]" value="2" /> <td align="center" width="15%"><select name="a[]" ><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></td> <td align="center" width="15%"><select name="c[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></td> <td align="center" width="15%">B: <select name="d[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> S: <select name="e[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> </td> <td align="center" width="8%"><select name="f[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option></td> <td align="center" width="5%"><select name="g[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></td> <td align="center" width="15%">Att: <select name="h[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> S: <select name="i[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> </td> <td align="center" width="8%"><select name="j[]"><option selected value="">---</option><option value="0">0</option><option value="1">1</option></select></td> </tr> </select> </body> </html> submit.php foreach($_POST['a'] as $row=>$At) { $a =$At; $b =$_POST['b'][$row]; $c =$_POST['c'][$row]; $d =$_POST['d'][$row]; $e =$_POST['e'][$row]; $f =$_POST['f'][$row]; $g =$_POST['g'][$row]; $h =$_POST['h'][$row]; $i =$_POST['i'][$row]; $j =$_POST['j'][$row]; } $addall = "INSERT INTO alphabet (`a`, `b`, `c`, `d`, `e`, `f`, `g`, `h`, `i`, `j`) VALUES ('.$a.','.$b.','.$c.','.$d.','.$e.','.$f.','.$g.','.$h.','.$i.','.$j.')"; Quote Link to comment https://forums.phpfreaks.com/topic/207792-insert-multiple-rows-in-mysql-using-php/#findComment-1086261 Share on other sites More sharing options...
jbradley04 Posted July 15, 2010 Author Share Posted July 15, 2010 Sorry... I posted it like that but in all reality I have it separate.... I have an html form being processed by php... So they way you wrote it is how I actually have it... I put it all together like that for the forum post... Did the INSERT statement look right? Should it work the way I have it? Quote Link to comment https://forums.phpfreaks.com/topic/207792-insert-multiple-rows-in-mysql-using-php/#findComment-1086262 Share on other sites More sharing options...
jbradley04 Posted July 15, 2010 Author Share Posted July 15, 2010 OK.. So I found one issue.... $addall = "INSERT INTO alphabet (`a`, `b`, `c`, `d`, `e`, `f`, `g`, `h`, `i`, `j`) VALUES ('.$a.','.$b.','.$c.','.$d.','.$e.','.$f.','.$g.','.$h.','.$i.','.$j.')"; Should be $addall = mysql_query("INSERT INTO alphabet (`a`, `b`, `c`, `d`, `e`, `f`, `g`, `h`, `i`, `j`) VALUES ('.$a.','.$b.','.$c.','.$d.','.$e.','.$f.','.$g.','.$h.','.$i.','.$j.')"); But the next issue is that it is not writing all rows.. but just the last row... How do I do it so it writes all arrays? Also, How do I prevent it from writing in rows where nothing was selected? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/207792-insert-multiple-rows-in-mysql-using-php/#findComment-1086270 Share on other sites More sharing options...
ajicles Posted July 15, 2010 Share Posted July 15, 2010 if ($_POST["a"] == "") { // Has Nothing } else { // Has something in it. } Quote Link to comment https://forums.phpfreaks.com/topic/207792-insert-multiple-rows-in-mysql-using-php/#findComment-1086301 Share on other sites More sharing options...
ZohaibAli Posted April 16, 2013 Share Posted April 16, 2013 (edited) <?php if ($_POST["a"] == "") { $arr_values = array();$arr_glued_with_comma = "";$addall = "INSERT INTO alphabet(`a`, `b`, `c`, `d`, `e`, `f`, `g`, `h`, `i`, `j`) VALUES ";foreach($_POST['a'] as $row=>$At){$a =$At;$b =$_POST['b'][$row];$c =$_POST['c'][$row];$d =$_POST['d'][$row];$e =$_POST['e'][$row];$f =$_POST['f'][$row];$g =$_POST['g'][$row];$h =$_POST['h'][$row];$i =$_POST['i'][$row];$j =$_POST['j'][$row];$arr_values[] = "('".$a."' ,'".$b."', '".$c."', '".$d."', '".$e."', '".$f."', '".$g."', '".$h."', '".$i."', '".$j."')";}$arr_glued_with_comma = implode(",", $arr_values);$addall .= $arr_glued_with_comma;$query = mysql_query($addall); } else { echo "Empty"; } ?> Edited April 16, 2013 by ZohaibAli Quote Link to comment https://forums.phpfreaks.com/topic/207792-insert-multiple-rows-in-mysql-using-php/#findComment-1425024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.