PHP MYSQLi Database not updating - php

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"]; ?>">

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

getting error when post select value

I have a small problem.
<select name="level_id"> is not posting.
So i get an error like : Undefined index : level_id
Exactly what can i do?
$sql1 = 'SELECT T_ABILITY.PK AS AB_PK,T_ABILITY.ABILITY_NAME AS AN,T_ABILITY_LEVEL.PK AS LE_PK,T_ABILITY_LEVEL.LEVEL_NAME AS LN
FROM T_USER_ABILITY_REL,T_ABILITY,T_ABILITY_LEVEL WHERE
T_USER_ABILITY_REL.ABILITY_FK = T_ABILITY.PK AND
T_USER_ABILITY_REL.ABILITY_LEVEL_FK = T_ABILITY_LEVEL.PK AND
T_USER_ABILITY_REL.USER_FK = '.$user_id.'
ORDER BY AN';
$stmt1 = oci_parse($conn, $sql1);
$r1 = oci_execute($stmt1);
while ($row1 = oci_fetch_array($stmt1, OCI_RETURN_NULLS + OCI_ASSOC)) {
echo '<form method="post">';
echo '<tr>';
echo '<td>'.$row1["AN"].'</td>';
echo '<input type="hidden" name="ability_id" value="'.$row1["AB_PK"].'"/>';
echo '<td class="select-level">';
$sql2 = 'SELECT PK,LEVEL_NAME FROM T_ABILITY_LEVEL ORDER BY LEVEL_ORDER';
$stmt2 = oci_parse($conn, $sql2);
$r2 = oci_execute($stmt2);
echo '<select name="level_id" class="form-control selectpicker" data-container="body" data-live-search="true" data-size="5" title="Seviye Seçiniz">';
while ($row2 = oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC)) {
echo '<option '.($row2["PK"] == $row1["LE_PK"] ? 'selected="selected"' : "").' value="'.$row2["PK"].'">'.$row2["LEVEL_NAME"].'</option>';
}
echo '</select>';
echo '<button type="submit" name="update-user-ability" class="btn btn-success">Güncelle</button>';
echo '<button type="submit" name="delete-user-ability" class="btn btn-danger">Sil</button>';
echo '</td>';
echo '</tr>';
echo '</form>';
}
submit part below
if (isset($_POST["update-user-ability"])) {
$user_id = $_GET["user_id"];
$ability_id = $_POST["ability_id"];
$level_id = $_POST['level_id'];
This is because your select is empty (that is, the oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC returns no rows.)
A select, when no option are present, will not return a value to the receiving php script.
Add a default option before the loop to be sure something is passed even if your sql query returns nothing.
this script will return an empty post:
<html>
<body>
<form method="post">
<select name="select"></select>
<input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(0) { }
while this one will have the value 12 defined:
<html>
<body>
<form method="post">
<select name="select">
<option value="12">12</option>
</select>
<input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(1) { ["select"]=> string(2) "12" }
Could be that the value of level_id isn't send along, because it is a <select>. Not sure, but could be that only <input /> values are properly posted.
You could use a <input type="hidden" name="level_id_val" /> and update its value with JavaScript every time the value of <select name="level_id"> is changed (using .onchange).
Then, in PHP, use that field instead of the select: $level_id = $_POST['level_id_val'];

How to add to this update SQL statement and make a drop down list too for it

Okay i have this SQL statement for update which works find but I want to add locations which is in te_venue table after adding the location into sql i want for locations to be a drop down list which user can pick one location from list and when clicked on update it should update.
This is the php code that I have already (I need to add location into this)
<?php
$eventID = $_GET['id'];
if(isset($_POST['submit']))
{
$title = $_POST['title'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
$price = $_POST['price'];
$description = $_POST['description'];
$sql = "UPDATE te_events SET eventTitle='$title',eventStartDate='$startdate',eventEndDate='$enddate',eventPrice='$price',eventDescription='$description' WHERE eventID=$eventID";
if ($conn->query($sql) === TRUE)
{
header('Location:edit.php');
}
else
{
echo "Error updating record";
}
}
$sql = "SELECT * FROM te_events where eventID='$eventID'";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
$eventTitle = $row['eventTitle'];
$eventDescription = $row['eventDescription'];
$eventStartDate = $row['eventStartDate'];
$eventEndDate = $row['eventEndDate'];
$eventPrice = $row['eventPrice'];
}
?>
And this is where i want to have a drop down list for location.
<form method="post" action="">
<label for="title">Title</label><br/>
<input type="text" name="title" required/ value="<?php echo $eventTitle; ?>"><br/>
<label for="startdate">Start Date</label><br/>
<input type="date" name="startdate" required/ value="<?php echo $eventStartDate; ?>"><br/>
<label for="enddate">End Date</label><br/>
<input type="date" name="enddate" required/ value="<?php echo $eventEndDate; ?>"><br/>
<label for="price">Price (£)</label><br/>
<input type="number" step="any" name="price" required/ value="<?php echo $eventPrice; ?>"><br/>
<label for="description">Description</label><br/>
<textarea name="description" cols="55" rows="5"><?php echo $eventDescription; ?></textarea><br/>
<input type="submit" name="submit" value="Update" class="button">
</form>
Here is a screenshot of my tables too it might help.
https://postimg.org/image/6ui8nsi55/
Do you want something like this? Getting the location from te_venue and adding the results to a select?
<select>
<?php
$sql = "SELECT location FROM te_venue";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
}
?>
<option value="<?php echo $row['location'];?>"><?php echo $row['location'];?> </option>
<?php
}
?>
</select>
<label for="location">Choose a location: </label>
<select class="form-control" name="location">
<option value="London">London</option>
<option value="Paris">Paris</option>
<option value="Rome">Rome</option>
<option value="Berlin">Berlin</option>
<option value="Moscow">Moscow</option>
</select>

php - Can't delete input in table using sql statement

I don't know what I have done to this but I had the form working and now it has stopped. My problems are with the bottom if statement where I am trying to delete the user. As I said it was working properly and now it has stopped. I just can't figure out what the issue is? And yes I do backup but clearly should do it more!
function HMdisplayrooms() {
global $wpdb;
echo '<html><body><h1>Display Rooms</h1>';
echo '<p>Order room view by: <p>
<form name = "view_HM_rooms" method="post" action="">
<select name="roomsView" size = "1">
<option value = "room_id">Room ID</option>
<option value = "room_type">Room Type</option>
</select></br>
<input type="submit" name="action">
</form></body></html>';
echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
if(isset($_POST['action'])) {
$roomType = $_POST['roomsView'];
$query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
$rooms = $wpdb->get_results($query);
foreach ($rooms as $room) {
$roomID = ($room->room_id);
echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
<input type="submit" name="action2" value="Delete Room"></form></td></tr>';
if(isset($_POST['action2'])){
$results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
$msg = "Room deleted";
return $msg;
}
}
}
}
Use the code like this,
function HMdisplayrooms() {
global $wpdb;
echo '<html><body><h1>Display Rooms</h1>';
echo '<p>Order room view by: <p>
<form name = "view_HM_rooms" method="post" action="">
<select name="roomsView" size = "1">
<option value = "room_id">Room ID</option>
<option value = "room_type">Room Type</option>
</select></br>
<input type="submit" name="action">
</form></body></html>';
echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
if(isset($_POST['action'])) {
$roomType = $_POST['roomsView'];
$query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
$rooms = $wpdb->get_results($query);
foreach ($rooms as $room) {
$roomID = ($room->room_id);
echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
<input type="hidden" name="DelRooMId" value="'.$roomID.'">
<input type="submit" name="action2" value="Delete Room"></form></td></tr>';
}
}
if(isset($_POST['action2'])){
$roomID = $_POST['DelRooMId']; // sanitize the input
$results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
$msg = "Room deleted";
return $msg;
}
}
I have pulled the DELETE block outside of your code. It may help you. The problem with your code is $_POST['action'] will be null at the time of DELETE operation , so the code never executes, that may be the error
Note - Make sure to get the $roomID when you are using the below structure

Need help for database update query in php form - Joomla database

Let's say I have a table with 10 records and I want to take name, lastname and rank from those 10 records. First I do something like this:
<?php // DATABASE SELECT QUERY
$db =& JFactory::getDBO();
$query="SELECT name, lastname, rank
FROM table
ORDER BY rank ASC";
$db->setQuery($query);
$rows = $db->loadObjectList(); ?>
Then, I add some fields in to my form that contain table's values, so I can edit them through form:
<form action="#" method="post" name="form">
<table><?php $count = 0; while ($count < 10){
$name = $rows[$count]->name;
$lastname = $rows[$count]->lastname;
$rank = $rows[$count]->rank; ?>
<tr>
<td><input name="name" value="<?php echo $name ?>" type="text" /></td>
<td><input name="lastname" value="<?php echo $lastname ?>" type="text" /></td>
<td><select name="rank">
<option value="<?php echo $rank ?>"><?php echo $rank ?></option>
<option disabled="disabled" value="...">...</option>
<?php for ($i = 0; $i <= 100; $i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
</select></td>
</tr><?php $count++;}?>
</table>
<input class="something" name="updatemod" type="submit" value="UPDATE" />
</form>
Next, before Select query, I have add an update query using this method below, so when I press the update button, update my DB:
// DATABASE UPDATE QUERY
if (isset($_POST['updatemod']) or isset($_GET['updatemod'])){
$db =& JFactory::getDBO();
$query = "UPDATE table
SET name = '".$_POST["name"]."',
SET lastname = '".$_POST["lastname"]."',
SET rank = '".$_POST["rank"]."'
";
$db->setQuery($query);
$db->query();}
But... Nothing is working!!! I have done exactly the same thing for an other form and it's working perfect! The only difference between those two forms, is that I am not using this while loop at the other form. So, maybe it has to do with this or something??? I don't know, at this point is where my knowledge confused, so I need your help!
I think you are trying to update all the rows in your table.
If that is the case,You need to do something like this.
<?php // DATABASE SELECT QUERY // also you should select your unique id field
$db =& JFactory::getDBO();
$query="SELECT name, lastname, rank,id
FROM table
ORDER BY rank ASC";
$db->setQuery($query);
$rows = $db->loadObjectList(); ?>
In your form you are placing element name as single but your requirement is to edit all the rows at one click you should use input names as array.Also here you have to introduce a new id field too for update condition
<form action="#" method="post" name="form">
<table><?php $count = 0; while ($count < 10){
$name = $rows[$count]->name;
$lastname = $rows[$count]->lastname;
$rank = $rows[$count]->rank;
$id = $rows[$count]->id; ?>
<tr>
<td><input name="uid[]" value="<?php echo $id?>" type="hidden" />
<input name="name[]" value="<?php echo $name ?>" type="text" /></td>
<td><input name="lastname[]" value="<?php echo $lastname ?>" type="text" /></td>
<td><select name="rank[]">
<option value="<?php echo $rank ?>"><?php echo $rank ?></option>
<option disabled="disabled" value="...">...</option>
<?php for ($i = 0; $i <= 100; $i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
</select></td>
</tr><?php $count++;}?>
</table>
<input class="something" name="updatemod" type="submit" value="UPDATE" />
</form>
Also in your Update Query also required loop and where cluase
// DATABASE UPDATE QUERY
if (isset($_POST['updatemod']) or isset($_GET['updatemod'])){
$db =& JFactory::getDBO();
$total_rows = sizeof($_POST["uid"]);
for($i =0; $i<$total_rows;$i++){
$query = "UPDATE table
SET name = '".$_POST["name"][$i]."',
lastname = '".$_POST["lastname"][$i]."',
rank = '".$_POST["rank"][$i]."'
WHERE id = '".$_POST["uid"][$i]."'
";
$db->setQuery($query);
$db->query();
}
}
I think this will solve your problem.
Here i just mentioned the "id" as your unique key of the table that may differ.But you will get the idea , i hopes
But this is a worst case update condition Bcoz you just imagine the table with 1000 rows.
It will take too long to get the result.
Try to update single row with proper method.It is the best method.
Hope this may help you.

Categories