4 Replies - 21932 Views - Last Post: 28 August 2011 - 08:12 AM Rate Topic: -----

#1 grydz3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 26-August 11

how to insert data using multiple input with same name?

Posted 27 August 2011 - 09:49 PM

my form goes like this.
<form action="order.php" method="post" name="order">

Description copy :<input type="file" name="desc"/>
Dr's Name:<input type="text" name="drname" maxlength="15"/>
License #:<input type="text" name="license" maxlength="15"/>

<input type="text" name="med[]" id="med_1"/><input type="text" name="gen[]" id="gen_1"/><input type="text"name="qty[]"id="qty_1"/>

<input type="text" name="med[]" id="med_2"/><input type="text" name="gen[]" id="gen_2"/><input type="text"name="qty[]"id="qty_2"/>

<input type="text" name="med[]" id="med_3"/><input type="text" name="gen[]" id="gen_3"/><input type="text"name="qty[]"id="qty_3"/>

<input type="submit" value="submit order"/>


i try to test the array!
foreach ($_POST['med'] as $med) // set variable for array 
foreach ($_POST['gen'] as $gen)
foreach ($_POST['qty'] as $qty)

if(!empty($med) && is_array($med) && !empty($gen) && is_array($gen) && !empty($qty) && is_array($qty)){		// test if not empty and if it is an array 
echo $med.$gen.$qty;
}else{
echo "problem ";

NOW I GET THE PROBLEM ERROR . HOPE SOMEBODY CAN HELP. HOW TO INSERT THE DATA. USING THE ARRAYS PER ROW. thanks IN ADVANCE

This post has been edited by grydz3: 27 August 2011 - 09:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how to insert data using multiple input with same name?

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12772
  • View blog
  • Posts: 45,965
  • Joined: 27-December 08

Re: how to insert data using multiple input with same name?

Posted 27 August 2011 - 09:59 PM

Textfields should not have the same name. Radio buttons and checkboxes should, but not textfields. You need to rethink your design on the HTML side.
Was This Post Helpful? 0
  • +
  • -

#3 grydz3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 26-August 11

Re: how to insert data using multiple input with same name?

Posted 27 August 2011 - 10:17 PM

oh ,meaning an array will not work and i have to do the insertion per row individually? thanks again . do you have any advice what is the good approach. sorry if im being stupid. THANKS AGAIN and have a great day.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: how to insert data using multiple input with same name?

Posted 27 August 2011 - 10:43 PM

Your IF statement will always be false because is_array($med), is_array($gen) and is_array($qty) will evaluate to false since $_POST['med'], $_POST['gen'] and $_POST['qty'] are not multidimensional arrays.
You also shouldn't have a nested loop in this case since you're going to get a lot of useless iterations with $_POST['gen'] and $_POST['qty'].
if(is_array($_POST['gen']) && is_array($_POST['med']) && is_array($_POST['qty'])) {
	// ... code
	for($i = 0; $i < count($_POST['gen']); $i++) {
		// ... reference index of arrays
		// $_POST['gen'][$i], $_POST['med'][$i] ... etc
	}
}



@macosxnerd101 That method is actually not limited to radio buttons and check-boxes.
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12772
  • View blog
  • Posts: 45,965
  • Joined: 27-December 08

Re: how to insert data using multiple input with same name?

Posted 28 August 2011 - 08:12 AM

Like I said- "should" not "could". :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1