inserting table data into data base have same names - php

I have a table with six rows and i have assigned same names to the values to each row , how can i save the six row values into to the database using php in codeigniter
<td>
<input type="text" name="compscheme" id="compscheme" class="form-control m-b-10">
</td>
<td>
<input type="text" name="compbrands" id="compbrands" class="form-control m-b-10">
</td>
<td>
<input type="text" name="companyname" id="companyname" class="form-control m-b-10">
</td>
<td>
<input type="text" name="cmpyscheme" id="cmpyscheme" class="form-control m-b-10">
</td>
</tr>
<tr>
<td>
<input type="text" name="compscheme" id="compscheme" class="form-control m-b-10">
</td>
<td>
<input type="text" name="compbrands" id="compbrands" class="form-control m-b-10">
</td>
<td>
<input type="text" name="companyname" id="companyname" class="form-control m-b-10">
</td>
<td>
<input type="text" name="cmpyscheme" id="cmpyscheme" class="form-control m-b-10">
</td>
in this way i have six rows. should i use multiple array tag or any other way for inserting

you should use array input name like- companyname[] -
<!--
<td>
<input type="text" name="compscheme[]" id="compscheme" class="form-control m-b-10">
</td>
<td>
<input type="text" name="compbrands[]" id="compbrands" class="form-control m-b-10">
</td>
.......
.......
.......
-->
<?php
if(($this->input->server('REQUEST_METHOD')=='POST')){
for ($i=0; $i <count($_POST['cmpyscheme']); $i++) {
$data=array(
'table_column_name'=>$_POST['cmpyscheme'][$i],
);
// for 6 rows
if($i==5){
echo $_POST['cmpyscheme'][$i];
}
}
}
?>

You can get directly value of six row.
$cmpyscheme=$_POST['cmpyscheme'][5];
$compbrands=$_POST['compbrands'][5];
$companyname=$_POST['companyname'][5];
$cmpyscheme=$_POST['cmpyscheme'][5];

Related

How to get two forms on one submit [duplicate]

This question already has answers here:
How to submit 2 forms in one page with a single submit button
(4 answers)
Closed 7 years ago.
I have two forms that look like this:
<h2>First form</h2>
<form id="first-form">
<table>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
</table>
</form>
<!-- some more HTML -->
<h2>Second form</h2>
<form id="second-form">
<label>
<input type="search" name="sentence" value="i like the bananas">
</label>
<label>
<input type="text" name="parse-as" value="S">
</label>
<label>
<select name="include-features">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<label>
<select name="show-earley">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<button type="submit">Parse</button>
</form>
Note that the first form can contain more rows; the user can add more rows to the form. As you can see, there's only one submit button. That's because both forms have to be submitted together. In jQuery I'd do something like this:
$("#second-form").submit(function(event) {
$("first-form").submit();
event.preventDefault();
});
As the front-end developer of the team, I need to pass down the values of both forms to the back-end. Unfortunately I'm at loss how to do this. I figured I could do something like this:
$("#second-form").on("submit", function(event) {
$("#first-form").submit();
$.post("php/do.php", $("#second-form").serialize());
event.preventDefault();
});
$("#first-form").on("submit", function(event) {
$.post("php/do.php", $("#first-form").serializeArray());
event.preventDefault();
});
.serializeArray() seems necessary because of the array-like values in the HTML for #first-form. In the second form, however, we don't need an array. The issue I'm having now is, what code do I need in do.php that can handle the input. How do you access the data, i.e. the serialized forms in PHP?
I tried a number of things such as
<?php
$data = parse_str($_POST["right[]"]);
var_dump($data);
$date1 = parse_str($_POST["right[][0]"]);
var_dump($data);
$data2 = parse_str($_POST["right[][1]"]);
var_dump($data);
?>
But all of these return an Undefined index error, which seems to mean that my "selector" is wrong. How would I go about selecting the array from the first form, and the values of the second form in PHP? And is my jQuery method of submitting first-form together with the second correct?
Why don't you do this :
<h2>First form</h2>
<form id="first-form">
<table>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
</table>
<!-- some more HTML -->
<h2>Second form</h2>
<label>
<input type="search" name="sentence" value="i like the bananas">
</label>
<label>
<input type="text" name="parse-as" value="S">
</label>
<label>
<select name="include-features">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<label>
<select name="show-earley">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<button type="submit">Parse</button>
</form>
Maybe it's not the cleanest way to do it, but it's the easier. Just get the results with :
<?php
$data = parse_str($_POST["right[]"]);
var_dump($data);
$date1 = parse_str($_POST["right[][0]"]);
var_dump($data1);
$data2 = parse_str($_POST["right[][1]"]);
var_dump($data2);
?>

ARRAY FORM DATA RECEIVED IN PHP

I want to received 4,5 row data in php. When I put values in the form like 3 or 4 row than I want to received the same data in php :(
I am trying to make a pos. When the customer order multiple product than the order row automatically increase but there is no limit 5, 10, 15 or else.
And I want to received the data value in same rows like
<form name="data" method="post" action="data_rec.php" enctype="multipart/form-data">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
</table>
<input name="Submit" type="submit" value="SEND">
</form>
<?php
foreach (array_combine($_POST['data'], $_POST['data']) as $i => $data) {
$i."<br />";
echo $data."<br />";
echo "SAGOR"."<br />";
}
?>
Output :
1
SAGOR
2
SAGOR
3
SAGOR
4
SAGOR
5
SAGOR
6
SAGOR
7
SAGOR
8
SAGOR
SAGOR
But I need :
1 2 3 4
SAGOR
5 6 7 8
You can add rows to your form on the browser with JS or JQuery.
You must give each row's elements unique ids/names like: <input name="data2" type="text" id="data2" /> <input name="data3" type="text" id="data3" />...
You will receive them all in you $_POST when the user submit the form

Pass multiple values of same data field through POST

I'm trying to pass multiple values from same parameters of a form using POST, but can't figure out how to proceed. I've used bootstrap css such that I can add multiple products. And I want to process the data of multiple orders by passing the values using POST method.
On clicking 'Add another' link, additional set of data field appear which enables recording of multiple transactions of a same user.
The code is as follows:
<div class="col-xs-5 col-lg-offset-3">
<form action="billingProceed.php" method="post" role="form">
<table id="itemElement">
<tr>
<td>
<select class="form-control">
<option class="form-control"> Customer Name</option>
<option class="form-control"> Customer ID</option>
</select>
</td>
<td><input type="text" name="<?php echo $data["name"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td>
<select class="form-control">
<option class="form-control"> Item Name</option>
<option class="form-control"> Item ID</option>
</select>
</td>
<td ><input type="text" name="<?php echo $data["item"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td style="float:right;">Quantity
</td>
<td><input type="text" name="<?php echo $data["quantity"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td style="float:right;">Price
</td>
<td><input type="text" name="<?php echo $data["price"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td style="float:right;">Discount
</td>
<td><input type="text" name="<?php echo $data["discount"]; ?>" class="form-control" /></td>
</tr>
</table>
<input type="submit" value="Proceed" class="btn btn-primary" />
<p style="float:right;">Add another</p>
You can use an array name.
Example:
<input name="data['name'][1]">
<input name="data['name'][2]">
Firstly you should be aware of the name array, for input names.
HTML Example:
<form>
<a id="add_another" href="#">Add Another</a>
<table>
<tr class="product_item">
<td>
<input type="text" name="product[1][name]" value=""/>
</td>
<td>
<input type="text" name="product[1][item]" value=""/>
</td>
</tr>
<tr id="dummy">
<td>
<input type="text" name="product[0][name]" value=""/>
</td>
<td>
<input type="text" name="product[0][item]" value=""/>
</td>
</tr>
</table>
</form>
On POST, in your PHP script, you will access them as follows:
foreach($_POST['product'] as $product)
{
$name = $product['name'];
$item = $product['item'];
}
Shoot for the JS
//You'll always start with one product row.
var productCount = 1;
$('#add_another').click(function() {
var dummyproduct = $('#dummy').clone();
//Increment Product count
productCount += 1;
//Rename all inputs
dummyproduct.find('input[name^="product[0]"]').each(function(){
$(this).attr('name',$(this).attr('name').replace('product[0]','product['+productCount +']'));
});
//Remove Id from cloned dummy
dummyproduct.removeAttr('id').addClass('product_item');
//Insert row before Dummy
dummyproduct.insertBefore('#dummy');
});

How to insert bulk data with same name field in php mysql

I had tried to insert bulk data with same name field contains multiple rows. But only single row is inserted.
How to insert bulk data as different values to insert into the database.
INSERT INTO table_name (username, luck_number, test, tester) VALUES (('$username', '$luck_number', '$test', '$tester').
<tr>
<td>1</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
#nisha,In your scenario only single row is inserted because variables are same name so it's overridden, Please try below code, It will give you array of fields so you can easily create for-loop & do multiple insert with your query.
<form method="post" name="userdata">
<tr>
<td>1</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<input type="submit" name="submit">
</form>
**Note:**you can worry about the security issue letter. read the answer with the comment.
Store them in array by adding [ ] this in your input field
<tr>
<td>1</td>
<form action="" method="POST">
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr></td><input type="submit" name="submit" value="submit"/><tr></td>
</form>
<?php
//connect with your database
for($i=0;$i<count($_POST['luck_number']);$i++)
{
//set the value for variable
$luck_number=$_POST['luck_number'][$i];
$test=$_POST['test'][$i];
$tester=$_POST['tester'][$i];
//run your query
//INSERT INTO table_name (username, luck_number, test, tester) VALUES (('$username', '$luck_number', '$test', '$tester').
}
First of all change all field names by adding [] at the end.
Second step, to parsing all values you may use something like this
for($i=0; $i < $count($_GET['username']); $i++)
{
$username = $_GET['username'][$i];
$luck_number= $_GET['luck_number'][$i];
$big= $_GET['big'][$i];
$test= $_GET['test'][$i];
$tester= $_GET['tester'][$i];
// insert into database
}
The reason of inserting single row instead of multiple rows is your input field name. You are using same name in different input field so when the server gets the reply it replace the duplicate name and the last occurrence is outputted.
The thing you have to do is to use array. If you use known number of rows then you can simply use a for loop to insert data.
<tr>
<td>1</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<?php
for ($i=0; $i<count($_POST['username']); $i++)
{
mysql_query("INSERT INTO table_name (`username`, `luck_number`, `test`, `tester`) VALUES (('".$_POST['username'][$i]."', '".$_POST['luck_number'][$i]."', '".$_POST['test'][$i]."', '".$_POST['tester'][$i]."')");
}
?>
Note: Sanitizing variable is always been a good practice and strongly recommended.

Post array with multiple same input names

Im having a form that posts an array (invoice[]) of company and invoice items.
My form got some inputs with the same name and i want to push them into the array and post them to the database as one row for each item. How can i push the items in item[0], item[1]...etc, in the same array to store it properly in the database ?
This is the input group that is been repeated in my form:
<tr>
<td class="col-xs-0">
<input type="checkbox" name="selected[]" class="checkall">
</td>
<td class="col-xs-5">
<textarea type="text" name="invoice[description]" class="form-control" id="description" rows="1" value="{{invdtl_itemDescription}}"></textarea>
</td>
<td class="col-xs-1">
<input type="number" name="invoice[unit]" class="form-control" id="unit" maxlength="5" pattern="\d+(\.\d{2})?">
</td>
<td class="col-xs-1">
<input type="number" name="invoice[qty]" class="form-control" id="qty" maxlength="5" pattern="\d+(\.\d{2})?">
</td>
<td class="col-xs-1">
<input type="number" name="invoice[price]" class="form-control" id="price" maxlength="5" pattern="\d+(\.\d{2})?">
</td>
<td class="col-xs-1">
<input type="number" name="invoice[discount]" class="form-control" id="discount" maxlength="5" pattern="\d+(\.\d{2})?">
</td>
<td class="col-xs-1">
<input type="number" name="invoice[discountPrice]" class="form-control" id="discountPrice" maxlength="5" pattern="\d+(\.\d{2})?">
</td>
<td class="col-xs-1">
<input type="number" name="invoice[total]" class="form-control" id="total" maxlength="5" pattern="\d+(\.\d{2})?">
</td>
<td class="col-xs-1">
<input type="number" name="invoice[vat]" class="form-control" id="vat" value="23" maxlength="3" pattern="\d+(\.\d{2})?">
</td>
</tr>
The array is posted with $.ajax to the database in two tables (one for invoice_header and one for invoice_details. Invoice items (the one that is been repeated in the form) should be stored in the invoice_details each row at a time.
Form data:
invoice[client]: 1
invoice[type]: receipt
invoice[invoice]: 155
invoice[date]: 2014-07-07
invoice[shipmethod]: 1
===== Item #1 =====
invoice[description]: Test
invoice[unit]: 1
invoice[qty]: 1
invoice[price]: 1
invoice[discount]: 1
invoice[discountPrice]: 1
invoice[total]: 1
invoice[vat]: 23
===== Item #2 =====
invoice[description]: Test 2
invoice[unit]: 2
invoice[qty]: 2
invoice[price]: 2
invoice[discount]: 2
invoice[discountPrice]: 2
invoice[total]: 2
invoice[vat]: 23

Categories