Codeigniter Insert Multiple Rows in SQL - mysql

I am fresh to Codeigniter. I have a form which looks something like this.
<tr>
<td><input type="text" name="Name[0]" value=""></td>
<td><input type="text" name="Address[0]" value=""><br></td>
<td><input type="text" name="Age[0]" value=""></td>
<td><input type="text" name="Email[0]" value=""></td>
</tr>
<tr>
<td><input type="text" name="Name[1]" value=""></td>
<td><input type="text" name="Address[1]" value=""><br></td>
<td><input type="text" name="Age[1]" value=""></td>
<td><input type="text" name="Email[1]" value=""></td>
</tr>
There may be from 0 to n rows, usually 5 to 10 rows. How do I insert them in SQL? Is this possible with Codeigniter or should I use a native PHP script?
$name=$_POST['Name'];
$address=$_POST['Address'];
$age=$_POST['Age'];
$email=$_POST['Email'];
$count = count($_POST['Name']);
for($i=0; $i<$count; $i++) {
$data = array(
'name' => $name[$i],
'address' => $address[$i],
'age' => $age[$i],
'email' => $email[$i],
);
$this->db->insert('mytable', $data);
}
I did this. It works. But the solution seems inelegant.
kevtrout's answer looks better but is currently throwing a lot of errors.
Is there any way to insert all data at one go?

Multiple insert/ batch insert is now supported by codeigniter. It will firing one query rather than firing too many queries.
$data =array();
for($i=0; $i<$count; $i++) {
$data[$i] = array(
'name' => $name[$i],
'address' => $address[$i],
'age' => $age[$i],
'email' => $email[$i],
);
}
$this->db->insert_batch('mytable', $data);

Make your form like this:
<tr>
<td><input type="text" name="user[0][name]" value=""></td>
<td><input type="text" name="user[0][address]" value=""><br></td>
<td><input type="text" name="user[0][age]" value=""></td>
<td><input type="text" name="user[0][email]" value=""></td>
</tr>
<tr>
<td><input type="text" name="user[1][name]" value=""></td>
<td><input type="text" name="user[1][address]" value=""><br></td>
<td><input type="text" name="user[1][age]" value=""></td>
<td><input type="text" name="user[1][email]" value=""></td>
</tr>
Then you can simply do:
foreach($_POST['user'] as $user)
{
$this->db->insert('mytable', $user);
}

The form you show will create a $_POST array with indexes of name, address, age, and email. Each of these will contain the n number of "rows" your form provides. For example:
array(
'name' => array('First Name','Second Name'),
'address' => array ('First Address','Second Address'),
'age' => array('First Age','Second Age'),
'email' => array('First Email', 'Second Email')
);
You may want to rearrange that array into one where each index of the array is a "person". This will make inserting the information into your database simpler.
//subtract 1 from below to account for the assumed submit button
$number_of_rows = count($_POST)-1;
for($i=0;$i<$number_of_rows;$i++){
$person[]['name'] = $this->input->post('Name')[$i];
$person[]['address'] = $this->input->post('Address')[$i];
$person[]['age'] = $this->input->post('Age')[$i];
$person[]['email'] = $this->input->post('Email')[$i];
}
This will create something like this:
array(
0=>array('First Name','First Address','First Age','First Email'),
1=>array ('Second Name','Second Address','Second Age','Second Email')
);
Now you can use a loop to insert each person into the db.
for($y=0;$y<count($person);$y++){
$this->db->insert('mytable',$person[$y];
}

Related

how to insert multiple row data based on checkbox selection in Laravel

I'm stuck with multiple row insertion in laravel. I keep on getting an error which says:
Trying to access array offset on value of type null.
blade.php file:
<tbody>
#forelse($GIV as $GIV)
<tr>
<td> <input type="checkbox" name="status[{{$GIV->id}}]" id="status" value="Issued" ></td>
<td> <input type="text" class="form-control" id="id" name="item_id[{{$GIV->id}}]" hidden="" value="{{$GIV->id}}">{{$GIV->id}}</td>
<td> <input type="text" class="form-control" id="pr_no" name="pr_no[{{$GIV->id}}]" hidden="" value="{{$GIV->pr_no}}">{{$GIV->pr_no}}</td>
<td> <input type="text" class="form-control" id="dep_name" name="dep_name[{{$GIV->id}}]" hidden="" value="{{$GIV->dep_name}}">{{$GIV->dep_name}}</td>
<td> <input type="hidden" class="form-control" id="item_name" name="item_name[{{$GIV->id}}]" value="{{$GIV->item_name}}">{{$GIV->item_name}}</td>
<td> <input type="text" class="form-control" id="description" name="description[{{$GIV->id}}]" hidden="" value="{{$GIV->description}}" >{{$GIV->description}}</td>
<td> <input type="number" class="form-control" id="item_qty" name="item_qty[{{$GIV->id}}]" hidden="" value="{{$GIV->item_qty}}">{{$GIV->item_qty}}</td>
<td> <input type="text" class="form-control" id="unit_measure" name="unit_measure[{{$GIV->id}}]" hidden="" value="{{$GIV->unit_measure}}">{{$GIV->unit_measure}}</td>
<td> <input type="number" class="form-control" id="authorized_qty" name="authorized_qty[{{$GIV->id}}]" hidden="" value="{{$GIV->authorized_qty}}">{{$GIV->authorized_qty}}</td>
</tr>
#empty
<tr><td colspan="16"><center>No Data Found</center</td></tr>
#endforelse
</tbody>
Controller file:
public function addDataGIV(Request $request)
{
$data =request()->all;
foreach(request()->status as $status) {
DB::table('g_i_v_s')->insert(
[
'item_id' =>$data['item_id'][$status],
'item_name' =>$data['item_name'][$status],
'description' =>$data['description'][$status],
'item_qty' =>$data['item_qty'][$status],
'unit_measure' =>$data['unit_measure'][$status],
'authorized_qty'=>$data['authorized_qty'][$status],
'dep_name' =>$data['dep_name'][$status],
'pr_no' =>$data['pr_no'][$status],
]
);
}
}
When a user selects the checkbox and clicks the submit button, I want only the selected checkbox row including the checkbox value to be stored into the database, without any errors.
You're actually quite close, but you're accessing your Array indices wrong; $status, if defined properly, would be Issued, which is not what you want. Also, request()->all is null, but request()->all() (with the ()) would be an Array of your form input; hence the error when trying to use $data[...][...]; later.
This code should work (Note: you don't need $data at all here):
foreach(request()->input('status', []) as $index => $status) {
DB::table('g_i_v_s')->insert([
'item_id' => request()->input("item_id.{$index}"),
'item_name' => request()->input("item_name.{$index}"),
'description' => request()->input("description.{$index}"),
'item_qty' => request()->input("item_qty.{$index}"),
'unit_measure' => request()->input("unit_measure.{$index}"),
'authorized_qty' => request()->input("authorized_qty.{$index}"),
'dep_name' => request()->input("dep_name.{$index}"),
'pr_no' => request()->input("pr_no.{$index}"),
]);
}
Alternative syntax would be request->input('item_id')[$index], like:
foreach(request()->input('status', []) as $index => $status) {
DB::table('g_i_v_s')->insert([
'item_id' => request()->input('item_id')[$index],
'item_name' => request()->input('item_name')[$index],
'description' => request()->input('description')[$index],
'item_qty' => request()->input('item_qty')[$index],
'unit_measure' => request()->input('unit_measure')[$index],
'authorized_qty' => request()->input('authorized_qty')[$index],
'dep_name' => request()->input('dep_name')[$index],
'pr_no' => request()->input('pr_no')[$index],
]);
}
Basically, when looping over request()->input('status', []), you have access to $index, and $status (which is not referenced, but that's ok). $index will be equivalent to whatever $giv->id is on the front-end, and each field exists as an Array Index with that value, so looping and referencing is super easy.

The post method is not supported for this route

I'm new to laravel and I want to submit a form which have three attachments plus some arrays
but when I click submit button laravel says
"The POST method is not supported for this route. Supported methods: GET, HEAD."
I've searched a lot but couldn't find any errors
My View
<div>
<form action="insertquotation" method="post" enctype="multipart/form-data">
#csrf
<table border="1" id="formTable">
<thead>
<tr>
<td>procurment_request_number</td>
<td>quotationer</td>
<td>shop_name</td>
<td>shop_account_number</td>
<td>shop_permit</td>
<td>shop_quatation</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="procurment_request_number" id="procurment_request_number" value="{{$result->first()->request_number}}">
{{--<select name="procurment_request_number[]" id="procurment_request_number">--}}
{{--#foreach($result as $results)--}}
{{--<option value="{{$results->request_number}}">{{$results->request_number}}</option>--}}
{{--#endforeach--}}
{{--</select>--}}
</td>
<td><input type="text" id="quotationer1" name="quotationer1"></td>
<td><input type="text" name="shop_name1" id="shop1_name"></td>
<td><input type="text" name="shop_account_number1" id="shop1_account_number"></td>
<td><input type="text" name="shop_permit1" id="shop1_permit"></td>
<td><input type="file" accept="application/pdf" name="shop_quatation1" id="shop_quatation"></td>
</tr>
<tr>
<td></td>
<td><input type="text" id="quotationer" name="quotationer2"></td>
<td><input type="text" name="shop_name2" id="shop1_name"></td>
<td><input type="text" name="shop_account_number2" id="shop1_account_number"></td>
<td><input type="text" name="shop_permit2" id="shop1_permit"></td>
<td><input type="file" accept="application/pdf" name="shop_quatation2" id="shop_quatation"></td>
</tr>
<tr>
<td></td>
<td><input type="text" id="quotationer" name="quotationer3"></td>
<td><input type="text" name="shop_name3" id="shop1_name"></td>
<td><input type="text" name="shop_account_number3" id="shop1_account_number"></td>
<td><input type="text" name="shop_permit3" id="shop1_permit"></td>
<td><input type="file" accept="application/pdf" name="shop_quatation3" id="shop_quatation"></td>
</tr>
<tr>
<td>Item_ID</td>
<td>Quantity</td>
<td>Unit_ID</td>
<td>description</td>
<td>Shop1_price</td>
<td>Shop2_price</td>
<td>Shop3_price</td>
</tr>
#foreach($result as $results)
<tr>
<td><input type="text" name="itemid[]" id="itemid" value="{{$results->id}}"></td>
<td><input type="text" name="quantity[]" id="quantity" value="{{$results->quantity}}"></td>
<td><input type="text" name="units_id[]" id="units_id" value="{{$results->units_id}}"></td>
<td><input type="text" name="description[]" id="description" value="{{$results->description}}"></td>
<td><input type="text" name="shop1_price[]" id="shop1_price"></td>
<td><input type="text" name="shop2_price[]" id="shop2_price"></td>
<td><input type="text" name="shop3_price[]" id="shop3_price"></td>
</tr>
</tbody>
#endforeach
</table>
{{--<input value="addrow" type="button" id="addrow" onclick="javascript: addRow();">--}}
<input type="submit" value="submit">
</form>
</div>
My Controller
public function insertquotation(request $request)
{
if ($file = $request->file('shop1_quatation')) {
$name = $file->getClientOriginalName();
$file->move('procurement_files', $name);
$file2 = $request->file('shop2_quatation');
$name2 = $file2->getClientOriginalName();
$file2->move('procurement_files', $name2);
$file3 = $request->file('shop2_quatation');
$name3 = $file2->getClientOriginalName();
$file3->move('procurement_files', $name3);
$procurment_request_number = $request->input('procurment_request_number');
$quotationer1 = $request->input('quotationer1');
$quotationer2 = $request->input('quotationer2');
$quotationer3 = $request->input('quotationer3');
$shop_name1 = $request->input('shop_name1');
$shop_account_number1 = $request->input('shop_account_number1');
$shop_permit1 = $request->input('shop_permit1');
$shop_name2 = $request->input('shop_name2');
$shop_account_number2 = $request->input('shop_account_number2');
$shop_permit2 = $request->input('shop_permit2');
$shop_name3 = $request->input('shop_name3');
$shop_account_number3 = $request->input('shop_account_number3');
$shop_permit3 = $request->input('shop_permit3');
$numbers = $request->input('itemid');
for ($i = 0; $i < count($numbers); $i++) {
$itemid = $request->input('itemid');
$shop1_price = $request->input('shop1_price');
$shop2_price = $request->input('shop2_price');
$shop3_price = $request->input('shop3_price');
DB::insert("INSERT INTO procurment_quotations (`procurment_request_number`, `itemid`, `quotationer1`, `quotationer2`, `quotationer3`, `shop1_name`, `shop1_account_number`, `shop1_permit`, `shop1_quatation`, `shop1_price`, `shop2_name`, `shop2_account_number`, `shop2_permit`, `shop2_quatation`, `shop2_price`, `shop3_name`, `shop3_account_number`, `shop3_permit`, `shop3_quatation`, `shop3_price`, `created_at`, `updated_at`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)", [$procurment_request_number, $itemid[$i],$quotationer1,$quotationer2,$quotationer3,$shop_name1,$shop_account_number1,$shop_permit1,$name,$shop1_price,$shop_name2,$shop_account_number2,$shop_permit2,$name2,$shop2_price,$shop_name3,$shop_account_number3,$shop_permit3,$name3,$shop3_price]);
}
}
}
and my route
Route::Post('insertquotation','Home#insertquotation');
The error is
"The POST method is not supported for this route. Supported methods: GET, HEAD."
Give the route a name in your routes/web.php file:
Route::post('insertquotation','Controllername#methodname')
->name('insertquotation');
And then change the form action in your blade file to point to that route:
<form action="{{ route('insertquotation') }}" method="post" enctype="multipart/form-data">
Couple of things which I need to point out on your code
It is always better to use route names while creating routes so that when you need to change the route URL to something else, you only need to change it inside routes file (not view files)
Route::post('insertquotation','Home#insertquotation')->name('insertquotation');
Now inside your view, you need to do this
<form method="POST" enctype="multipart/form-data" action="{{ route('insertquotation') }}"> ... </form>
In the future when you want (maybe not) to change the URL to /quotation/insert, all you have to do is to change it inside the routes file.
Now, inside your controller,
There is a function to check whether the file exists $request->hasFile('shop1_quatation') which more correct to use inside an if condition rather passes in the file.
Also, it is much better to use Laravel eloquent for inserting data into the database. Learn more here
I think from action="insertquotation", you should make it into action="{{url('insertquotation')}}"
And may I see the codes in your routes/web.php file? you should declare the route insertquotation there
For example:
Route::post('insertquotation', 'YourController#yourMethod');

In laravel i can show the product name, how to show it in vue.js

This is the view route
public function showOrder(Order $order)
{
return view('return.sales-return-show', compact('order'));
}
This is output for dd($order);
#original: array:18 [▼
"id" => 7
"company_id" => 1
"order_type" => 1
"order_no" => "12"
"date" => "2019-01-16"
"status" => "1"
"transaction_raw" => "[{"amount":"82264","transaction_type":3,"payment_type":1,"owner_type":"App\\Model\\Customer","owner_id":"1"},{"amount":"0","transaction_type":4,"payment_type":1 ▶"
"line_items" => "[{"customer_id":"1","product_id":"10","unit_id":"2","quantity":"5","price":"2700","total_price":"13500"},{"customer_id":"1","product_id":"43","unit_id":"1","quantity":"52","price":"7","total_price":"364"},{"customer_id":"1","product_id":"9","unit_id":"2","quantity":"18","price":"3800","total_price":"68400"}] ◀"
"total" => 82264.0
"discount" => 0.0
"sub_total" => 82264.0
"paid" => 0.0
"due" => 82264.0
"supplier_id" => 0
"customer_id" => 1
"others_fin" => "{"transport":"0","type":"income"}"
"created_at" => "2019-01-16 19:13:27"
"updated_at" => "2019-01-16 19:13:27"
]
This is the loop where I can show the product name
#foreach($order->items as $item)
{{$item->product->name}}
#endforeach
This is my json route
public function json(Order $order)
{
return response()->json(['orders' => $order]);
}
JSON Data:
{
"orders":{
"id":7,
"company_id":1,
"order_type":1,
"order_no":"12",
"date":"2019-01-16",
"status":"1",
"transaction_raw":[
{
"amount":"82264",
"transaction_type":3,
"payment_type":1,
"owner_type":"App\Model\Customer",
"owner_id":"1"
},
{
"amount":"0",
"transaction_type":4,
"payment_type":1,
"owner_type":"App\Model\Customer",
"owner_id":"1",
"account_head_id":1
}
],
"line_items":[
{
"customer_id":"1",
"product_id":"10",
"unit_id":"2",
"quantity":"5",
"price":"2700",
"total_price":"13500"
},
{
"customer_id":"1",
"product_id":"43",
"unit_id":"1",
"quantity":"52",
"price":"7",
"total_price":"364"
},
{
"customer_id":"1",
"product_id":"9",
"unit_id":"2",
"quantity":"18",
"price":"3800",
"total_price":"68400"
}
],
"total":82264,
"discount":0,
"sub_total":82264,
"paid":0,
"due":82264,
"supplier_id":0,
"customer_id":1,
"others_fin":"{\"transport\":\"0\",\"type\":\"income\"}",
"created_at":"2019-01-16 19:13:27",
"updated_at":"2019-01-16 19:13:27"
}
}
Now I need to show PRODUCT NAME here
<tr v-for="order in orders.line_items">
<td></td>
<td><input name="" v-model="PRODUCT NAME" class="form-control"></td>
<td>{{order.product_id}}</td>
<td></td>
<td><input name="" v-model="order.quantity" class="form-control"></td>
<td><input name="" v-model="order.price" class="form-control" disabled></td>
<td><input name="" v-model="order.quantity * order.price" class="form-control" disabled></td>
<td></td>
</tr>
How to do it...?
It seems your product name is missing in JSON you provided. As far as I see, your product is related with entity "line_item". You need to "eager load" your product with your "line_item"
Change your json action to this:
public function json($id)
{
$order = Order::with(['items', 'items.product'])
->where('id', $id)
->first();
return response()->json(['order' => $order]);
}
And then in your vue template:
<tr v-for="order_item in order.items">
<td></td>
<td><input name="" v-model="order_item.product.name" class="form-control" type="text"></td>
<td>{{order_item.product_id}}</td>
<td></td>
<td><input name="" v-model="order_item.quantity" class="form-control" type="text"></td>
<td><input name="" v-model="order_item.price" class="form-control" disabled type="text"></td>
<td><input name="" v-model="order_item.quantity * order_item.price" class="form-control" disabled type="text"></td>
<td></td>
</tr>
I am not sure that code will work as it is, but the main idea is to "eager load" order lines ("line_items") and products with your object of order, which you get by id in your json action. After that, you iterate through order lines in your vue template.
Yea, the problem is solved now.
This is my json data below
public function json(Order $order)
{
$products = $order->line_items;
$productIds = array_column($products, 'product_id');
$orderedProducts = Product::whereIn('id', $productIds)->get()->groupBy('id');
return response()->json(['orders' => $order, 'orderedProducts' =>$orderedProducts->toArray() ]);
}
And this is AdReturn.vue
<tr v-for="order in orders.line_items">
<td><input name="" v-model="orderedProducts[order.product_id][0].name" class="form-control" disabled></td>
<td><input name="" v-model="order.quantity" class="form-control"></td>
<td><input name="" v-model="order.price" class="form-control" disabled></td>
<td><input name="" v-model="order.quantity * order.price" class="form-control" disabled></td>
</tr>

How to update the existing rows add insert new rows in table in laravel for multiple array values

i have a table of data with array input values , as like below
<table>
<tr>
<td>Name</td>
<td>Email</td>
<td>Phone</td>
</tr>
<tr>
<td><input type="text" name="user_id[]" value="1"></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="email[]"></td>
<td><input type="text" name="phone[]"></td>
</tr>
<tr>
<td><input type="text" name="user_id[]" value="2"></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="email[]"></td>
<td><input type="text" name="phone[]"></td>
</tr>
<tr>
<td><input type="text" name="user_id[]" value=""></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="email[]"></td>
<td><input type="text" name="phone[]"></td>
</tr>
<tr>
<td><input type="text" name="user_id[]" value=""></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="email[]"></td>
<td><input type="text" name="phone[]"></td>
</tr>
</table>
And i m trying to update the first two rows with user_id value and trying to insert last two rows as new rows.
I m using the below insert query in laravel controller :
if(!empty(Input::get('user_id')))
{
for($i = 0; $i < count(Input::get('name')); $i++)
{
$id = Input::get('user_id')[$i];
$data = User::find($id);
$data->name = Input::get('name')[$i];
$data->email = Input::get('email')[$i];
$data->phone = Input::get('phone')[$i];
$data->save();
}
}
if(empty(Input::get('user_id')))
{
for($i = 0; $i < count(Input::get('name')); $i++)
{
$new = new User();
$new->name = Input::get('name')[$i];
$new->email = Input::get('email')[$i];
$new->phone = Input::get('phone')[$i];
$new->save();
}
}
But its not working properly . can somebody suggest me any solution for this ?
You should be using a foreach loop to, well, loop through the fields and do something if it is empty or not.
Maybe, something like this would help you.
$fields = Input::get('user_id');
foreach($fields as $field)
{
if(! empty($field))
{
// field is not empty
// update here
}
else
{
// field is empty
// do something here
}
}

Append text from HTML to XML

G'day,
As a part of my assignment, I have to store the details entered by a user in a registration form into an XML document.
I have managed to do that BUT the problem is when a new user registers, the old details are over-written with the details of the new user. So, there is ONLY 1 user details in the XML document in the end.
I was wondering if there's any way to save the details of new user while "preserving" the old details.
Any help is appreciated :)
HTML Code -
<form id="rego" name="rego" method="get" action="register2.php">
<table width="719" border="0">
<tr>
<td><div align="right">Email Address</div></td>
<td><label for="email"></label>
<input type="text" name="email" id="email" maxlength="50" /></td>
</tr>
<td width="376"><div align="right">First Name</div></td>
<td width="333"><label for="firstName"></label>
<input type="text" name="firstName" id="firstName" maxlength="15" /></td>
<td><div id="underInput"></div></td>
</tr>
<tr>
<td><div align="right">Last Name</div></td>
<td><label for="lastName"></label>
<input type="text" name="lastName" id="lastName" maxlength="20" /></td>
</tr>
<tr>
<td><div align="right">Phone Number</div></td>
<td><label for="phoneNumber"></label>
<input type="text" name="phoneNumber" id="phoneNumber" maxlength="10" /></td>
</tr>
<tr>
<tr>
<td><div align="right">Password</div></td>
<td><label for="password"></label>
<input type="password" name="password" id="password" maxlength="30" /></td>
</tr>
<tr>
<td><div align="right">Re-type password</div></td>
<td><label for="confirmPassword"></label>
<input type="password" name="confirmPassword" id="confirmPassword" maxlength="30" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="Submit" name="Submit" id="Submit" value="Submit"/><!--onclick="saveForm();"--></td>
</tr>
PHP Code (I'm using DOM) -
<?php
$CustomerEmail = $_GET["email"];
$CustomerFName = $_GET["firstName"];
$CustomerLName = $_GET["lastName"];
$CustomerPhoneNumber = $_GET["phoneNumber"];
$CustomerPassword = $_GET["password"];
$CustomerConfirmPassword = $_GET["confirmPassword"];
$doc = new DomDocument('1.0');
$root = $doc->createElement('customers');
$root = $doc->appendChild($root);
$customer = $doc->createElement('customer');
$customer = $root->appendChild($customer);
$email = $doc->createElement('email');
$email = $customer->appendChild($email);
$valueEmail = $doc->createTextNode($CustomerEmail);
$valueEmail = $email->appendChild($valueEmail);
$fName = $doc->createElement('firstname');
$fName = $customer->appendChild($fName);
$valueFName = $doc->createTextNode($CustomerFName);
$valueFName = $fName->appendChild($valueFName);
$lName = $doc->createElement('lastname');
$lName = $customer->appendChild($lName);
$valueLName = $doc->createTextNode($CustomerLName);
$valueLName = $lName->appendChild($valueLName);
$phone = $doc->createElement('phone');
$phone = $customer->appendChild($phone);
$valuePhone = $doc->createTextNode($CustomerPhoneNumber);
$valuePhone = $phone->appendChild($valuePhone);
$password = $doc->createElement('password');
$password = $customer->appendChild($password);
$valuePassword = $doc->createTextNode($CustomerPassword);
$valuePassword = $password->appendChild($valuePassword);
$confirmPassword = $doc->createElement('confirmpassword');
$confirmPassword = $customer->appendChild($confirmPassword);
$valueConfirmPassword = $doc->createTextNode($CustomerConfirmPassword);
$valueConfirmPassword = $confirmPassword->appendChild($valueConfirmPassword);
$doc->save('customer2.xml');
?>
I apologise for any inconvience.
You need to load your existing file, before creating a new one each time and override the previous.
.....
$doc = new DomDocument('1.0');
if($xml = file_get_contents( 'customer2.xml')){
$doc->loadXML( $xml, LIBXML_NOBLANKS );
$root=$doc->getElementsByTagName('customers')->item(0);
}else{
$root = $doc->createElement('customers');
$root = $doc->appendChild($root);
}
$customer = $doc->createElement('customer');
$customer = $root->appendChild($customer);
.....
$doc->save('customer2.xml');