?
Solved

How to submit multiple php array to mysql ?

Posted on 2012-04-12
6
Medium Priority
?
411 Views
Last Modified: 2012-04-13
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.
0
Comment
Question by:smksa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 20

Expert Comment

by:Mark Brady
ID: 37838898
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
 
LVL 2

Author Comment

by:smksa
ID: 37840651
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
 
LVL 15

Expert Comment

by:Insoftservice
ID: 37841378
hi,

provide ur both array o/p

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

$i++;
}

please provide echo of this value.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 2

Author Comment

by:smksa
ID: 37841435
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
 
LVL 11

Expert Comment

by:Chris Sandrini
ID: 37841506
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
 
LVL 15

Accepted Solution

by:
Insoftservice earned 2000 total points
ID: 37841536
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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question