Delete row from MySQL table when I press button Delete - php

I need to make cancel booking of room for one user who can see all his room booked.
The problem is It shows the booked room for the user, but the delete button did not work correctly.
I have snapshot of my db here and screenshot here.please any help?
Thanks.
Here is my php code for show booking data and the delete part in manage.php
<?php
session_start();
$connection = mysqli_connect('localhost', 'root', '', 'webapp');
global $connection;
$user_id="";
if(!isset($_SESSION['login_ID'])) {
header('Location: login.php');
die();
} else {
$user_id= $_SESSION['login_ID'];
}
$secondQueryStmt = "SELECT * FROM `booking` where UserID='".$user_id."'";
$query = mysqli_query($connection, $secondQueryStmt);
if(isset($_POST['BookingID'])) {
$boking_id = $_POST['BookingID'];
if(isset($_POST['delete_id'])){
$query = mysqli_query($connection, "DELETE FROM booking where BookingID= '".$boking_id."'");
}
}
?>
here is my form html in the same page also.
<article id="content" style="height:810px; background-color:white">
<div class="box1">
<!--content herer-->
<table class="wrapper" style="text-align:center">
<tr>
<th class="track animated fadeInUp">Room</th>
<th class="title animated fadeInUp">Quantity</th>
<th class="speaker animated fadeInUp">CheckIn Date</th>
<th class="day animated fadeInUp">CheckOut Date </th>
<th class="time animated fadeInUp"></th>
</tr>
<?php while($fetcher = mysqli_fetch_array($query)): ?>
<tr>
<td><?=$fetcher['Type']?></td>
<td><?=$fetcher['Quantity']?></td>
<td><?=$fetcher['CheckInDate']?></td>
<td><?=$fetcher['CheckOutDate']?></td>
<td><td><form action="manage.php" method= "post" />
<input type="hidden" name="b_id" value="<?=$fetcher['BookingID'] ?>"/>
<input type="button" class="btnSm hvr-fade lightRed" name="delete_id" value="Cancel Booking"/>
</form></td></tr>
<?php endwhile; ?>
</table>
</div>
</article>

Look at this statement here,
<input type="hidden" name="b_id" value="<?=$fetcher['BookingID'] ?>"/>
^ you've named it b_id, not BookingID
So change
$boking_id = $_POST['BookingID'];
to
$boking_id = $_POST['b_id'];
And use mysqli_affected_rows() function to check how many rows are affected from this UPDATE operation.
Here's the reference:
http://php.net/manual/en/mysqli.affected-rows.php
So your code should be like this:
// your code
$query = mysqli_query($connection, $secondQueryStmt);
if(isset($_POST['delete_id'])){
$boking_id = $_POST['b_id'];
mysqli_query($connection, "DELETE FROM booking where BookingID= '".$boking_id."'");
if(mysqli_affected_rows($connection)){
// success
}else{
// failure
}
}

Related

How to store the old data of a table before update query

How can I store the old value of the selected ID in a table before update query? I have this code for my insert query and update query . I want to save the old value of the selected ID before my UPDATE query.
<form method="post" id="reg-form" action="user_save">
<div class="box-body">
<div class="row">
<div class="col-md-12">
<table id="example1" class="table table-bordered table-striped" style="margin-right:-10px">
<thead>
<tr>
<th>RECRUITER</th>
</tr>
</thead>
<?php
include('../dist/includes/dbcon.php');
$bdo=$_SESSION['name'];
$query=mysqli_query($con,"SELECT * FROM accounts_at ")or die(mysqli_error());
while($row=mysqli_fetch_array($query)){
$id=$row['id'];
$rec=$row['rec'];
<tr>
<td><?php echo $rec;?></td>
<td>
<a id="click" href="user_edit.php?id=<?php echo $id;?>">
<i class="glyphicon glyphicon-edit text-blue"></i></a>
</td>
And this is my user_update query .
<?php session_start();
if(empty($_SESSION['id'])):
header('Location:../index');
endif;
include('../dist/includes/dbcon.php');
$id = mysqli_real_escape_string($con, $_POST['id']);
$rec= mysqli_real_escape_string($con,$_POST['rec']);
mysqli_query($con,"UPDATE accounts_at set rec='$rec' where id='$id'")or
die(mysqli_error());
echo "<script type='text/javascript'>alert('Successfully updated account
details!');</script>";
echo "<script>document.location='update_acc'</script>";
?>
it is dependent on how you want to save this data ?
you can save it in file as you can use fopen() and write it first
or send it to your self via mail()
$save_method = //method you want to save data
if($save_method){//code to update data}

trying to update the records but failing in php

im trying to a update a record by getting the id of the post via $_GET then update the records through $_POST.
i already have performed the delete action through $_GET it works fine also the mysqli_fetch_assoc works fine for displaying the record for editing but the actual editing does not happens it gives a Empty error from the validation in the code in the empty check function.
i have gone through lots research but cant seem to get my head around the error , i would thank full if any one could suggest any changes in the code.
Thank you in advance!
This is the error
Notice: Undefined index: id in /then the long url etc/
Below is the code
<?php
//DB Connection
include'include/db-conn.php';
if (isset($_POST['edit'])) {
//Raw GET Inputs
$raw_c_id = $_GET['id'];
//Cleaned Inputs
$c_c_id = filter_var($raw_c_id, FILTER_SANITIZE_STRING);
//Error Mwssages
$empty = '<div class="alert alert-danger alert-dismissible">
×
<strong>Error!</strong>Field is empty please provide content!
</div>
';
$success = '<div class="alert alert-success alert-dismissible fixed-top">
×
<strong>Success!</strong> Content Added Successfully
</div>
';
$not_success = '<div class="alert alert-danger alert-dismissible">
×
<strong>Not Success!</strong> Content Not Added Successfully
</div>
';
if (empty($c_c_id)) {
echo $empty;
exit();
header("Location:index.php");
}
$update = "UPDATE `continents`
SET `continent_name`='$c_c_name', `last_edited`='date(d/m/Y)'
WHERE `id`='$c_c_id'";
$run_update = mysqli_query($conn, $update);
if (!$run_update) {
header("Location: index.php");
echo $not_success;
}
else{
header("Location: index.php");
echo $success;
}
}
?>
This is the html part
<div class="panel-body">
<form action="edit.php" method="POST">
<div class="form-group">
<label for="continent_name">Continent Name</label>
<input required type="text" placeholder="10" class="form-control" value="<?php echo $c_name ; ?>" name="continent_name">
</div>
<small>Date Added: <?php echo $c_dated_added ; ?></small> / <small>Last Edited: <?php echo $c_last_edited ; ?></small>
<div class="form-group">
<input class="form-control btn-success" type="submit" name="edit" value="Submit">
</div>
</form>
</div>
Thid the while loop
<div class="table-responsive">
<table id="example" class="table table-hover ">
<thead>
<tr class="">
<th>ID</th>
<th>Continent Name</th>
<th>Date Added</th>
<th>Status</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$all_continents = "SELECT * FROM `continents` ORDER BY `status`";
$run = mysqli_query($conn,$all_continents);
while ($row_result = mysqli_fetch_assoc($run)) {
$id = $row_result['id'];
$c_continent_name = $row_result['continent_name'];
$c_date_added = $row_result['date_added'];
$c_status = $row_result['status'];
echo "
<tr>
<td>$id</td>
<td>$c_continent_name</td>
<td>$c_date_added</td>
<td>$c_status</td>
<td>
<a class='btn btn-info' href='edit.php?id=$id'>Edit</a>
</td>
<td>
<a class='btn btn-danger' href='delete.php?id=$id'>Delete</a>
</td>
</tr>
";
}
?>
</tbody>
</table>
It looks like you're trying to use GET and POST parameters at the same time. The reason it's not working is because the GET parameter is lost when you submit your form. You need to pass it on in the form's action attribute:
<form action="edit.php?id=<?php echo $_GET['id'] ?>" method="POST">
Please also take a look at the advice in this post:
Is there a way to use GET and POST together?

PHP: Flagged Comments

I'm implementing a flag comment system on my blog, currently have this code for it:
<form method="POST">
<?php
$con=mysqli_connect("localhost","root","admin","MyDatabase");
$sql = mysqli_query($con, "SELECT id, username, comment_body, user_id, DATE_FORMAT(date_created, '%M %d %Y') AS date_created FROM news_comments WHERE entry_id = '" . $entry_id. "'");
?>
<?php while ($comment = mysqli_fetch_array($sql)) { ?>
<table class="table">
<thead>
<tr>
...
</tr>
<td>
<?php echo $comment['username']; ?>
</br>
<?php echo $comment['comment_body']; ?>
</br>
<?php echo $comment['date_created'];?>
</td>
...
<td>
<button name = "report">Report</button>
<?php if(isset($_POST['report'])) {
$id = $comment['id'];
$conn=mysqli_connect("localhost","root","admin","MyDatabase");
$sqli = mysqli_query($con, "UPDATE news_comments SET isFlagged = 1 WHERE id = '$id'");
} ?>
</td>
....
</table>
It works, but when the report button is clicked, it flags all comments on the page. How can I change this so it's just the one specific comment?
Thanks
This is happening because you have your flagging logic in the while loop. Whenever a user clicks the Report button every condition where if(isset($_POST['report'])) is triggered - which would be all the comments.
You have to move the condition out of the loop (typically the top of the page), and then send the id of the post you want to flag as it will no longer be part of the loop.

$_GET does not work and query the property

I have a problem with $_GET method. I have retrieved some data about admins of a webpage from database & I added a hyperlink for users to get the information about that them.
Here's the code in my 1st page:
<?php if(($adminlevel)==1){
echo '
<h4 class="widgettitle">List of admins</h4>
<table class="table responsive">
<thead>
<tr>
<th>Admin Level</th>
</tr>
</thead>
'; getAdmins(); echo '
</table>
';
}else{
echo '<h4 class="widgettitle">You dont have permission to see this table</h4>';
}
?>
<div class="divider15"></div>
The function getAdmins() goes like this:
<?php
function getAdmins(){
global $con;
$get_admin = "select * from admins order by id";
$run_admin = mysqli_query($con,$get_admin);
while($row_admin = mysqli_fetch_array($run_admin)){
$id_admin = $row_admin['id'];
echo "
<tbody>
<tr>
<td>Trident</td>
<td class='center'><a href='editlevel.php?id=$id_admin' title='Clik to change admin level' target='_blank'>$adminlevel_admin</a></td>
</tr>
</tbody>
";
}
}
?>
As you see I link the users from my first page to another page which is called editlevel.php by the function getAdmins().
Therefore I made my hyperlink like this:
<a href='editlevel.php?id=$id_admin'>$adminlevel_admin</a>
And Here's the editlevel.php page:
<body>
<?php
if (isset($_GET['id_admin'])){
$result_id = $_GET['id_admin'];
$get_result = "select * from admins where id='$result_id'";
$run_result = mysqli_query($con,$get_result);
while($row_result= mysqli_fetch_array($run_result)){
$id_target = $row_result['id'];
$username_target = $row_result['username'];
$adminlevel_target = $row_result['adminlevel'];
$email_target = $row_result['email'];
echo '
<div class="mainwrapper">
<div class="header">
'; include "php/php_includes/overall/header.inc.php"; echo'
</div>
<div class="leftpanel">
';include "php/php_includes/overall/leftpanel.inc.php"; echo '
</div><!-- leftpanel -->
<div class="rightpanel">
'; include "php/php_includes/gadgets/rightpanel.editlevel.php"; echo '
</div><!--rightpanel-->
</div><!--mainwrapper-->
';
}
}
?>
</body>
So basically I used if (isset($_GET['id_admin'])){ to get the results of the item which user clicked & try to retrieve the data of that item from database via that.. But the problem is nothing appears at my screen. No error message & no result. Please if you know how can I solve it please let me know!
It appears that your link is:
<a href='editlevel.php?id=$id_admin'>$adminlevel_admin</a>
When it should be:
<a href='editlevel.php?id_admin=$id_admin'>$adminlevel_admin</a>
In order for it to work with:
if (isset($_GET['id_admin'])){
$result_id = $_GET['id_admin'];
Edit: It goes without saying, you should never trust user input (such as $_GET). These values should be validated and sanitised before being used in SQL queries.

update value from '0' to '1' on image click?

I'm new to php and mysql so sorry if i'm doing it wrong. i have a page on my site that lists the reviews that members give to other other users.
Basically i have approved and deleted in my database which means that after a user sends the review it has to be reviewed by the user before it gets displayed.
once the user clicks the approved image which is a tick it goes to approved_review.php and in their i have my sql code to update the value from 0 to 1 in my database.
It should work exactly the same for the delete but obviously instead of updating the approved column it will update deleted.
the code i have tried is not working i have been working on this for quite some time and can;t figure it out.
Can someone please tell me where i'm going wrong?
Heres the code:
<?php
$reviews_set = get_pending_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<p> </p>
<div class="pending-review-content">
<?php
$date = $reviews['date_added'];
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="message_pic"><?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">
<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" /></a>";?>
</div>
<div class="reviews-date"><? echo "$date"; ?></div>
<div class="reviews-from">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">{$reviews['display_name']}"; ?>
</a> Wrote:
</div>
<div class="reviews-content">
<?php echo "{$reviews['content']}"; ?>
</div>
</div>
<div class="reviews-approve">
<img src="assets/img/icons/tick.png" width="30" height="25" /></div>
<div class="reviews-delete">
<img src="assets/img/icons/cross.png" width="30" height="25" />
</div>
<? } ?>
approved_review.php function:
<?
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
echo "<div class=\"infobox1\">review approved.</div>";
?>
Your approach seems logical. After you loop through your reviews, you click on the tick or delete pngs to update or delete.
So, in approved_review.php
<?php
//you are missing the connection to your mysql database...
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
//execute the mysql query
$r = mysql_query($sql);
if (!mysql_error())
{
echo "<div class=\"infobox1\">Review Approved.</div>";
}
?>
a little edit rrrfusco's post
// or die for details if mysql_query won't work correct
$r = mysql_query($sql) or die (mysql_error());

Categories