Make AJAX on add and remove items to avoid reload - php

I have 3 php files in doing addition and deletion of medicine details. medicines detail are used to order drugs for each patient who comes for treatment. In order not to be a hassle, how do I prevent my application from needing to be reloaded with AJAX? The 3 files are: services.php , insertDetailMedicines.php and deleteDetail.php .
services.php
$data = mysqli_query($conn, "SELECT MAX(id_service) AS ids FROM tbl_services");
$final_data = mysqli_fetch_array($data);
$id1 = $final_data['ids'];
$id2 = substr($id1,3,3); //MR for Medical Record
$id3 = $id2 + 1;
$id4 = 'MR'.sprintf('%03s' , $id3); // <-- Auto generating unique id
<form method="POST" action="services.php">
<input type="hidden" name="id_medical_record" value="<?php echo $id4 ?>">
<select name="medicineName" id="medicineName" required>
<option value="">- Choose -</option>
<?php
$medicines = mysqli_query($conn, "SELECT * FROM tbl_medicines ORDER BY id_medicines ASC");
$m = mysqli_fetch_array($medicines);
while($m = mysqli_fetch_array($medicines)){ ?>
<option value="<?php echo $m['id_medicine'] ?>">
<?php echo $m['medicine_name'] ?>
</option>
<?php } ?>
</select>
<input type="text" name="qty_medicines" id="qty_medicines" value="1" required />
<button type="submit" name="add" style="cursor: pointer">ADD</button>
</form>
<table> <!--this is to display the drugs that have been added-->
<?php
$show_details = mysqli_query($conn, "SELECT * FROM tbl_detail_medicines LEFT JOIN tbl_medicines USING (id_medicine)");
$num = 1;
if(mysqli_num_rows($show_details) > 0)
{
while ($detail = mysqli_fetch_array($show_details))
{
?>
<tr>
<td>
<?php echo $num++.'.'; ?>
</td>
<td>
<?php echo $detail['medicine_name'] ?>
</td>
<td>
<?php echo $detail['qty'] ?>
</td>
<td>
<a href="deleteDetail.php?delete=<?php echo $detail['id'] ?>">
<b> X </b>
</a>
</td>
</tr>
<?php
}}
?>
</table>
insertDetailMedicines.php
<?php
if (isset($_POST['add'])) {
$idMR = $_POST['id_medical_record'];
$medicineName = $_POST['medicineName'];
$qty_medicines = $_POST['qty_medicines'];
$insert_detail = "insert into tbl_detail_medicines (id,id_service,id_medicine,qty)
VALUES
(null,'$idMR','$medicineName','$qty_medicines')";
if (mysqli_query($conn,$insert_detail)) {
//echo "inserting success!";
}
}
?>
deleteDetail.php
<?php
require 'koneksi.php';
if(isset($_GET['delete'])){$delete = mysqli_query($conn, "DELETE FROM tbl_detail_medicines WHERE id = '".$_GET['delete']."' ");
header('location:services.php');
}
?>
apppearance

Related

PHP MYSQLi Database not updating

I've been trying to find the problem for 3 nights. It gave me nightmares. Please help. I think my code is already perfect. Can someone please fix my code and tell me whats wrong?
$sql = "SELECT py.idPembayaran, p.idPelajar, p.nama, b.namaBarangan,
pb.kuantiti, b.harga, py.jumlahBayaran, py.statusPembayaran,
py.statusPenghantaran, pb.tarikhPembelian FROM barangan b
INNER JOIN pembelian pb on pb.idBarangan = b.idBarangan
INNER JOIN pembayaran py on py.idPembelian = pb.idPembelian
INNER JOIN pelajar p on p.idPelajar = pb.idPelajar";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '
<form method="POST" action="purchaselist.php">
<tr>
<input type="hidden" name="idPembayaran" value="<?php echo $idPembayaran ?>">
<td>'.$row["idPembayaran"].'</td>
<td>'.$row["idPelajar"].'</td>
<td>'.$row["nama"].'</td>
<td>'.$row["namaBarangan"].'</td>
<td>'.$row["kuantiti"].'</td>
<td>'.$row["harga"].'</td>
<td>'.$row["jumlahBayaran"].'</td>
<td>
<select name="statusPembayaran">
<option value="In process">In process</option>
<option value="Successful">Successful</option>
</select>
</td>
<td>
<select name="statusPenghantaran">
<option value="In process">In process</option>
<option value="Arrived">Arrived</option>
</select>
</td>
<td>'.$row["tarikhPembelian"].'</td>
<td><input type="submit" name="submit" value="Update"></td>
</tr>
</form>
';
}
}
if (!empty($_POST["submit"])) {
$idPembayaran = $_POST["idPembayaran"];
$statusPembayaran = $_POST["statusPembayaran"];
$statusPenghantaran = $_POST["statusPenghantaran"];
$sql = "UPDATE pembayaran SET statusPembayaran ='".$statusPembayaran."', statusPenghantaran ='".$statusPenghantaran."' WHERE idPembayaran = '".$idPembayaran."'";
if(mysqli_query($conn, $sql)) {
echo "
<script>
alert('test');
window.location.href = 'purchaselist.php';
</script>
";
}
else {
echo "Update error.";
}
}
Im trying to update table "pembayaran" but it is not updating.
There is only two column that i want to update which is "statusPembayaran" and "statusPenghantaran" in that table. The value is from select option.
When you set the value for the field idPembayaran in the first place, you have...
<input type="hidden" name="idPembayaran" value="<?php echo $idPembayaran ?>">
at this point $idPembayaran isn't set, it should be $row["idPembayaran"] which is the value from the SELECT...
<input type="hidden" name="idPembayaran" value="<?php echo $row["idPembayaran"]; ?>">

For loop inserts only 1 MySQL record

I have been working on a form that allows entering a menu item for a cafe establishment, alongside its ingredients (arrays of data). Unfortunately, a problem came up as it only executes 1 query even though 2 or more ingredients were entered in the form (dynamic, jQuery).
Here is the PHP code:
<?php
include("session.php");
if (isset($_POST['submit'])) {
$productname = $_POST['product_name'];
$categoryID = $_POST['categoryID'];
$price = $_POST['srp'];
// ingredients
$ingredients = $_POST['ingredients'];
$qty = $_POST['qty'];
$measure = $_POST['measure'];
if (!empty($productname) && !empty($categoryID) && !empty($price) && !empty($ingredients) && !empty($qty) && !empty($measure)) {
for ($i=0; $i < count($ingredients); $i++) {
if ($ingredients[$i] != "" && $qty[$i] != "" && $measure[$i] != "") {
mysqli_query($db, "insert into individual_ingredients values ('', '$productname', '{$ingredients[$i]}', '{$qty[$i]}', '{$measure[$i]}')");
}
}
mysqli_query($db, "insert into end_products values ('', '$productname', '$price', '', '$categoryID')");
mysqli_query($db, "insert into audit_trail values ('', now(), '{$_SESSION['login_user']}', 'New end product added')");
header("location: end_products.php");
} else {
echo '<font color="red">'."Incomplete data entered".'</font>';
}
}
?>
And here is the HTML form and JQuery:
<html>
<head>
<title><?php echo $login_session; ?> | New End Product Record</title>
<link rel="stylesheet" href="css/main.css" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Bootstrap js library -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//add more fields group
$(".addMore").click(function(){
var fieldHTML = '<div class="form-group fieldGroup">'+$(".fieldGroupCopy").html()+'</div>';
$('body').find('.fieldGroup:last').after(fieldHTML);
});
//remove fields group
$("body").on("click",".remove",function(){
$(this).parents(".fieldGroup").remove();
});
});
</script>
</head>
<body>
HTML form:
<table style="margin: 5% -1% 0 -10%; font-size: 0.9em">
<tr>
<form action="new_end_product_record.php" method="post">
<td>Name</td>
<td><input type="text" name="product_name"></td>
<td>Raw Material/s Used</td>
<td>
<div class="fieldGroup">
<select name="ingredients[]">
<option value="">Ingredient</option>
<?php
$items_sql = "select name from raw_materials where status='Active'";
$get_items = mysqli_query($db, $items_sql);
while ($option = mysqli_fetch_assoc($get_items)) { ?>
<option value="<?php echo $option['name']; ?>"><?php echo $option['name']; ?></option>
<?php } ?>
</select>
<input type="text" name="qty[]" placeholder="Quantity" style="width:60px">
<select name="measure[]">
<option value="">Measure Unit</option>
<?php
$get_units = "select * from raw_material_measures";
$units = mysqli_query($db, $get_units);
while ($unit = mysqli_fetch_assoc($units)) {
?>
<option value="<?php echo $unit['full_name']; ?>"><?php echo $unit['full_name']; ?></option>
<?php } ?>
</select>
ADD
<br /><br />
</div>
<!-- second set -->
<div class="fieldGroupCopy" style="display: none;">
<div class="input-group">
<select name="ingredients[]">
<option value="">Ingredient</option>
<?php
$items_sql = "select name from raw_materials where status='Active'";
$get_items = mysqli_query($db, $items_sql);
while ($option = mysqli_fetch_assoc($get_items)) { ?>
<option value="<?php echo $option['name']; ?>"><?php echo $option['name']; ?></option>
<?php } ?>
</select>
<input type="text" name="qty[]" placeholder="Quantity" style="width:60px">
<select name="measure[]">
<option value="">Measure Unit</option>
<?php
$get_units = "select * from raw_material_measures";
$units = mysqli_query($db, $get_units);
while ($unit = mysqli_fetch_assoc($units)) {
?>
<option value="<?php echo $unit['full_name']; ?>"><?php echo $unit['full_name']; ?></option>
<?php } ?>
</select>
REMOVE
<br /><br />
</div>
</div>
</td>
</tr>
<tr>
<td>SRP</td>
<td><input type="text" name="srp"></td>
</tr>
<tr>
<td>Category</td>
<td>
<select name="categoryID">
<option value="">Select category...</option>
<!--list all categories in the database-->
<?php
$cat_query = "select category_ID, name from end_products_categories";
$get_cats = mysqli_query($db, $cat_query);
while ($option = mysqli_fetch_assoc($get_cats)) { ?>
<option value="<?php echo $option['category_ID']; ?>"><?php echo $option['name']?></option>
<?php } ?>
</select>
</td>
<!-- <td>Expiration</td>
<td><input type="date"></input></td> -->
</tr>
</table><br>
<input type="submit" class="button" name="submit" value="ADD RECORD">
<input type="reset" value="ERASE ALL">
</div></form>
</div>
</body>
</html>
Is there a problem with the loop or with the HTML form that prevents the second to the last set of values from being inserted? Any help would be appreciated.

Not able to update 2 items using php and mysqli

I have a table where I want to update data in 2 rows.
Heres my code:
<?php
include("includes/conn.php");
session_start();
if(!isset($_SESSION['username']))
{
header('Location: login.php');
}
else{
?>
<?php
$page_title = 'Update Order';
include("includes/header.inc.php");
?>
<?php if( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update-order']) ){
if( !empty($_POST['productname']) && !empty($_POST['quantity']) && !empty($_POST['price']) && !empty($_POST['amount']) ){
$total = 0;
$pid = mysqli_real_escape_string($db,$_POST['pid']);
$advance = mysqli_real_escape_string($db,$_POST['advance_paid']);
$dod = mysqli_real_escape_string($db,$_POST['dod']);
$dod = date('Y-m-d', strtotime($dod));
$mop = mysqli_real_escape_string($db,$_POST['mop']);
for($i = 0; $i<count($_POST['productname']); $i++)
{
$total = $total + $_POST['amount'][$i];
$query = "UPDATE `invoice` SET product_name = (select name from item where id = '{$_POST['productname'][$i]}'), quantity = '{$_POST['quantity'][$i]}', price = '{$_POST['price'][$i]}', discount = '{$_POST['discount'][$i]}', amount = '{$_POST['amount'][$i]}', dod = '{$dod}', added_by = '{$_SESSION['id']}' WHERE id = '{$pid}' ";
mysqli_query($db, $query);
$update_stock = "UPDATE `item` SET added_by = '{$_SESSION['id']}', total_stock = total_stock + '{$_SESSION['quant']}' - '{$_POST['quantity'][$i]}' WHERE id = '{$_POST['productname'][$i]}'";
mysqli_query($db, $update_stock);
}
$query_orders = "UPDATE `orders` SET amount = '{$total}', dod = '{$dod}', mop = '{$mop}', advance = '{$advance}', added_by = '{$_SESSION['id']}' WHERE order_id = '{$_POST['orderid']}' ";
mysqli_query($db, $query_orders);
}
}
?>
<div id="alteration-form">
<div style="width: 90%; margin: 0 auto;">
<form method="GET" action="">
<label for="update_orderno" >Order NO.</label>
<input type="text" name="orderno" class="input-field" id="update_orderno" />
<input type="submit" name="find-order" value="Search Order" class="btn-all-submit" id="btn-order" />
</form>
</div>
</div>
<?php if( $_SERVER["REQUEST_METHOD"] == "GET" ){ ?>
<?php
if( !empty($_GET['orderno']) ){
$orderno = $_GET['orderno'];
$sql_cust = "SELECT cust_id FROM `orders` where order_id = '$orderno' ";
$result_cust = mysqli_query($db, $sql_cust);
if( mysqli_num_rows($result_cust) > 0 ){
while( $row_cust = mysqli_fetch_array($result_cust, MYSQLI_ASSOC) ){
$cust_id = $row_cust['cust_id'];
}
$sql_name = "SELECT * FROM `users` WHERE id = '$cust_id' ";
$result_name = mysqli_query($db, $sql_name);
if( mysqli_num_rows($result_name) > 0 ){
while( $row_name = mysqli_fetch_array($result_name, MYSQLI_ASSOC) ){
$name = $row_name['name'];
$phone = $row_name['phone'];
$address = $row_name['address'];
$city = $row_name['city'];
$state = $row_name['state'];
}
}
}
?>
<div class="result-table-div">
<div class="divRow">
<div class="divCell" ><strong>Name:</strong></div>
<div class="divCell" ><?php echo $name; ?></div>
<?php if(!empty($phone)){ ?>
<div class="divCell" ><strong>Phone:</strong></div>
<div class="divCell" ><?php echo $phone; ?></div>
<?php } ?>
</div>
<div class="divRow">
<?php if(!empty($address)){ ?>
<div class="divCell" ><strong>Address:</strong></div>
<div class="divCell" ><?php echo $address; ?></div>
<?php } ?>
<?php if(!empty($city)){ ?>
<div class="divCell" ><strong>City:</strong></div>
<div class="divCell" ><?php echo $city; ?></div>
<?php } ?>
</div>
<div class="divRow">
<?php if(!empty($state)){ ?>
<div class="divCell" ><strong>State:</strong></div>
<div class="divCell" ><?php echo $state; ?></div>
<?php } ?>
</div>
</div>
<div class="result-table-div">
<form method="POST" action="">
<div class="divRow">
<div class="divCell" ><strong>Date of Delivery:</strong></div>
<div class="divCell" ><input type="text" name="dod" id="dod" value="<?php echo date("m/d/Y"); ?>"/></div>
<div class="divCell" ><strong>Mode of Payment:</strong></div>
<div class="divCell" >
<select name="mop">
<option value="cash">Cash</option>
<option value="card/cheque/online banking">Card/Cheque/Online Banking</option>
</select>
</div>
</div>
<table id="invoice-table">
<thead>
<tr>
<th>S No.</th>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
<th>Discount</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM invoice where order_id = '$orderno' ";
$result = mysqli_query($db, $sql);
if( mysqli_num_rows($result) > 0 ){
$count = 1;
while( $row = mysqli_fetch_array($result,MYSQLI_ASSOC) ){
$id = $row['id'];
$product_name = $row['product_name'];
$quantity = $row['quantity'];
$price = $row['price'];
$discount = $row['discount'];
$amount = $row['amount'];
$_SESSION['cust_id'] = $row['cust_id'];
$_SESSION['quant'] = $quantity;
?>
<tr>
<input type="hidden" name="pid" value="<?php echo $id; ?>" />
<td><?php echo $count++; ?></td>
<!--<td><input type="text" name="productname[]" value="?php echo $product_name ?>"></td>-->
<td>
<select name="productname[]">
<option value="1" <?php if( isset($product_name) && strtolower($product_name) == "bags" ) echo "selected"; ?> >Bags</option>
<option value="2" <?php if( isset($product_name) && strtolower($product_name) == "shoes" ) echo "selected"; ?> >Shoes</option>
<option value="3" <?php if( isset($product_name) && strtolower($product_name) == "suit" ) echo "selected"; ?> >Suit</option>
<option value="4" <?php if( isset($product_name) && strtolower($product_name) == "belts" ) echo "selected"; ?> >Belts</option>
<option value="5" <?php if( isset($product_name) && strtolower($product_name) == "t-shirts" ) echo "selected"; ?> >T-shirts</option>
<option value="6" <?php if( isset($product_name) && strtolower($product_name) == "others" ) echo "selected"; ?> >Others</option>
</select>
</td>
<td><input type="text" class="quantity" name="quantity[]" value="<?php echo $quantity ?>"></td>
<td><input type="text" class="price" name="price[]" value="<?php echo $price ?>"></td>
<td><input type="text" class="discount" name="discount[]" value="<?php echo $discount ?>"></td>
<td><input type="text" class="amount" name="amount[]" value="<?php echo $amount ?>"></td>
</tr>
<?php
}
?>
<input type="hidden" name="orderid" value="<?php echo $orderno; ?>">
<?php
}
?>
</tbody>
</table>
<p style="text-align: center;">Advanced Paid: <input type="text" name="advance_paid" /></p>
<div class="btn-div">
<input type="submit" class="btn-all-submit" name="update-order" value="Update Order">
</div>
</form>
</div>
<?php } } ?>
<script>
$('body').delegate('.quantity,.price,.discount','keyup',function()
{
var tr=$(this).parent().parent();
var qty=tr.find('.quantity').val();
var price=tr.find('.price').val();
var dis=tr.find('.discount').val();
var amt =(qty * price)-(qty * price *dis)/100;
tr.find('.amount').val(amt);
});
</script>
<?php include("includes/footer.inc.php"); ?>
<?php } ?>
I am using for loop to update the data but if I have 2 items to update, it is only updating second item and not the first one. Can someone please help me with this.
Problem occurs while updating invoice table.
I found the solution for my problem.
Query should be
$query = "UPDATE `invoice` SET product_name = (select name from item where id = '{$_POST['productname'][$i]}'), quantity = '{$_POST['quantity'][$i]}', price = '{$_POST['price'][$i]}', discount = '{$_POST['discount'][$i]}', amount = '{$_POST['amount'][$i]}', dod = '{$dod}', added_by = '{$_SESSION['id']}' WHERE id = '{$_POST['pid'][$i]}' ";
mysqli_query($db, $query);

Posting inside the page in another form action?

I am currenty having a thesis project which is the voting system, and my problem is how can I count the vote if I click on the radio button after I click the next button how to updates the votes?. And guys is it possible that in my php page I can save my data but the form action is not the saving part I mean like this
I have a form codes:
//preview page is the page where I can see all tha candidates that I have choose.
<form id="msform" action="preview.php" method="POST">
<?php
$resultasa = $db->prepare("SELECT * FROM candposition");
$resultasa->execute();
for($i=0; $rowasa = $resultasa->fetch(); $i++){
$exrxrxrx=$rowasa['pos_name'];
if($exrxrxrx=='President'){
?>
<fieldset>
<table>
<tr><td><h2 class="fs-title"><?php echo $rowasa['pos_name'] ?></h2></td>
<td><input name="<?php echo $rowasa['pos_name'] ?>" type="hidden" value="0" /></td>
</tr>
<?php
$YearNow=Date('Y');
$dsds=$rowasa['posid'];
$results = $db->prepare("SELECT * FROM candidates,student,school_year,partylist where student.idno = candidates.idno AND school_year.syearid = candidates.syearid AND posid =:a AND candidates.partyid = partylist.partyid AND school_year.from_year like $YearNow ");
$results->bindParam(':a', $dsds);
$results->execute();
for($i=0; $rows = $results->fetch(); $i++){
?>
<td><img src="admin/candidates/images/<?php echo $rows['image']; ?>" height="160px;" width="155px;"></td>
<td><br>
<div style="text-align:center">
<input name="pres" style="margin-left:-19px;" type="radio" value="<?php echo $rows['candid'] . "-" ."&nbsp". $rows['lastname']. "&nbsp". $rows['firstname'] ?>"> <?php echo $rows['lastname'] ?>,
<?php echo $rows['firstname'] ?>
- <?php
echo $rows['party_name']?>
<?php
//I think this part will be having an update like
$sql = "UPDATE candidates
SET votes=votes+?
WHERE candid=? ";
$q = $db->prepare($sql);
}
?>
<?php
}
?>

I can't seem to view my transaction history?

Please help :(
<?php
include 'dbFunctions.php';
session_start();
$user_id = $_SESSION['user_id'];
?>
<table>
<td>
<b><label for="billinghistory">Billing History:</b></label>
<select name="billing_history"
The following codes can't seem to print my transaction history, I am not sure if I have chosen the correct fields.
<?php
$query3 = "SELECT * from billinghistory";
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link));
while ($row3 = mysqli_fetch_array($result3)) {
$billing_name = $row3['billing_name'];
$billing_name_id = $row3['billing_name_id'];
?>
<option value="<?php echo $billing_name; ?> "><?php echo $billing_name ;?></option>
</select>
</td>
</table>
Try
while ($row3 = mysqli_fetch_array($result3,MYSQLI_ASSOC)) {

Categories