Solved

How to submit multiple php array to mysql ?

Posted on 2012-04-12
6
404 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
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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:un1x86
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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Read about why website design really matters in today's demanding market.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now