How to submit multiple php array to mysql ?

Hi,

I would like to request an assistant.

May i know how to capture the following submitted form and update it in mysql ?

The following is the form :

$id=$line[id];
<input type=text name='newip[]'> <input type=hidden name='id[]' value='$id' >



The following is my script to capture the submitted form, it is obvious that  **foreach($newiparray as $newip) && foreach($idarray as $id) {** will not work, but i wanted the same scenario but with working solution .



if(isset($_POST['Submitip'])){
$newiparray=$_POST['newip'];
$idarray=$_POST['id'];

foreach($newiparray as $newip) && foreach($idarray as $id) {

if ($newip !=""){
$sql=mysql_query("UPDATE tblip SET newip='$newip' WHERE id='$id' ");
if($sql){echo "<p><font face='Arial'><b>New IP $newip and ID $id Updated</b></font></p>";}}}}



Appreciates anybody can assist me on how to capture the "$id" array with the "$newip" array.


Thank you.
LVL 2
smksaAsked:
Who is Participating?
 
InsoftserviceConnect With a Mentor Commented:
srry.
i forgot to provide the <br> let it be


$i =0;
foreach($newiparray as $newip)
{
echo $ip = $idarray[$i];
echo "UPDATE tblip SET newip='". $newip."' WHERE id='"$ip"';
$i++;
echo "<br>";
}}
u will get ur both ips
0
 
Mark BradyPrincipal Data EngineerCommented:
Looks like you have the right idea but you need to use it like this

foreach($newiparray as $newip) {
     
    foreach($idarray AS $id) {
    // sql insert statement here
    }
}

So that should go through each value in the $newiparray and with each one it will loop through each value in the $idarray and insert it into the table.
0
 
smksaAuthor Commented:
I tried as follows :

if(isset($_POST['Submitip']) && isset($_POST['newip'])){
$newiparray=$_POST['newip'];
$idarray=$_POST['id'];
foreach($newiparray as $newip) {
foreach($idarray as $id) {
$sql=mysql_query("UPDATE tblip SET newip='$newip' WHERE id='$id' ");
if($sql){echo "<p><font face='Arial'><b>New IP $newip and ID $id Updated</b></font></p>";}}}}


It seems single NEWIP is updated in All ID table.

Its not working as it should.

Appreciates any other assistant.
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
InsoftserviceCommented:
hi,

provide ur both array o/p

$i =0;
foreach($newiparray as $newip)
{
echo $idarray[$i];

$i++;
}

please provide echo of this value.
0
 
smksaAuthor Commented:
Hi,

My Array form is as follows :

<input size='16' type=text name='newip[]' value='$newip'> <input type=hidden name='id[]' value='$id' >


The following script :


if(isset($_POST['Submitip'])){
$newiparray=$_POST['newip'];
$idarray=$_POST['id'];

$i =0;
foreach($newiparray as $newip)
{
echo $idarray[$i];
$i++;
}}


Produce :


727444221552156308309375155915601941194221621753541681168217321733216721681815181614511452145314541987198819891990199119922622631571157220662067206818972126212721872188179236736821022103194719482078207920802081209620972098214221432144108710881089109021792180192619271881891901911566156721812182218314631464584585608153815392050205116401641164219491950195119521953195419791980246247555556274275185918602202220317761777188118822088208920692070201420153143153163173183193203211671167216731674167516761630163121202121161416153623631652165317301731165416552158215917101711171217134734741634163517541755184818492108544545202020311596159721702171217221732174217518541855182518261643164418911892213021312122212342644371732052061420156115681595213621371761987988523524213821392140214114177577584485255268242054205517221723108137921652166205620911929193087037637721282129170417055996001746174718501851175017511037103820462047164516461879188021892190603604539540192817569369371726172720442045213421351687168866919811982219221932063206484684763163263363463763821322133172417251601291301311321331341351365931786178720572058189318942032203320942095644645219621978488531041104267667764664766483383421982199917762142665465519081909701702211621172118211970821242125220422051005100620902162709146514661310131167142114228666668652149215011625942182153215495395421602161176817691039104089589689789889990020482049901763764220022012109192219239939944634642163216472172210851086926927938939161216132169737738942143714381380913914977978214621471468148214831986202820292030170118331834185614698641254890641589588616263991992313292492596812477344785659278124991040411124051314215715161375989990865232475252632427283252930461462192020320415315448548621942195641039899856857858859860959697720867185218532023202420259659859861413141412571258135413551158115913561411141217401741194617421743121512161840184120422043209921451967147814791788178918571858193919401995199615621563172112291230187018711876205920601955195621002101199720022003200420072008179517962177217820822083211421151972197319742065209220931401140221512152142514451446207320742075207620772148217621912206220722081247124811601161184519851868186912901291152917781779176217631427142814291430188718881326132721042105210621071286128315801620


Appreciates any assistant.
0
 
Chris SandriniSenior System EngineerCommented:
You can create multidimensional arrays

<input type='text' name='newip[$id]'>

Open in new window


if ( isset($_POST['submit']))
{	
	foreach(array_keys($_POST['newip']) as $key)
	{
		$sql = "UPDATE tblip SET newip='{$_POST['newip'][$key]}' WHERE id='{$key}'";
		echo $sql;
	}

}

Open in new window


Here some output

Array
(
    [1] => 192.168.1.1
)
UPDATE tblip SET newip='192.168.1.1' WHERE id='1'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.