[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to submit multiple php array to mysql ?

Posted on 2012-04-12
6
Medium Priority
?
413 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

In this article, we’ll look at how to deploy ProxySQL.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

656 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