jayeshkv
jayeshkv

Reputation: 2208

insert multiple records from html to db via php

So Basically i have a form with this layoutthe layout

Let me explain the layout first , when ever the user presses the + another row gets added inside the form and vice versa to the -. After filling out the text fields whenever the user presses the add button the values entered within the table must be inserted inside the table, and for this i redirected the form to the php page using get method and lets say the url somewhat looks like

host.com/index.html?id=1&name=n1&age=12&location=location1&id=2&name=n2&age=14&location=location2

i can segregate the values in the php file using this

$_GET['id']; and so on...

and now the problem that i'm facing is i can get the values only once, say i can get only the values for the elements in the id 1. what i want is , i wanna insert the 2 rows in a single query and i'm not able to get multiple row values via the method i use

Upvotes: 2

Views: 180

Answers (3)

Bill Heller
Bill Heller

Reputation: 250

If you name the fields with square brackets, PHP will create an array of values for you

<input type="text" name="myFieldName[]" value="Val1" />
<input type="text" name="myFieldName[]" value="Val2" />

Will result in

$_REQUEST['myFieldName'][0];  //  Val1
$_REQUEST['myFieldName'][1];  //  Val2

Upvotes: 1

donald123
donald123

Reputation: 5739

Either you use unique request-param-names eg

id1=

or you build an array by using

id[]=1&name[]=n1

Upvotes: 1

Phil
Phil

Reputation: 164733

Change your input names to the following format

name="id[]"

When submitted, PHP will transform each key into an array.

Also, you should really use POST for this data. I'm not sure why you're redirecting anything. Just set the form's action attribute to your PHP file with method="POST" and submit normally.

Upvotes: 1

Related Questions