how to add multiple arrays in form in mysql database php? - php

I have a form that is fetching database entries of a product. I have added multiple rows of the products in a form, and I need help in database insertion.
I want data to be added in a single row and I have multiple arrays.
<form id='students' method='post' name='students' action='add-
<div class="table-responsive">
<table class="table">
<tr>
<th><input class='check_all' type='checkbox' onclick="select_all()"/></th>
<th>S. No</th>
<th>BarCode</th>
<th>HSN</th>
<th>Product Name</th>
<th>MRP</th>
<th>CGST</th>
<th>SGST</th>
</tr>
<tr>
<td><input type='checkbox' class='case'/></td>
<td><span name='snum[]' id='snum'>1.</span></td>
<td><input class="form-control" type='text' id='countryname_1' name='countryname[]'/></td>
<td><input class="form-control" type='text' id='country_no_1' name='country_no[]'/></td>
<td><input class="form-control" type='text' id='phone_code_1' name='phone_code[]'/></td>
<td><input class="form-control" type='text' id='country_code_1' name='country_code[]'/> </td>
<td><input class="form-control" type='text' id='cgst_1' name='cgst1[]'/> </td>
<td><input class="form-control" type='text' id='sgst_1' name='sgst1[]'/> </td>
</tr>
</table>
<input type="submit" value="submit">
<button type="button" class='btn btn-danger delete'>- Delete</button>
<button type="button" class='btn btn-success addmore'>+ Add More</button>
</div>
</form>

If I understand your question correctly, you are trying to store arrays in database. It is not possible per se, but you can use the serialize() function and unserialize() when you fetch data.
PHP manual

Related

How to make edit button with php, html only no mysql

I want to bring many $_REQUEST from Tag but only $_REQUEDT['editid'] works. how to solve it.
echo "<td><a href=product.php?editid=$id, editname=$name, editqty=$qty, editprice=$price>Edit</a></td>";
//when I put $_REQUEST['editid'] in input it works but others not.
<form action="product.php">
<table>
<tr>
<td>ID</td>
<td><input type="text" name="tid" value="<?php if(isset($_REQUEST['editid'])){echo $_REQUEST['editid'];} ?>"></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="tname" value="<?php if(isset($_REQUEST['editname'])){echo $_REQUEST['editname'];}?>"></td>
</tr>
<tr>
<td>Qty</td>
<td><input type="text" name="tqty" ></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="tprice" ></td>
</tr>
<tr>
<td></td>
<td><button type="submit" name="btn-add">Add</button>
<button type="submit" name="btn-clear">Clear</button></td>
</tr>
</table>
</form>
The URL parameters after the 1st one should be separated by & (not ,)
Change from
<a href=product.php?editid=$id, editname=$name, editqty=$qty, editprice=$price>
to
<a href="product.php?editid=$id&editname=$name&editqty=$qty&editprice=$price">

Multiple Form Submit - Laravel

I have my edit.blade.php file as below mentioned
<table class="table">
<thead>
<tr>
<th>Vendor</th>
<th>Item</th>
<th>Qty</th>
<th>Rate</th>
<th>Total</th>
<th>Description</th>
<th>Job No</th>
</tr>
</thead>
<tbody>
#foreach ($po_items as $po_item)
<tr>
<td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
<td><input type="text" class="form-control" name="item_name[]" id="item_name" value="{{$po_item->cost_item->item->name}}" readonly></td>
<td><input type="text" class="form-control" name="qty[]" id="qty" value="{{$po_item->qty}}"></td>
<td><input type="text" class="form-control" name="rate[]" id="rate" value="{{$po_item->rate}}"></td>
<td><input type="text" class="form-control" name="total[]" id="total" value="{{$po_item->qty * $po_item->rate}}"></td>
<td><input type="text" class="form-control" name="description[]" id="description" value="{{$po_item->description}}"></td>
<td><input type="text" class="form-control" name="job_id[]" id="job_id" value="{{$po_item->cost_item->job_id}}" readonly></td>
<td>
<form action="{{action('Employee\POItemController#destroy', $po_item->id)}}" method="post">
{{csrf_field()}}
{{method_field('DELETE')}}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
<form action="{{ route('employee.poitem.update', $vendor_company_id )}}" method="post">
{{csrf_field()}}
{{method_field('PATCH')}}
#foreach ($po_items as $po_item)
<input type="hidden" name="cost_item_id[]" value="?">
<input type="hidden" name="qty[]" value="?">
<input type="hidden" name="rate[]" value="?">
<input type="hidden" name="description[]" value="?">
#endforeach
<button type="submit" class="btn btn-primary">Update</button>
</form>
Since there are two forms in the same page, i find it hard to pass new form values to the update function. I read on using the same session to solve this issue, but it wasn't clear enough for me to proceed with it
Instead of Making delete form you can do it with anchor tag. I don't know it is correct way or not but it works fine.
Delete
Hope this helps :)
Your input id values are incorrect. In HTML, an id should be unique across all HTML tags. When you make a foreach :
#foreach ($po_items as $po_item)
<tr>
<td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
</tr>
#endforeach
you're assigning all inputs vendor_name to same id="vendor_name, which is W3C incorrect. You could use instead
#foreach ($po_items as $po_item)
<tr>
<td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name_{{$po_item->id}}" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
</tr>
#endforeach
But that's not the real problem here, you need upon submitting the UPDATE Form to read each input data and pushes it in hidden inputs.
Do you use vanilla Javascript or JQuery? With JQuery you can achieve it easier, you must put some scripts on the page that contains the forms. The idea is to find each input value for like qty, push them into array and convert that array to JSON string and put that JSON string as value to hidden qty input. Try this codes :
<table class="table">
<thead>
<tr>
<th>Vendor</th>
<th>Item</th>
<th>Qty</th>
<th>Rate</th>
<th>Total</th>
<th>Description</th>
<th>Job No</th>
</tr>
</thead>
<tbody id="table_inputs">
#foreach ($po_items as $po_item)
<tr>
<td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name_{{$po_item->id}}" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
<td><input type="text" class="form-control" name="item_name[]" id="item_name_{{$po_item->id}}" value="{{$po_item->cost_item->item->name}}" readonly></td>
<td><input type="text" class="form-control" name="qty[]" id="qty_{{$po_item->id}}" value="{{$po_item->qty}}"></td>
<td><input type="text" class="form-control" name="rate[]" id="rate_{{$po_item->id}}" value="{{$po_item->rate}}"></td>
<td><input type="text" class="form-control" name="total[]" id="total_{{$po_item->id}}" value="{{$po_item->qty * $po_item->rate}}"></td>
<td><input type="text" class="form-control" name="description[]" id="description_{{$po_item->id}}" value="{{$po_item->description}}"></td>
<td><input type="text" class="form-control" name="job_id[]" id="job_id_{{$po_item->id}}" value="{{$po_item->cost_item->job_id}}" readonly></td>
<td>
<form action="{{action('Employee\POItemController#destroy', $po_item->id)}}" method="post">
{{csrf_field()}}
{{method_field('DELETE')}}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
<form id="form_update_poitem" action="{{ route('employee.poitem.update', $vendor_company_id )}}" method="post">
{{csrf_field()}}
{{method_field('PATCH')}}
#foreach ($po_items as $po_item)
<input type="hidden" name="cost_item_id[]" >
<input type="hidden" name="qty[]" >
<input type="hidden" name="rate[]" >
<input type="hidden" name="description[]" >
#endforeach
<button type="submit" class="btn btn-primary">Update</button>
</form>
<script>
$(document).ready(function() {
console.log('document is ready, proceed to form update codes');
// find button form
var button = $('#form_update_poitem button');
// add onclick event
button.on('click', function(e){
// find input array input values, turn them into array and pass that array to Hidden Input array
var itemArr = [];
$("#table_inputs input[type='item_name']").each(function(){
itemArr.push($(this).val());
});
console.log(itemArr);
$("#form_update_poitem input[type='cost_item_name']").val(JSON.stringify(itemArr));
var qtyArr = [];
$("#table_inputs input[type='qty']").each(function(){
qtyArr.push($(this).val());
});
console.log(qtyArr);
$("#form_update_poitem input[type='qty']").val(JSON.stringify(qtyArr));
var rateArr = [];
$("#table_inputs input[type='rate']").each(function(){
rateArr.push($(this).val());
});
console.log(rateArr);
$("#form_update_poitem input[type='rate']").val(JSON.stringify(rateArr));
var descriptionArr = [];
$("#table_inputs input[type='description']").each(function(){
descriptionArr.push($(this).val());
});
console.log(descriptionArr);
$("#form_update_poitem input[type='description']").val(JSON.stringify(descriptionArr));
});
});
</script>

undefined index from form post (untypical)

Here is the page with a table, which contains info rows that generates for every row from request.
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Account</th>
<th>Level</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<?php
$mysqli=connect();
$result=$mysqli->query("request is here");
while($rows=$result->fetch_assoc()) {
echo "<form action='script.php' method='post'>";
if ($rows['admin']>$_SESSION['admin']||$_SESSION['admin']<6||$rows['online']==1)
$levelinput='<td><input type="text" class="input-sm
form-control" value="'.$rows['admin'].'" disabled></td>
<td class="text-center"><input type="submit" class="btn btn-
simple btn-sm" value="Change" disabled></td>';
else $levelinput='<td><input type="number" class="input-sm
form-control" name="level" value="'.$rows['admin'].'"></td> ----- LEVEL IS HERE
<td class="text-center"><input type="submit" class="btn btn-round
btn-simple btn-sm btn-warning" value="Change"></td>';
if ($rows['ucp_online']>time()-60*5) $name=$rows['login']." <span
class='label label-success'>ONLINE</span>";
else $name=$rows['login'];
echo '
<tr>
<td>'.$rows['id'].'</td>
<td><a href="/admin/user.php?id='.$rows['id'].'" class="btn
btn-simple btn-sm">'.$name.'</a></td>
'.$levelinput.'
</tr>
<input type="hidden" name="admin" value="'.$rows['id'].'">
</form>';
}
$mysqli->close();
?>
</tbody>
</table>
Form moves us to script.php, which takes only $_POST['admin'] index and don't takes $_POST['level'] index. And of course, at the page level value displays correctly.

updating/deleting form data with php to update database using phpmyadmin

I am in the midst of completing a uni assignment and ive come across the issue of being totally stumped on how to update and delete data in my database through the use of a form.
I have successfully connected the database so that when the user selects the the addressID in the previous page they wish to work on, it will take them to the updateform.php page.
My code for the form is as follows:
<form method="post">
<tr>
<td>Firstline</td>
<td><input type="text" name="firstline" class="form-control"/></td>
</tr>
<tr>
<td>Secondline</td>
<td><input type="text" name="secondline" class="form-control"/></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="city" class="form-control"/></td>
</tr>
<tr>
<td>State</td>
<td><input type="text" name="state" class="form-control"/></td>
</tr>
<tr>
<td>Zip</td>
<td><input type="text" name="zip" class="form-control"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/></td>
</tr>
<tr>
<td></td>
<td><input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/></td>
</tr>
I am now completely stumped on how to connect the data they enter in the form to update the database.
I am trying to update the addresses table with the selected addressID chosen earlier if that helps.
Any push in the correct direction will be greatly appeciated.
Kind Regards
Change these line:
<form method="post">
to
<form method="post" action="process.php">
and
<input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/>
to
<input type="submit" name="update" value="Update" class="btn btn-success btn-lg"/>
and
<input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/>
to
<input type="delete" name="delete" value="Delete" class="btn btn-success btn-lg"/>
process.php:
if(isset($_REQUEST['update']))
{
// update block
// get all required value and fire update query
}
if(isset($_REQUEST['delete']))
{
// delete block
// get all required value and fire delete query
}
You could try something like this:
updateform.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
strip tags from user input:
$firstline= trim(strip_tags($_POST['first_line']));
$secondline= trim(strip_tags($_POST['second_line']));
create query:
$myquery = "INSERT INTO tablename (firstline, secondline)
VALUES ($firstline', '$secondline')";
execute query (confirm success/failure if/else):
if (#mysqli_query($database, $myquery)) {
print '<p>Entries accepted</p>';
}
else {
print '<p style="color: red;">Could not INSERT values because:<br />'
. mysqli_error($database) . '</p>;
}
}
?>
Then have your form post values for the query variables:
<form action="updateform.php" method="post">
<tr>
<td>Firstline</td>
<td><input type="text" name="firstline" size="20" value= <?php if (isset($_POST['firstline'])) {
print htmlspecialchars($_POST['first_line']);
} ?> /></td>
<tr>
<td>Secondline</td>
<td><input type="text" name="secondline" size="20" value= <?php if (isset($_POST['secondline'])) {
print htmlspecialchars($_POST['second_line']);
} ?> /></td>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/></td>
</tr>
<tr>
<td></td>
<td><input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/></td>

Form with enctype='multipart/form-data' is not working when there is no file uploaded

I have a form like this
<form role="form" class="form-inline" action='pageadd.php' method='post' enctype='multipart/form-data'>
<table class='formTable'>
<tr>
<td>Name</td>
<td>:</td>
<td><input type="text" maxlength='140' class="form-control" size='50' name='title' /></td>
</tr>
<tr>
<td>Photo</td>
<td>:</td>
<td>
<input type="file" name='myphoto'/><br/>
</td>
</tr>
<tr>
<td>Content</td>
<td>:</td>
<td>
<textarea name='content' style='width:100%;height:240px;'></textarea>
</td>
</tr>
<tr>
<td colspan='3'><input type="submit" class="btn btn-success" name='save' value='Save' /></td>
</tr>
</table>
</form>
i prefer photo upload is optional
but the problem is when there is no photo uploaded, the form is not working, anyone know or ever found same problem?

Categories