Lister471 Posted January 13, 2012 Share Posted January 13, 2012 Me again i will get the hang of this one day I have a form that has 3 fields battleno, asname, amon. I can send the arrays to the next page and echo them out no problem, but this is individually. What i am unable to do is add the other 2 fields into the code below so the code keeps working. I have tried a few things and well i thought it was time to post. I have a few books but there is nothing in them about this. Also searched the internet but there is little that i understand to be honest and the things i did find dont work. battleno is not an array btw the other 2 are. foreach($_POST['asname'] as $row => $assetname) { $asname = mysql_real_escape_string($assetname); $query = "INSERT INTO `assetlist` (`asset`) VALUES ('$asname')"; if(!mysql_query($query)) { echo"Error"; } else { echo "$row record added<br />"; } } mysql_close($con) Hope that makes sense and thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/254938-need-some-help-again/ Share on other sites More sharing options...
Muddy_Funster Posted January 13, 2012 Share Posted January 13, 2012 I think you want to be looking at multi dimensional arrays (siplified they are arrays within arrays). something like : $master = array(); foreach ($_POST['asname'] as $key => $value){ $master['asname'][$key] = $value; } then use more foreach along with explode to build up a single insert string that you can run on the database. you could user the $_POST array, but I preffer to leave that alone whenever I can. Quote Link to comment https://forums.phpfreaks.com/topic/254938-need-some-help-again/#findComment-1307185 Share on other sites More sharing options...
Lister471 Posted January 13, 2012 Author Share Posted January 13, 2012 cheers. maybe i am unsure to be honest. If i was doing it with out arrays i would have something like. <? $value1 = $_POST['value1']; $value2 = $_POST['value1']; $value3 = $_POST['value1']; $app_sql = mysql_query("INSERT INTO table (value1, value 2, value3) VALUES('$value1', '$value2','$value3')") or die (mysql_error()); if(!$app_sql){ echo 'There has been an error creating your account. Please contact the webmaster.'; exit(); }else{ echo"Done info has been added"; } But with the information coming from arrays well i just get lost. Quote Link to comment https://forums.phpfreaks.com/topic/254938-need-some-help-again/#findComment-1307197 Share on other sites More sharing options...
Lister471 Posted January 13, 2012 Author Share Posted January 13, 2012 OK taking the advise above i have made some small headway. However i have a problem i have no idea how to fix. Ok the problem is the array is carrying empty fields. So there is 26 fields and people can fill out anything from 1 of them to 26. If you say fill out 3 of the fields you get a result like. Returned Value 1 Returned Value 2 Returned Value 3 Empty Empty x 21. And then it adds 21 empty fields to the database. My question is how do i make it so it only add fields to the database that has content rather than adding the full 26 rows. Hope it makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/254938-need-some-help-again/#findComment-1307365 Share on other sites More sharing options...
Muddy_Funster Posted January 13, 2012 Share Posted January 13, 2012 something like this should help, you can obviously include the if within an existing foreach : foreach ($array as $key => $value){ if (empty($array[$key]){ unset($array[$key]); } } Quote Link to comment https://forums.phpfreaks.com/topic/254938-need-some-help-again/#findComment-1307389 Share on other sites More sharing options...
jcbones Posted January 13, 2012 Share Posted January 13, 2012 Pass it to array_filter without a callback. Read the note under the callback parameter. Quote Link to comment https://forums.phpfreaks.com/topic/254938-need-some-help-again/#findComment-1307397 Share on other sites More sharing options...
Lister471 Posted January 13, 2012 Author Share Posted January 13, 2012 Cheers for the replys. Muddy_Funster i tried what you said but it generates and error. That it canot unset. Has for the array_filter() that looks just like what i need. I will have a read up and hopefully it will make some sense. Thanks to you both. Quote Link to comment https://forums.phpfreaks.com/topic/254938-need-some-help-again/#findComment-1307416 Share on other sites More sharing options...
Lister471 Posted January 13, 2012 Author Share Posted January 13, 2012 I am getting close well what i class has close lol. However i have 1 problem well 2. $battleno = $_POST['battleno']; $asname = $_POST['asname']; $amon = $_POST['amon']; $amon = (array_filter($amon)); foreach ($asname as $asname) { foreach ($amon as $amon) // { $query = "INSERT INTO `assetlist` (`battleid` , `asset` , `amount`) VALUES ('$battleno' , '$asname' , '$amon')"; if(!mysql_query($query)) { echo"error"; } else { echo "$amon | $asname record added<br />"; } //} } mysql_close($con) Doing it like this adds everything fine exsept the amon field it adds only the last number from the array. However $battleno = $_POST['battleno']; $asname = $_POST['asname']; $amon = $_POST['amon']; $amon = (array_filter($amon)); foreach ($asname as $asname) { foreach ($amon as $amon) { $query = "INSERT INTO `assetlist` (`battleid` , `asset` , `amount`) VALUES ('$battleno' , '$asname' , '$amon')"; if(!mysql_query($query)) { echo"error"; } else { echo "$amon | $asname record added<br />"; } } } mysql_close($con) Doing it with the { } back in add everything fine expect the asname field where it add only the last select item from the array. I realize that the coding is prob all wrong but it sort of works Quote Link to comment https://forums.phpfreaks.com/topic/254938-need-some-help-again/#findComment-1307429 Share on other sites More sharing options...
jcbones Posted January 14, 2012 Share Posted January 14, 2012 OK, I'll attempt this, even though I am not 100% sure what the data holds. <?php $battleno = $_POST['battleno']; //integer? $asname = $_POST['asname']; //array of names that matches the count of the amon array BEFORE taking out empty values? $amon = $_POST['amon']; //amon array. $combined = array_combine($asname,$amon); //combine the arrays, so that asname is keys, and amon is values. foreach ($combined as $asn => $am) { if(!empty($am)) { $parts[] = "('$battleno' , '$asn' , '$am')"); } //if amon is not empty, add it to the query. } $query = "INSERT INTO `assetlist` (`battleid` , `asset` , `amount`) VALUES " . implode(', ',$parts); mysql_query($sql) or trigger_error($query . ' has encountered an error: <br />' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/254938-need-some-help-again/#findComment-1307586 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.