Arsenelupin Posted January 30, 2009 Share Posted January 30, 2009 I have been trying to insert two different arrays into a MySQL table, but with no success. First, I have a form for adding records to a database: <form enctype="multipart/form-data" action="iactor2.php" method="POST"> <script src="addmulti.js" language="Javascript" type="text/javascript"></script> <div id="dynamicInput"> Checkbox: <br><input type="checkbox" name="acheckbox[]" value="A"> Text: <br><input type="text" name="atext[]"> </div> <input type="button" value="Add another input" onClick="addmulti('dynamicInput');"> <input type="submit" value="Add"> </form> As you can see, I also have a possibility for adding an extra field and checkbox. For that, I am using the javascript addmulti.js: (I have kinda copied this from: http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/ ) var counter = 1; var limit = 3; function addmulti(divName){ if (counter == limit) { alert("You have reached the limit of adding " + counter + " inputs"); } else { var newdiv = document.createElement('div'); newdiv.innerHTML = "Some Title " + (counter + 1) + " <br><input type='text' name='atext[]'><br><input type='checkbox' name='acheckbox[]' value='A'>"; document.getElementById(divName).appendChild(newdiv); counter++; } } OK, now I want to insert the two arrays into a table. An example of this can be a user that want to insert two checkbox/text values into a table. Form: Checkbox value = A Text value = something written 1 Checkbox value = A Text value = something written 2 Table: ID Column1 Column2 1 A something written 1 2 A something written 2 It is here that I get stuck. I am able to insert one array, like this: $acheckbox=$_POST["acheckbox"]; foreach($acheckbox as $bcheckbox) { $sql="INSERT INTO table (column1) VALUES ('$bcheckbox')" ; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } } But how do I do if I also want the text for the corresponding textbox? Thanks for your time. Cheers Finn Quote Link to comment Share on other sites More sharing options...
premiso Posted January 30, 2009 Share Posted January 30, 2009 Either add a 2nd column or concat the two values: $acheckbox=$_POST["acheckbox"]; foreach($acheckbox as $key => $bcheckbox) { $sql="INSERT INTO table (column1) VALUES ('$bcheckbox::{$_POST['atext'][$key]}')" ; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } } That would work, given that they have the same array index. Quote Link to comment Share on other sites More sharing options...
Arsenelupin Posted January 30, 2009 Author Share Posted January 30, 2009 Either add a 2nd column or concat the two values: $acheckbox=$_POST["acheckbox"]; foreach($acheckbox as $key => $bcheckbox) { $sql="INSERT INTO table (column1) VALUES ('$bcheckbox::{$_POST['atext'][$key]}')" ; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } } That would work, given that they have the same array index. Thank you. My problem, however, have been to insert the other array into the second column. For example: $atext=$_POST["atext"]; $acheckbox=$_POST["acheckbox"]; foreach($acheckbox as $bcheckbox) { $sql="INSERT INTO table (column1, column2) VALUES ('$bcheckbox', '$atext')" ; Which gives me the value "Array" into my column2. (I am very new in php) Quote Link to comment Share on other sites More sharing options...
landavia Posted January 30, 2009 Share Posted January 30, 2009 >>$atext lets back to top post u type like this >>Text: <br><input type="text" name="atext[]"> and then u try to take $atext like this >>$atext=$_POST["atext"]; this is false?!! why.. because in the first u type name as array >> name="atext[]" fix $atext like this <b>$atext=$_POST["atext"][];</b> Quote Link to comment Share on other sites More sharing options...
premiso Posted January 30, 2009 Share Posted January 30, 2009 Ummm, then you take the same logic I gave you and use it: $acheckbox=$_POST["acheckbox"]; foreach($acheckbox as $key => $bcheckbox) { $sql="INSERT INTO table (column1, column2) VALUES ('$bcheckbox', '{$_POST['atext'][$key]}')" ; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } } Should work. Quote Link to comment Share on other sites More sharing options...
landavia Posted January 30, 2009 Share Posted January 30, 2009 premiso: thx use premiso answer.. Quote Link to comment Share on other sites More sharing options...
Arsenelupin Posted February 2, 2009 Author Share Posted February 2, 2009 Ummm, then you take the same logic I gave you and use it: Sorry, I didn't read it enough. Thank you, it helped! Quote Link to comment Share on other sites More sharing options...
Arsenelupin Posted February 2, 2009 Author Share Posted February 2, 2009 Now I am having problem inserting multiple files to a folder on my server. For one file, I have this code to define the path and to get the filename: $target = "path to my folder"; $target = $target . basename( $_FILES['file']['name']); (Some insert statements etc..) But how should I handle this if it is an array? Thanks again for your help. Quote Link to comment Share on other sites More sharing options...
Arsenelupin Posted February 2, 2009 Author Share Posted February 2, 2009 Now I am having problem inserting multiple files to a folder on my server. For one file, I have this code to define the path and to get the filename: $target = "path to my folder"; $target = $target . basename( $_FILES['file']['name']); (Some insert statements etc..) But how should I handle this if it is an array? Thanks again for your help. Or that was easy to solve... sometimes I ask before I think. Quote Link to comment Share on other sites More sharing options...
Arsenelupin Posted February 2, 2009 Author Share Posted February 2, 2009 Or not really... does someone want to help me with this? It works to upload files without arrays, but when I make an array of them, it does not work. $folder = "path to my directory"; $file = $folder . basename( $_FILES['nameoffile']['name']); if (move_uploaded_file($_FILES['nameoffile']['tmp_name'], $file)) { echo "File uploaded"; } else { echo "File not uploaded"; } $file1 = ($_FILES['nameoffile']['name']); foreach($file1 as $file2) { mysql_query("INSERT INTO bp_image (img_name) VALUES ('$file2')"); } ?> Cheers /Arsene Quote Link to comment Share on other sites More sharing options...
landavia Posted February 6, 2009 Share Posted February 6, 2009 perhaps this might help you $_FILES['nameoffile'][$i]['name'] $i are int??? Quote Link to comment 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.