Update SQL command is not updating SQL record using JSP - sql

I am trying to update SQL table record but nothing changes done in SQL table. Whenever I do changes in input JSP page and do click on update button then record is not getting updated. I have id as a primary key in my table. Using id I am updating record.
Here is update.jsp
<body>
<%
Connection con;
PreparedStatement stmt = null;
ResultSet rs = null;
Statement stat = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dmsqms","root","");
%>
<form action="" method="post" enctype="multipart/form-data">
<%
stat = con.createStatement();
String id = request.getParameter("id");
String data = "select * from dmsfiles where id = '"+id+"'";
rs = stat.executeQuery(data);
while(rs.next()){
%>
<input type="hidden" name="id" value="<%=rs.getString("id") %>"/>
<center>
<table border="1" width="30%" cellpadding="5">
<thead>
<tr>
<th colspan="2">Update Record</th>
</tr>
</thead>
<tbody>
<tr>
<td>ID</td>
<td><input type="text" required="" name="id1" value="<%=rs.getString("id")%>" readonly=""/></td>
</tr>
<tr>
<td>Z id</td>
<td>
<input type="text" required="" name="zid" value="<%=rs.getString("zid")%>"/>
</td>
</tr>
<tr>
<td>First Name</td>
<td>
<input type="text" required="" name="firstname" value="<%=rs.getString("firstname")%>"/>
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<input type="text" required="" name="lastname" value="<%=rs.getString("lastname")%>"/>
</td>
</tr>
...
<tr>
<td><label class="hidden">Issuer</label></td>
<td>
<input type="text" name="issuer" class="hidden" value="<%=rs.getString("issuer")%>"/>
</td>
</tr>
<tr>
<td><label class="hidden">Status</label></td>
<td>
<select name="status" class="hidden" id="wgtmsr">
<option value="<%=rs.getString("status")%>"><%=rs.getString("status")%></option>
<option value="Public">Reserved</option>
<option value="Team">Implemented</option>
</select>
</td>
</tr>
<%
}
%>
<tr>
<td><button type="submit" class="btn btn-warning"/>Update</td>
<td>Back</td>
</tr>
</body>
</html>
<%
String id1 = request.getParameter("id");
String a = request.getParameter("zid");
String b = request.getParameter("firstname");
String c = request.getParameter("lastname");
String d = request.getParameter("mailid");
...
String u = request.getParameter("issuer");
String v = request.getParameter("status");
if(a!=null && b!=null && c!=null && d!=null && e!=null && f!=null && g!=null && h!=null && i!=null && j!=null && k!=null && l!=null && m!=null && n!=null && o!=null && p!=null && q!=null && r!=null && s!=null && t!=null && u!=null && v!=null){
String query = "update dmsfiles set id=?, zid=?, firstname=?, lastname=?, mailid=?, division=?, department=?, costcentercode=?, document_number=?, document_name=?, document_type=?, document_category=?, document_classification=?, authorised_by=?, fromdate=?, todate=?, document_level=?, document_general=?, serial_number=?, revision_number=?, issuer=?, status=? where id='"+id1+"'";
stmt = con.prepareStatement(query);
stmt.setString(1, id1);
stmt.setString(2, a);
stmt.setString(3, b);
stmt.setString(4, c);
stmt.setString(5, d);
...
stmt.setString(19, r);
stmt.setString(20, s);
stmt.setString(21, t);
stmt.setString(22, u);
stmt.setString(23, v);
stmt.executeUpdate();
response.sendRedirect("home.jsp");
}
%>

Related

How to pass value to model (Vuejs 3)

in my Vuejs3 project, there is a form in which the new row will be generated by pressing a button, so I put an array to handle new rows then I need to validate inputs, but after validation of the input of the array, the value didn't pass to model, but it works without the validation.. please help me to understand the mistake that I did.
The Form:
<table class="table table-stripped table-border">
<thead>
<tr>
<th>#</th>
<th style="width: 18%">
Medikament <span class="text-danger">*</span>
</th>
<th style="width: 9%">Milligram</th>
<th style="width: 9%">
Packung <span class="text-danger">*</span>
</th>
<th style="width: 7%">
Stück <span class="text-danger">*</span>
</th>
<th style="width: 19%">Apothekenname</th>
<th style="width: 24%">Adresse der Apotheke</th>
<th style="width: 14%">Postleitzahl</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in patientRequestForCreationDtos" :key="index">
<td>{{ index + 1 }}</td>
<td>
<input type="text" v-model="item.drugName" class="form-control" />
</td>
<td>
<input type="number" v-model="item.milligram" class="form-control" />
</td>
<td>
<input type="number" v-model="item.box" class="form-control" />
</td>
<td>
<input type="number" v-model="item.total" class="form-control" />
</td>
<td>
<input type="text" class="form-control" v-model="item.drugStoreName" :readonly="patientDetails.isElga == false" />
</td>
<td>
<input type="text" class="form-control" v-model="item.drugStoreAddress" :readonly="patientDetails.isElga == false" />
</td>
<td>
<input type="text" class="form-control" v-model="item.drugStorePostalCode" :readonly="patientDetails.isElga == false" />
</td>
<td>
<button type="button" #click="deleteRequestItemRow(index)" class="btn btn-sm btn-danger">
-
</button>
</td>
</tr>
</tbody>
</table>
The Array:
patientRequestForCreationDtos: [{
milligram: null,
box: null,
total: null,
drugStoreName: "",
drugStoreAddress: "",
drugStorePostalCode: "",
drugName: "",
}, ],
The validation function:
checkValidation() {
if (!this.patientRequestForCreationDtos.drugName) {
Swal.fire("drug name is required...");
return;
}
return true;
},
```js
---
it always says => drug name is required..
this.patientRequestForCreationDtos is array. maybe you can do this.
checkValidation(patientRequestForCreationDto) {
if (!patientRequestForCreationDto.drugName) {
Swal.fire("drug name is required...");
return;
}
return true;
},
if you'll have only one element in patientRequestForCreationDtos then you gotta choose first element in the array and then check its property
checkValidation() {
if (!this.patientRequestForCreationDtos[0].drugName) {
Swal.fire("drug name is required...");
return;
}
return true
},
also if your patientRequestForCreationDtos is always gonna be an array then you might find this helpful

ASP.NET Core - How would I set a name with ID of HTML element on CSHTML

How would I set a name with ID of HTML element on CSHTML?
<tr>
<td>
#item.Items.ItemID
</td>
<td>
#item.Items.ItemModelDescription
</td>
<td class="text-right">
<input id="#item.Items.ItemID + 'UnitPrice'" class="form-control text-right" value="#item.Items.ItemUnitPrice" />
</td>
<td class="text-right">
<input id="#item.Items.ItemID + 'Quantity'" class="form-control text-right" value="#item.Quantity" oninput="return change_quantity('#item.Items.ItemID')"/>
</td>
<td class="text-right">
#(item.Quantity * item.Items.ItemUnitPrice)
</td>
<td>
<a class="btn btn-sm btn-danger btn-rounded" asp-controller="purchaseorderheader" asp-action="Remove" asp-route-id="#item.Items.ItemID"><span class="fa fa-trash"></span></a>
</td>
</tr>
I can't get the value of HTML element using javascript is there anyway or proper way of setting an id of each quantity input? Or any keywords to search regarding this one.
According to your code and description, I assume you want to calculate the cost based on the Quantity and the ItemUnitPrice. If that is the case, please refer to the following sample code, you can refer it to change your code:
ViewModel:
public class ItemViewModel
{
public int ItemId { get; set; }
public string ItemDescription { get; set; }
public decimal ItemUnitPrice { get; set; }
public decimalc Quantity { get; set; }
}
Index.cshtml: we could set the id attribute like this id='Quantity_#item.ItemId', after rendering, the output like Quantity_XXX:
#model IEnumerable<WebApplication6.Models.ItemViewModel>
<table class="table">
<thead>
<tr>
...
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#item.ItemId
</td>
<td>
#item.ItemDescription
</td>
<td class="text-right">
<input id="ItemUnitPrice_#item.ItemId" class="form-control text-right" type="text" value="#item.ItemUnitPrice" />
</td>
<td class="text-right">
<input id='Quantity_#item.ItemId' class="form-control text-right txtquantity" type="text" value="#item.Quantity" oninput="return change_quantity(this,#item.ItemId)" />
</td>
<td class="text-right">
#(item.Quantity * item.ItemUnitPrice)
</td>
<td>
<a class="btn btn-sm btn-danger btn-rounded" asp-controller="purchaseorderheader" asp-action="Remove" asp-route-id="#item.ItemId"><span class="fa fa-trash"></span></a>
</td>
</tr>
}
</tbody>
</table>
Then at the end of the Index.cshtml page, add the following JavaScript: we could use the parent() and closest() method to find the current row, and then find the relates elements in this row.
#section Scripts{
<script>
function change_quantity(e, itemid) {
//find the item unit price.
var price = $("#" + "ItemUnitPrice_" + itemid).val();
//find the quantity value.
var quantity = $("#" + "Quantity_" + itemid).val();
//calculate the cost and change the html content.
$(e).parent().closest("tr").find(".text-right:last").html((price * quantity).toFixed(2).toString());
}
</script>
}
The output like this:
you can use this approach
#foreach (var item in Model)
{
<tr id="tr-#item.ItemId">
<td>
#item.ItemId
</td>
<td>
#item.ItemDescription
</td>
<td class="text-right">
<input id="ItemUnitPrice_#item.ItemId" class="form-control text-right" type="text" value="#item.ItemUnitPrice" />
</td>
<td class="text-right">
<input id='Quantity_#item.ItemId' class="form-control text-right txtquantity" type="text" value="#item.Quantity" oninput="return change_quantity(this,#item.ItemId)" />
</td>
<td class="text-right">
#(item.Quantity * item.ItemUnitPrice)
</td>
<td>
<a class="btn btn-sm btn-danger btn-rounded" asp-controller="purchaseorderheader" asp-action="Remove" asp-route-id="#item.ItemId"><span class="fa fa-trash"></span></a>
</td>
</tr>
}

Error TokenMismatchException in VerifyCsrfToken.php line 68:

I want to do multiple edit, I want to edit from the data I checked following script .blade, I found the TokenMismatchException error in VerifyCsrfToken.php line 68: when updating.
<form name="form" action="{{url('/update-kb')}}" method="post" onsubmit="return deleteConfirm();"/>
<div class="table table-responsive">
<table id="example1" class="table table-bordered">
<thead>
<tr class="info">
<th width="3%"><input type="checkbox" name="select_all" id="select_all" value=""/></th>
<th>No</th>
<th>Data lengkap</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach( $datasiswa as $row )
<tr>
<td>
<label class="checkbox-inline"><input type="checkbox" name="checked_id[]" class="checkbox" value="{{$row->id}}"/>
</label>
</td>
<td width="5%">{{ $i }}</td>
<td width="95%">
<table class="table">
<tr>
<td class="info">Nama panggilan</td>
<td>{{$row->nm_panggilan}}</td>
<td class="warning">Pekerjaan ibu</td>
<td>{{$row->pekerjaan_ibu}}</td>
</tr>
<tr>
<td class="info">Jenis kelamin</td>
<td>{{$row->jenis_kelamin}}</td>
<td class="warning">No. Handphone</td>
<td>{{$row->hp_ibu}}</td>
</tr>
<tr>
<td class="info">Tempat, Tanggal lahir</td>
<td>{{$row->tempat}}, {{$row->tanggal_lahir}}</td>
<td class="warning">Alamat</td>
<td>{{$row->alamat}}</td>
</tr>
<tr>
<td class="info">Status anak</td>
<td>{{$row->status_anak}}</td>
<td class="warning">Golongan darah</td>
<td>{{$row->goldar}}</td>
</tr>
<tr>
<td class="info">Agama</td>
<td>{{$row->agama}}</td>
<td class="warning">Nama wali</td>
<td>{{$row->nm_wali}}</td>
</tr>
<tr>
<td class="info">Kewarganegaraan</td>
<td>{{$row->kewarganegaraan}}</td>
</tr>
<tr>
<td class="info">Anak ke-</td>
<td>{{$row->anak_ke}}</td>
</tr>
<tr>
<td class="info">Kelas</td>
<td>{{$row->kelas}}</td>
</tr>
</table>
</td>
<td>
<a href="{!! url('/'.$row->id.'/edit-siswa') !!}">
<button class="btn btn-default btn-block"><i class="fa fa-edit"></i></button><br>
</a>
<a href="{!! url('/'.$row->id.'/delete-siswa') !!}">
<button class="btn btn-danger btn-block"><i class="fa fa-trash"></i></button>
</a>
</td>
</td>
</tr>
<?php $i++; ?>
#endforeach
</tbody>
</table>
<div class="col-md-3">
<input type="submit" class="btn btn-danger" name="delete_submit" value="Hapus"/>
</div>
</div>
</form>
But I still have error, what causes it?
public function updatekb($id, Request $request)
{
$data = Datasiswa::find($id);
if (isset($request->delete_submit)) {
$idArr = $request->checked_id;
foreach ($idArr as $id) {
DB::update('update tb_siswa, tb_pernyataan set tb_pernyataan.kelas = "cekcek" where tb_pernyataan.kelas = "TK A" and tb_siswa.sekolah = "KB TK KHALIFAH 25" and id = "' . $id . '" ');
}
return back();
Session::flash('sukses', 'Data berhasil di update', compact('data'));
}
}
Add below to your form element.
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
Some answers are suggesting to disable csrf protection which is possible but NOT RECOMENDED. This leaves your application vulnureable.
Laravel use CSRF token to verify user request. so you have to use it on yevery request if you want to disable it then you can disable it from See here how to disable it or you can use it as
{{ csrf_field() }}
or
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
You can resolve this issue in two ways:-
First One:-
{{ csrf_field() }}
or
<input type="hidden" name="_token" value="{{ csrf_token() }}"/> // add this in form
Or the other (simpler) way, inside your app\Http\Middleware/VerifyCsrfToken.php add
protected $except = [
'update-kb', // your route name
];
Hope it helps!

system.argumentnullexception value cannot be null on Model.Any()

I am getting "system.argumentnullexception" on the Model.Any() line when I submit the form and the table in database is currently empty. the code of view are as follow.
#using System.Linq
#model IEnumerable<Al_sehrawi.Models.tbl_Packing>
#{
ViewBag.Title = "AddBox2";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>AddBox2</h2>
#{
if (Model.Any())
{
foreach (var i in Model)
{
<form action="~/Packing/AddBox2" method="post">
<table>
<tr>
<td>
Id:
#{int counter = i.packing_id + 1;}
<input name="id" type="number" value="#counter" />
</td>
</tr>
<tr>
<td>
Name :
</td>
<td>
<input name="packingName" type="text" />
</td>
</tr>
<tr>
<td>
Unit :
</td>
<td>
<input name="unit" type="text" />
</td>
</tr>
<tr>
<td>
Quantity of 1st Packing :
</td>
<td>
<input name="quantity1" type="number" />
</td>
</tr>
<tr>
<tr>
<td>
Quantity of 2nd Packing :
</td>
<td>
<input name="quantity2" type="number" />
</td>
</tr>
<tr>
<td>
<input name="Submit1" type="submit" value="submit" />
</td>
</tr>
</table>
</form>
}
}
else
{
<form action="~/Packing/AddBox2" method="post">
<table>
<tr>
<td>
Id:
<input name="id" type="number" value="1" />
</td>
</tr>
<tr>
<td>
Name :
</td>
<td>
<input name="packingName" type="text" />
</td>
</tr>
<tr>
<td>
Unit :
</td>
<td>
<input name="unit" type="text" />
</td>
</tr>
<tr>
<td>
Quantity of 1st Packing :
</td>
<td>
<input name="quantity1" type="number" />
</td>
</tr>
<tr>
<tr>
<td>
Quantity of 2nd Packing :
</td>
<td>
<input name="quantity2" type="number" />
</td>
</tr>
<tr>
<td>
<input id="Submit1" type="submit" value="submit" />
</td>
</tr>
</table>
</form>
}
}
The code in PackingController are as follow.
public ActionResult AddBox2()
{
PackingModel c = new PackingModel();
var id = c.getNextId();
return View(id);
}
[HttpPost]
public ActionResult AddBox2(int id, string packingName, string unit, int quantity1, int quantity2)
{
PackingModel p = new PackingModel();
p.addBox2(id, packingName, unit, quantity1, quantity2);
return View();
}
and the Model is
public bool addBox2(int id, string packingName, string unit,int quantity1, int quantity2)
{
try
{
using (POSEntities1 db = new POSEntities1())
{
tbl_Packing p = new tbl_Packing();
tbl_packing_box2a b1 = new tbl_packing_box2a();
tbl_packing_box2b b2 = new tbl_packing_box2b();
p.name = packingName;
p.unit = unit;
b1.quantiity = quantity1;
b1.tbl_packing_box_id = id;
int a_Id;
var result = db.tbl_packing_box2a.OrderByDescending(b => b.tbl_packing_box1a_id).Take(1).ToList();
foreach (var item in result)
{
a_Id = item.tbl_packing_box1a_id;
b2.quantity = quantity2;
b2.tbl_packing_box2a_id = a_Id;
db.tbl_Packing.Add(p);
db.tbl_packing_box2a.Add(b1);
db.tbl_packing_box2b.Add(b2);
db.SaveChanges();
}
}
}
catch (Exception)
{
return false;
}
return true;
}
First check if model is empty and then try Model.Any()
#if (Model != null)
{
if (Model.Any())
{
....
}
}

POST request not working in form

I'm trying to submit some form using POST method. Here is par of form:
<form action="interlopers.php" mehod="post" id = "interlopersForm" name="interlopersForm" onsubmit="return validateInterlopersForm()">
<table border="0">
<tr>
<td> <label for="ast_num" > Ast. num </label> </td>
<td> <input type="text" name="ast_num" id = "ast_num"
value="<?php if(isset($_REQUEST['ast_num'])) { echo htmlentities ($_REQUEST['ast_num']); } ?>"
size="6"> </td>
</tr>
<tr>
<td><label for="cut_off"> Cut-off </label></td>
<td><input type="text" name="cut_off" id="cut_off" size="6" ></td>
</tr>
<tr>
<td><label for="data"> Data </label></td>
<td><input type ="checkbox" name="data" id = "dataSDSS" value="SDSS" checked> SDSS <br>
<input type ="checkbox" name="data" id="dataWISE" value="WISE" checked > WISE <br>
<input type ="checkbox" name="data" id = "dataSp" value="Sp" checked> Taxonomy</td>
</tr>
<tr>
<td> <label for="isFinalStep"> Just interlopers? </label> </td>
<td> <input type = "checkbox" name="isFinalStep" id = "isFinalStep"> </td>
</tr>
<tr> <input type="hidden" name="submitted" value="1"> </tr>
<tr>
<td colspan="2" align="center"> <input type="submit" value="Calculate" > </td>
</tr>
</table>
</form>
But, instead of getting POST request im getting GET request. I'm using apache server and php5 on Ubuntu. I tried this on local-server and on remote host, but still GET instead of POST.
Change mehod="post" to method="post".
You wrote wrong.