Not able to update 2 items using php and mysqli - php

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);

Related

Make AJAX on add and remove items to avoid reload

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

Handling a simple order form php

I'm fetching all the products from a database and displaying them with a number input with a name like: name="product_name[<?php $row['id'];?>]" On submission i'm sending the array of product ids and their quantities to the handler page, my question is how do I select the relevant id from my database and then display them back with their quantities for order confirmation.
order_form.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_name("user");
session_start();
include 'includes/con.inc.php';
?>
<form class="" action="handlers/confirm.order.hd.php" method="POST">
<h5>Meal Deal</h5>
<div class="row">
<?php
$load_deals = $dbh->prepare("SELECT * FROM products WHERE category = :category");
$load_deals->bindValue(':category', 'lunchbox', PDO::PARAM_STR);
if($load_deals->execute()) {
$load_deals->setFetchMode(PDO::FETCH_ASSOC);
}
while($row = $load_deals->fetch()) {
$id = $row['id'];
$name = $row['name'];
$price = $row['price'];
$des = $row['description'];
$img = $row['image'];
?>
<div class="col-md-4 mb-3">
<div class="item-holder" id="<?php echo $id;?>">
<label class=""><b><?php echo $name;?></b> - <?php echo $price;?> AZN <small><?php echo $des; ?></small></label>
<input type="number" name="deals[<?php $row['id'];?>]" class="amount" placeholder="0">
</div>
</div>
<?php } ?>
</div>
<h5>Sandwiches</h5>
<div class="row">
<?php
$load_sandwiches = $dbh->prepare("SELECT * FROM products WHERE category = :category");
$load_sandwiches->bindValue(':category', 'Sandwich', PDO::PARAM_STR);
if($load_sandwiches->execute()) {
$load_sandwiches->setFetchMode(PDO::FETCH_ASSOC);
}
while($row = $load_sandwiches->fetch()) {
$id = $row['id'];
$name = $row['name'];
$price = $row['price'];
$des = $row['description'];
$img = $row['image'];
?>
<div class="col-md-4 mb-3">
<div class="item-holder" id="<?php echo $id;?>">
<label class=""><b><?php echo $name;?></b> - <?php echo $price;?> AZN <small><?php echo $des; ?></small></label>
<input type="number" name="sandwiches[<?php $row['id'];?>]" class="amount" value="0">
</div>
</div>
<?php } ?>
</div>
<h5>Drinks</h5>
<div class="row">
<?php
$load_drinks = $dbh->prepare("SELECT * FROM products WHERE category = :category");
$load_drinks->bindValue(':category', 'Drink', PDO::PARAM_STR);
if($load_drinks->execute()) {
$load_drinks->setFetchMode(PDO::FETCH_ASSOC);
}
while($row = $load_drinks->fetch()) {
$id = $row['id'];
$name = $row['name'];
$price = $row['price'];
$des = $row['description'];
$img = $row['image'];
?>
<div class="col-md-4 mb-3">
<div class="item-holder" id="<?php echo $id;?>">
<img src="imgs/<?php echo $img;?>" class="img-fluid">
<label class=""><b><?php echo $name;?></b> - <?php echo $price;?> AZN <small><?php echo $des; ?></small></label>
<input type="number" name="drinks[<?php $row['id'];?>]" class="amount" value="0">
</div>
</div>
<?php } ?>
</div>
<button class="btn btn-primary mx-auto w-50 mb-3 d-block" type="submit"> confirm order</button>
</form>
confirm.order.hd.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_name("goodfooduser");
session_start();
include '../includes/con.inc.php';
$deals = $_POST['deals'];
$sandwiches = $_POST['sandwiches'];
$drinks = $_POST['drinks'];
$id_list = array_merge($deals , $sandwiches , $drinks);
$split_list = implode(',', $id_list);
$get_product = $dbh->prepare("SELECT * FROM products WHERE id = :id ");
$get_product->bindValue(':id', $split_list);
if($get_product->execute()) {
$get_product->setFetchMode(PDO::FETCH_ASSOC);
}
while($row = $get_product->fetch()) {
$product_id = $row['id'];
$product_name = $row['name'];
$output = array(
'id' => $product_id,
'item' => $product_name,
'amount' => $value,
);
print_r($output);
}
?>
On order confirm page while binding your sql query you should use string since you are passing the value for a single placeholder which is :id
pass implode(',',$id_list) into your bindValue and it should work fine

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.

How to keep the attributes from dropdowns to next few pages using php mysql

i have this codes from a dropdown page.(not all)
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Sila pilih kategori peperiksaan
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<form name="selection" role="form" method="post" action="index.php?pages=peperiksaan2" enctype="multipart/form-data" onsubmit="return validate_form ( );">
<div class="form-group">
<?php
$sql = "SELECT tahun_nama FROM tahun";
$result = $con->query($sql);
?>
<label for="tahun_nama">TAHUN</label>
<select class="form-control" id="tahun_nama" name="tahun_id">
<option value=""<?php if(!isset($_POST['tahun_nama']) || (isset($_POST['tahun_nama']) && empty($_POST['tahun_nama']))) { ?>selected<?php } ?>>Sila pilih tahun</option>
<?php
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row['tahun_nama'].'"'. ((isset($_SESSION['tahun_nama']) && !empty($_SESSION['tahun_nama']) && ($_SESSION['tahun_nama'] == $row['tahun_nama'])) ? 'selected="selected"' : '') .'>'.$row['tahun_nama'].' </option>';
}
?>
</select>
</div>
<div class="form-group">
<?php
$sql = "SELECT peperiksaan_nama FROM peperiksaan";
$result = $con->query($sql);
?>
<label>PEPERIKSAAN</label>
<select class="form-control" id="peperiksaan_nama" name="peperiksaan_id">
<option value=""<?php if(!isset($_POST['peperiksaan_nama']) || (isset($_POST['peperiksaan_nama']) && empty($_POST['peperiksaan_nama']))) { ?>selected<?php } ?>>Sila pilih peperiksaan</option>
<?php
while($row = $result->fetch_assoc()) {
?>
<option value="<?php echo $row['peperiksaan_nama']; ?>" <?php if(isset($_POST['peperiksaan_nama']) && $_POST['peperiksaan_nama'] == $row['peperiksaan_nama']) { ?>selected<?php } ?>><?php echo $row['peperiksaan_nama']; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<?php
$sql = "SELECT darjah_nama FROM darjah";
$result = $con->query($sql);
?>
<label>DARJAH</label>
<select class="form-control" id="darjah_nama" name="darjah_id">
<option value=""<?php if(!isset($_POST['darjah_nama']) || (isset($_POST['darjah_nama']) && empty($_POST['darjah_nama']))) { ?>selected<?php } ?>>Sila pilih darjah</option>
<?php
while($row = $result->fetch_assoc()) {
?>
<option value="<?php echo $row['darjah_nama']; ?>" <?php if(isset($_SESSION['darjah_nama']) && $_SESSION['darjah_nama'] == $row['darjah_nama']) { ?>selected<?php } ?>><?php echo $row['darjah_nama']; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<?php
$result = mysqli_query($con,"SELECT * FROM kelas");
?>
<label>KELAS</label>
<select class="form-control" id="kelas_nama" name="kelas_id">
<option value=""<?php if(!isset($_POST['kelas_nama']) || (isset($_POST['kelas_nama']) && empty($_POST['kelas_nama']))) { ?>selected<?php } ?>>Sila pilih kelas</option>
<?php
while($row = mysqli_fetch_array($result)) {
echo " <option value=" . $row['kelas_nama'] . ">" . $row['kelas_nama'] ."</option>"; } ?>
</select>
</div>
<!-- <button style="display: block; margin: 0 auto;" type="submit" name="select" class="btn btn-primary" href="pptt415.php">PAPAR</button-->
<!--center><a class="btn btn-primary" href="index.php?pages=newpptt415" role="button">PAPAR</a></center-->
<center><button type="submit" class="btn btn-primary">PAPAR</button></center>
<!--a class="btn btn-primary" style="display: block; margin: 0 auto;" href="index.php?pages=edit_guru&guru_nama=?php echo $guru_nama ?>" role="button"><span class="glyphicon glyphicon-glyphicon glyphicon-pencil" aria-hidden="true"></span></a-->
</form>
</div>
</div>
</div>
</div>
</div>
and this the page after selecting the options from dropdown where user needs to import file based on the option selected.. (a few codes below)
<center><h4 class="page-header">PEPERIKSAAN : <?php
$peperiksaan_nama=$_POST["peperiksaan_id"];
echo $peperiksaan_nama ?> <br>
MATAPELAJARAN : Matematik <br>
KELAS : <?php
$kelas_nama=$_POST['kelas_id'];
$darjah_nama=$_POST['darjah_id'];
echo $darjah_nama; ?> <?php
echo $kelas_nama; ?> <br>
TAHUN : <?php $tahun_nama=$_POST['tahun_id']; echo $tahun_nama?>
</h4></center>
<center> <form id="import" name="import" method="post" enctype="multipart/form-data">
<input type="file" name="file" /><br />
<input class="btn btn-primary" type="submit" name="submit" value="Import"/> <br><br>
</form> </center>
<?php
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$murid_ic = $filesop[0];
$murid_nama = $filesop[1];
$murid_markah = $filesop[2];
$murid_gred = $filesop[3];
$tahun_id = $filesop[4];
$peperiksaan_id = $filesop[5];
$kelas_id = $filesop[6];
$darjah_id = $filesop[7];
$query = query("INSERT INTO murid (murid_ic,murid_nama, murid_markah, murid_gred, tahun_id, peperiksaan_id, kelas_id, darjah_id)
VALUES ('$murid_ic','$murid_nama','$murid_markah','$murid_gred','$tahun_id','$peperiksaan_id','$kelas_id','$darjah_id')");
$c = $c + 1;
}
if($query){
redirect("index.php?pages=peperiksaan2&tahun_id=$tahun_id&peperiksaan_id=$peperiksaan_id&darjah_id=$darjah_id&kelas_id=$kelas_id");
echo "You database has imported successfully. You have inserted ". $c ." recoreds";
}else{
echo "Sorry! There is some problem.";
}
}
?>
<table width="100%" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>BIL</th>
<th>NAMA</th>
<th>NO IC</th>
<th><center>MARKAH</center></th>
<th><center>GRED</center></th>
</tr>
</thead>
<?php
$bil = 0;
$SQLSELECT = "SELECT murid.*, peperiksaan.*, tahun.*, darjah.*, kelas.* FROM murid, peperiksaan, tahun, darjah, kelas WHERE murid.peperiksaan_id = peperiksaan.peperiksaan_id AND murid.tahun_id = tahun.tahun_id AND murid.darjah_id = darjah.darjah_id AND murid.kelas_id = kelas.kelas_id" ;
$result_set = mysqli_query($con, $SQLSELECT);
while($row = mysqli_fetch_array($result_set))
{
$bil++;
?>
<tr>
<td><?php echo $bil; ?></td>
<td><?php echo $row['murid_nama']; ?></td>
<td><?php echo $row['murid_ic']; ?></td>
<td><center><?php echo $row['murid_markah']; ?></center></td>
<td><center><?php echo $row['murid_gred']; ?></center></td>
</tr>
<?php
}
?>
it happen to be just fine.. but after importing a file.. the attributes wont read the values from options anymore. why is that happen? the import button leads to the same page anyway.
store your values in cookie and use when you want

multiple checkbox check if value in database?

code:
<?php
$id = $_GET['id'];
$sql = "select * from admin_menu where id = '$id'";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
$menu_name = $row['menu_name'];
$menu_link = $row['menu_link'];
$priority = $row['priority'];
$admin_id = explode(",", $row['admin_id']);
}
if(isset($_POST['update']))
{
$admin_id = $_POST['admin_id'];
$chk="";
foreach($admin_id as $chk1)
{
$chk .= $chk1.",";
}
$menu_name = $_POST['menu_name'];
$menu_link = $_POST['menu_link'];
$priority = $_POST['priority'];
$sql = "update admin_menu set menu_name = '$menu_name', menu_link = '$menu_link', priority = '$priority', admin_id = '$chk' where id = '$id'";
$result = mysqli_query($link,$sql);
if($result == true)
{
$msg .= "<h3 style='color:green;'>update</h3>";
}
else
{
$msg .= "<h3 style='color:red;'>Error!</h3>";
}
}
?>
<form name="myform" method="post" >
<div class="row">
<label for="Producer_firstname">Admin Name</label>
<?php
foreach ($admin_id as $admin_id)
{
$chk = "";
if (in_array($chk, $admin_id))
{
$chk = 'checked="checked" ';
}
echo '<input type="checkbox" name="admin_id[]" value="'.$admin_id.'" '.$chk.'/><br/>';
}
?>
</div>
<div class="row">
<label for="Producer_firstname">Menu Name </label>
<input size="60" maxlength="255" name="menu_name" id="menu_name" value="<?php echo $menu_name; ?>" type="text" />
</div>
<div class="row">
<label for="Producer_lastname" >Menu Link </label>
<input size="60" maxlength="255" name="menu_link" id="menu_link" type="text" value="<?php echo $menu_link; ?>" />
</div>
<div class="row">
<label for="Producer_lastname" >Priority</label>
<select name="priority" id="priority">
<option value="<?php echo $priority; ?>"><?php echo $priority; ?></option>
<option value="">choose any one</option>
<option value="1">1</option>
<option value="0">0</option>
</select>
</div>
<div class="row buttons">
<button type="submit" name='update' id='update'>update Menu</button>
</div>
</form>
In this code I am fetching multiple checkbox value from table admin2 and I want when I update form value checkbox check if the value of checkbox is exist into database. How can I fix it ?
Thank You
Your code has few issues,
1. Update should be done before select query
2. List of admin not managed separately
3. Priority radio buttons not managed properly
Additional Suggestions,
1. Use prepare query statements
2. use implode for appending multiple values instead of foreach
3. print admin names before checkboxes
<?php
$id = $_GET['id'];
if(isset($_POST['update']))
{
$chk = implode(',', $_POST['admin_id']);
$menu_name = $_POST['menu_name'];
$menu_link = $_POST['menu_link'];
$priority = $_POST['priority'];
$sql = "update admin_menu set menu_name = '$menu_name', menu_link = '$menu_link', priority = '$priority', admin_id = '$chk' where id = '$id'";
$result = mysqli_query($link,$sql);
$msg = "";
if($result == true)
{
$msg .= "<h3 style='color:green;'>update</h3>";
}
else
{
$msg .= "<h3 style='color:red;'>Error!</h3>";
}
echo $msg;
}
$sql = "select * from admin_menu where id = '$id'";
$result = mysqli_query($link,$sql);
$row = mysqli_fetch_array($result);
$menu_name = $row['menu_name'];
$menu_link = $row['menu_link'];
$priority = $row['priority'];
$admin_id = explode(",", $row['admin_id']);
$admins = array('admin1', 'admin2', 'admin3', 'admin4', 'admin5', 'admin6', 'admin7', 'admin8');
?>
<form name="myform" method="post" >
<div class="row">
<label for="Producer_firstname">Admin Name</label>
<?php
foreach ($admins as $admin)
{
$chk = "";
if (in_array($admin, $admin_id))
{
$chk = 'checked="checked" ';
}
echo $admin.' <input type="checkbox" name="admin_id[]" value="'.$admin.'" '.$chk.'/><br/>';
}
?>
</div>
<div class="row">
<label for="Producer_firstname">Menu Name </label>
<input size="60" maxlength="255" name="menu_name" id="menu_name" value="<?php echo $menu_name; ?>" type="text" />
</div>
<div class="row">
<label for="Producer_lastname" >Menu Link </label>
<input size="60" maxlength="255" name="menu_link" id="menu_link" type="text" value="<?php echo $menu_link; ?>" />
</div>
<div class="row">
<label for="Producer_lastname" >Priority</label>
<select name="priority" id="priority">
<option value="1" <?php if($priority == 1) echo "selected='selected'"; ?>>1</option>
<option value="0" <?php if($priority == 0) echo "selected='selected'"; ?>>0</option>
</select>
</div>
<div class="row buttons">
<button type="submit" name='update' id='update'>update Menu</button>
</div>
</form>

Categories