delete button is not working - php

I am going to delete the particular row of my database, but when i delete the button it tells me that undefined display in deletebookajax.php on line no 54..
So can u please tell me where i have gone wrong...
here is my code
book.php is for displaying the entire book details....
<?php
include('assets/page_header.php');
include('db/db.php');
?>
<html>
<head><title></title>
<style>
h1 {
text-align:center;
}
</style>
</head>
<h1>BOOKS PAGE</h1>
<?php
error_reporting(0);
$str="select * from books";
$query1=mysql_query($str);
$q=mysql_num_rows($query1);
//$query2=mysql_query("select status from bookrentalinfo where bookid=$bookid");
//echo $query2;
//$res=mysql_fetch_array($query2);
echo "<table align='center'>";
echo "<tr><th>BookID</th><th>Title</th><th>Author</th><th>Publisher</th><th>Numcopies</th><th>Shelfno</th><th>Status</th><th colspan=2 >Action</th></tr>";
while($rows=mysql_fetch_array($query1))
{
echo "<tr>";
echo "<td>".$rows['bookid']."</td>";
echo "<td>".$rows['title']."</td>";
echo "<td>".$rows['author']."</td>";
echo "<td>".$rows['publisher']."</td>";
echo "<td>".$rows['numcopies']."</td>";
echo "<td>".$rows['shelfno']."</td>";
echo "<td>".$rows['status']."</td>";
echo "<td><button class='button1' data-toggle='tooltip' title='Delete' id=".$rows['bookid']." value='delete' name='delete'><img id='image' src='./images/trash.png'/></button></td>";
echo "<td><a id='colour' class='tooltip' href='edit1form.php?book_id=".$rows['bookid']."'><img id='image' src='./images/small.gif'/><span class='tooltiptext' >Edit</span></a></td>";
echo "</tr>";
}
echo "</table>";
?>
<div id="display">
</div>
<!--echo "<td><button type='button'>delete</button></td>";
/*if($res['status']=="BORROWED")
{
echo "Sorry You Can't Delete The Book";
}
else
{
echo "The Row Is Deleted";
}*/-->
<script type="text/javascript">
$('document').ready(function() {
$(".button1").click(function(e) {
$('[data-toggle="tooltip"]').tooltip();
var strconfirm = confirm("Are you sure you want to delete?");
if(strconfirm)
{
var bookid=$(this).attr("id");
alert(bookid);
dataString='book_id='+bookid;
alert(dataString);
// AJAX Code To Submit Form.
$.ajax({
type: "GET",
url: "db/deletebookajax.php",
data: dataString,
cache: false,
success: function(result){
alert("submitted"+result);
$('#display').html(result);
window.location.href="books.php";
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
else {
return false;
}
});
});
</script>
</body>
</html>
deletebookajax.php
<?php
if(isset($_GET['book_id']))
{
$bookid = $_GET['book_id'];
echo $bookid;
}
include('../assets/page_header.php');
?>
<input type="hidden" name="bookid" value=<?php if(isset($bookid)) echo $bookid; ?>>
<?php
include('db.php');
//if(isset($_POST['bookid']))
//
//$bookid=mysql_real_escape_string($_POST['bookid']);
$delete = "delete from books WHERE bookid=$bookid";
$query1=mysql_query($delete);
echo $query1;
if($query1)
{
$q=mysql_query("select * from books");
echo $q;
$display="<table>";
$display.="<tr><th>BookID</th><th>Title</th><th>Author</th><th>Publisher</th><th>numcopies</th><th>shelfno</th><th>status</th><th>Action</th></tr>";
while($row=mysql_fetch_array($q))
{
$display.="<tr>";
$display.= "<td>".$row['bookid']."</td>";
$display.= "<td>".$row['title']."</td>";
$display.= "<td>".$row['author']."</td>";
$display.= "<td>".$row['publisher']."</td>";
$display.="<td>".$row['numcopies']."</td>";
$display.="<td>".$row['shelfno']."</td>";
$display.="<td>".$row['status']."</td>";
$display.= "</tr>";
}
$display.="</table>";
}
else
{
$display.= "U can't delete The book";
}
//echo "nothing";
echo $display;
?>
</body>
</html>

Change Your deletebookajax.php code
<?php
if(isset($_GET['book_id']))
{
$bookid = $_GET['book_id'];
echo $bookid;
}
include('../assets/page_header.php');
?>
<input type="hidden" name="bookid" value=<?php if(isset($bookid)) echo $bookid; ?>>
<?php
//include('db.php');
//if(isset($_POST['bookid']))
//
//$bookid=mysql_real_escape_string($_POST['bookid']);
$user="root";
$server="localhost";
$password="";
$db="library book";
$dbconn = mysqli_connect($server,$user,$password,$db);
$delete = "delete from books WHERE bookid=$bookid";
$query1=mysqli_query($dbconn, $delete);
echo $query1;
if($query1)
{
$q=mysqli_query($dbconn, "select * from books");
echo $q;
$display="<table>";
$display.="<tr><th>BookID</th><th>Title</th><th>Author</th><th>Publisher</th><th>numcopies</th><th>shelfno</th><th>status</th><th>Action</th></tr>";
while($row=mysqli_fetch_array($dbconn, $q))
{
$display.="<tr>";
$display.= "<td>".$row['bookid']."</td>";
$display.= "<td>".$row['title']."</td>";
$display.= "<td>".$row['author']."</td>";
$display.= "<td>".$row['publisher']."</td>";
$display.="<td>".$row['numcopies']."</td>";
$display.="<td>".$row['shelfno']."</td>";
$display.="<td>".$row['status']."</td>";
$display.= "</tr>";
}
$display.="</table>";
}
else
{
$display = "U can't delete The book";
}
//echo "nothing";
echo $display;
?>
</body>
</html>

Related

Pagination doesn't work

I am going to make a pagination for my members page, I have a created 8 pagination numbers from 1 to 8 so that when i click on each page , it should display only 1 row of particular database table,database table consists of 8 rows, But in my code when i click , correct page numbers are passing but it doesn't update and display particular selected pagination row , please can u help me in getting solution to this please.
members.php
<?php
include('assets/page_header.php');
include('db/db.php');
$page="";
if(isset($_GET['page']))
{
$page=$_GET['page'];
}
?>
<html>
<head><title></title>
<style>
h1 {
text-align:center;
}
</style>
</head>
<?php
/*$memno=mysql_real_escape_string($_POST['memno']);
$memname=mysql_real_escape_string($_POST['name']);
$address=mysql_real_escape_string($_POST['address']);
$phonenumber=mysql_real_escape_string($_POST['phno']);
$renewaldate=mysql_real_escape_string($_POST['renewaldate']);
$maxborrowlimit=mysql_real_escape_string($_POST['maxborrowlimit']);
$status=mysql_real_escape_string($_POST['status']);*/
?>
<h1>MEMBERS PAGE</h1>
<?php
$str="select * from member LIMIT 1,1 ";
$query1=mysql_query($str);
echo "<table align='center'>";
echo "<tr><th><MemberNumber</th><th>MemberName</th><th>Address</th><th>Phonenumber</th><th>RenewalDate</th><th>MaxBorrowLimit</th><th>Status</th><th colspan=2>Action</th></tr>";
while($query2=mysql_fetch_array($query1))
{
echo "<tr>";
echo "<td>".$query2['memno']."</td>";
echo "<td>".$query2['name']."</td>";
echo "<td>".$query2['address']."</td>";
echo "<td>".$query2['phno']."</td>";
echo "<td>".$query2['renewaldate']."</td>";
echo "<td>".$query2['maxborrowlimit']."</td>";
echo "<td>".$query2['status']."</td>";
echo "<td><button class='button1' data-toggle='tooltip' title='Delete' id=".$query2['memno']." value='delete' name='delete'><img id='image' src='./images/trash.png'/></button></td>";
echo "<td><a id='colour' class='tooltip' href='membereditform.php?mem_no=".$query2['memno']."'><img id='image' src='./images/small.gif'/><span class='tooltiptext' >Edit</span></a></td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
if($page=="" || $page=='1')
{
$page1=0;
}
else
{
$page1=($page*4)-4;
}
$query1=mysql_query("select * from member");
$count=mysql_num_rows($query1);
$s=$count/1;
echo $s;
echo "<br>";
for($i=1;$i<=$s;$i++)
{
?><a href="members.php?page=<?php echo $i ?>" style="text-decoration":none,"Text-align":center;><?php echo $i?></a><?php
}
?>
Try your code like this:
<?php
include('assets/page_header.php');
include('db/db.php');
?>
<html>
<head><title></title>
<style>
h1 {
text-align:center;
}
</style>
</head>
<?php
/*$memno=mysql_real_escape_string($_POST['memno']);
$memname=mysql_real_escape_string($_POST['name']);
$address=mysql_real_escape_string($_POST['address']);
$phonenumber=mysql_real_escape_string($_POST['phno']);
$renewaldate=mysql_real_escape_string($_POST['renewaldate']);
$maxborrowlimit=mysql_real_escape_string($_POST['maxborrowlimit']);
$status=mysql_real_escape_string($_POST['status']);*/
?>
<h1>MEMBERS PAGE</h1>
<?php
$num_rec_per_page = 10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$str = "select * from member LIMIT $start_from, $num_rec_per_page";
$query1=mysql_query($str);
echo "<table align='center'>";
echo "<tr><th><MemberNumber</th><th>MemberName</th><th>Address</th><th>Phonenumber</th><th>RenewalDate</th><th>MaxBorrowLimit</th><th>Status</th><th colspan=2>Action</th></tr>";
while($query2=mysql_fetch_array($query1))
{
echo "<tr>";
echo "<td>".$query2['memno']."</td>";
echo "<td>".$query2['name']."</td>";
echo "<td>".$query2['address']."</td>";
echo "<td>".$query2['phno']."</td>";
echo "<td>".$query2['renewaldate']."</td>";
echo "<td>".$query2['maxborrowlimit']."</td>";
echo "<td>".$query2['status']."</td>";
echo "<td><button class='button1' data-toggle='tooltip' title='Delete' id=".$query2['memno']." value='delete' name='delete'><img id='image' src='./images/trash.png'/></button></td>";
echo "<td><a id='colour' class='tooltip' href='membereditform.php?mem_no=".$query2['memno']."'><img id='image' src='./images/small.gif'/><span class='tooltiptext' >Edit</span></a></td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
$sql = "select * from member";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
if($total_records > 0) {
echo "<a href='members.php?page=1'> ".'<'." </a> "; // Goto 1st page
}
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='members.php?page=".$i."'> ".$i." </a> ";
};
if($total_records > 0) {
echo "<a href='members.php?page=$total_pages'> ".'>'." </a> "; // Goto last page
}
?>
</html>

How can I make my submit button add the values to my cart table

I'm trying to get the "Cart_View02" table to update when the submit button is clicked. I'm new to php and sql this semester. Anybody know what I'm doing wrong?
<?PHP
if (isset($_GET['tab'])) $table= $_GET['tab'];
else $table="Cart_view02";
$reset = true;
$errmsg = array("","");
//1. Make a connection to the database
$dbconn = mysqli_connect("localhost","root","","mydatabase1")
or die(mysqli_connect_error());
if($_SERVER['REQUEST_METHOD']=='POST'){
$action = $_POST['action'];
$qty = $_POST['txtqty'];
$crtnum = $_POST[100];
$itemid = $_POST[$row[0]];
if($action == "select"){
$sql = "INSERT INTO cart_lineitems VALUES ($crtnum, '$itemid',
'$qty');";
$result = mysqli_query($db,$sql) or die(mysql_error($db));
}
//print_r($_POST);
header("location: cartdump.php");
}
?>
<!DOCTYPE html>
<html>
<head><title>Order Page</title>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
</script>
<img class="banner" src="Images/acme_banner.jpg" alt="Acme Spook Shoppe"/>
</head>
<body>
<br><br>
Home
<form name="form1" method='POST'>
<?php
if (isset($_GET['prod'])) $prod=$_GET['prod'];
else $prod = "arm01";
//echo $prod; //<------To print results
$sql = "select productid,name,imagefile, "
."shortdescription, longdescription,"
."unitprice "
."from products "
."where productid='$prod'";
echo "<br>";
//echo $sql; //<------To print results
//2. Run a query against the database
$result = mysqli_query($dbconn,$sql)
or die(mysqli_error($dbconn));
//print_r($result); //<------To print results
//3. Return the results
echo "<table class=ordertable >";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[1]<br><br>";
echo "$row[3]<br><br>";
echo "$row[4]<br><br>";
echo "Price: $row[5]<br><br>";
echo "<input type='text' name='txtqty' value='1' size=2 maxlength=2>";
echo "<input type='submit' name='btnadd' value='Add To Cart'
OnClick='SubmitForm'>";
echo "</td>";
echo "<td><img src='images/products/$row[2]' "
."height=300px width =250px></td>";
echo "</tr>";
echo "</table>";
//print_r($row);
}
//4. Release the resources
mysqli_free_result($result);
//5. Close the connection
mysqli_close($dbconn);
?>
</form>
</body>
</html>
Any help on this would be greatly appreciated.
This is what I ended up with:
<!DOCTYPE html>
<?PHP
print_r($_POST);
if (isset($_GET['prod'])) $prod=$_GET['prod'];
else $prod = "arm01";
//if (isset($_GET['tab'])) $table= $_GET['tab'];
// else $table="Cart_view02";
//$reset = true;
//$errmsg = array("","");
//1. Make a connection to the database
$dbconn = mysqli_connect("localhost","root","","mydatabase1")
or die(mysqli_connect_error());
if($_SERVER['REQUEST_METHOD']=='POST'){
$qty = $_POST['txtqty'];
$crtnum = 100;
if (preg_match("/^[0-9]{1,2}$/",$qty)){
$sql = "INSERT INTO cart_lineitems VALUES ($crtnum, '$prod', $qty);";
$result = mysqli_query($dbconn,$sql) or die(mysqli_error($dbconn));
//echo"<br>$sql";
header("location: cartdump.php");
}
else echo "Quantity is not valid<br>";
}
?>
<html>
<head><title>Order Page</title>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
</script>
<img class="banner" src="Images/acme_banner.jpg" alt="Acme Spook Shoppe"/>
</head>
<body>
<br><br>
Home
<form name="form1" method='POST'>
<?php
//echo $prod; //<------To print results
$sql = "select productid,name,imagefile, "
."shortdescription, longdescription,"
."unitprice "
."from products "
."where productid='$prod'";
echo "<br>";
//echo $sql; //<------To print results
//2. Run a query against the database
$result = mysqli_query($dbconn,$sql)
or die(mysqli_error($dbconn));
//print_r($result); //<------To print results
//3. Return the results
echo "<table class=ordertable >";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[1]<br><br>";
echo "$row[3]<br><br>";
echo "$row[4]<br><br>";
echo "Price: $row[5]<br><br>";
echo "<input type='text' name='txtqty' value='1' size=2 maxlength=2>";
echo "<input type='submit' name='btnadd' value='Add To Cart' OnClick='SubmitForm'>";
echo "</td>";
echo "<td><img src='images/products/$row[2]' "
."height=300px width =250px></td>";
echo "</tr>";
echo "</table>";
//print_r($row);
}
//4. Release the resources
mysqli_free_result($result);
//5. Close the connection
mysqli_close($dbconn);
?>
</form>
</body>
</html>

How can I pass php variable into ajax function

How can I pass id="'.$row["courseid"].'" into the ajax function,
I'm trying data:{'courseid':deleteId}, but is not working any ideas on how to fix this problem.
<?php
echo "<table width='100%'>";
echo "<tr>
<th>Course name</th>
<th>Delete</th>
<th>Edit</th>
</tr>";
?>
<?php foreach($rows as $row):
echo "<tr>";
echo '<td>' . htmlentities($row['coursename'], ENT_QUOTES, 'UTF-8') . '</td>';
echo '<td><button onclick="deleteC(' . $row['courseid'] . ')");"><font color="#e70404"> Delete </font> </button></td>';
echo '<td><a class="delete" id="'.$row["courseid"].'">Delette</a></td>';
echo "</tr> ";
endforeach;
echo "</table>";
?>
And this is the ajax function which is in the same page
<script type="text/javascript">
function deleteC(deleteId){
$.ajax({
type: "GET",
url: "deleteCourse.php",
data:{'courseid':deleteId},
success: function(result){
if(result=='correct'){
window.location='index.php';
}else {
window.location='coursesData.php';
}
}
});
}
</script>
This is the deleteCourse.php
<?php
require("connect.php");
if (isset($_GET['courseid']) && is_numeric($_GET['courseid']))
{
$id = $_GET['courseid'];
echo"$courseid";
$con=mysqli_connect("localhost","root","","independentstudyclass");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM courses WHERE courseid=$id");
echo "correct";
mysqli_close($con);
echo "correct";
}
else
{
header ("Location: ../index.php");
}
?>
Instead of this:
echo '<td><font color="#e70404"> Delete </font> </td>';
Just pass the id to the JS function as a parameter and use it:
PHP:
echo '<td><font color="#e70404"> Delete </font> </td>';
JS
function deleteC(deleteId){
$.ajax({
type: "GET",
url: "deleteCourse.php",
data:{'courseid':deleteId},
success: function(result){
if(result=='correct'){
window.location='index.php';
} else {
}
}
});
}

Update using checkbox using jquery

I'm trying to update my course_code into my database when the checkbox checked but it fail. I tried to modify from others but seem like I make mistake. can anyone tell me what is the problem?
here is my assigncourse.php
<?php require_once("../includes/session.php"); ?>
<?php require_once("sessioncourse.php"); ?>
<?php $course_codefac = $_SESSION['course_code'] ; ?>
<?php confirm_logged_in(); ?>
<?php require_once("../includes/connection.php") ?>
<?php require_once("../includes/functions.php") ?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function Change(id) {
$.ajax({
type: "GET",
url: "updateassigncourse.php",
data: {"id": id},
'success': function (response) {
console.log(response);
//TODO: use server response
}
});
};
</script>
<?php include("includes/header.php"); ?>
<div id="main">
<div class="full_w">
<?php
$querysel = "SELECT * FROM tblstudent ORDER BY student_id " ;
$resultsel = mysql_query($querysel, $connection);
echo "<h2><div class=\"h_title\">Please tick the student to join this
".$course_codefac." course</div></h2>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th scope=\"col\">Matric ID</th>";
echo "<th scope=\"col\">Name</th>";
echo "<th scope=\"col\">Assign</th>";
echo "</tr>";
echo "</thead>";
while($rowsel = mysql_fetch_array($resultsel)){
if($rowsel['course_code'] == NULL){
$id = $rowsel['id'];
echo "<tr>";
echo "<tr>"."<td class=\"align-center\">".$rowsel['student_id']."
</td>";
echo "<td class=\"align-center\">".$rowsel['name']."</td>";
echo "<td class=\"align-center\">";
echo "<input type=\"checkbox\" onchange=\"javascript:
Change($id);\">";
echo "</td>";
}
}
echo "</table>";
?>
</div>
</div>
<?php include("includes/footer.php"); ?>
then here is my updateassigncourse.php
<?php require_once("../includes/session.php"); ?>
<?php require_once("sessioncourse.php"); ?>
<?php $course_codeapp = $_SESSION['course_code'] ; ?>
<?php confirm_logged_in(); ?>
<?php require_once("../includes/connection.php") ?>
<?php require_once("../includes/functions.php") ?>
<?php
$id = $_GET['id'];
$course = $course_codeapp;
$sql="UPDATE tblstudent set course_code = ". mysql_real_escape_string($course)
." WHERE id = " .mysql_real_escape_string($id);
$result = mysql_query($sql);
?>
If this is your exact code, then there is a missing ';'
$course = $course_codeapp
it should have been
$course = $course_codeapp;

Set approval for admin with checkbox using php,jquery and ajax

I am working in a project where i want to show only those products to user which are selected by admin from database. Actualy i want to set the approval 1 or 0 in database when admin check or unchecked that checkbox.
jobs.php
$(document).ready(function(){
$('input.check').click(function(){
if($("input.check").is(':checked'))
{
$id = $(this).attr("id");
$.post("handle.php",{action:"checked",id:$id},function(data){
alert("Peoduct is set to display...");
});
}
else
{
alert("unchecked");
$id = $(this).attr("id");
$.post("handle.php",{action:"unchecked",id:$id},function(data){
alert("Peoduct is un-set to display...");
});
}
});
});
<?php
$dbqry = mysql_query("select * from job_category");
echo "<table width='50%', border='2'>
<tr>
<th>Catergory ID</th>
<th>Category Name</th>
<th>Remove</th>
<th>Update</th>
<th>Approval</th>
</tr>";
if($dbqry)
{
while($row = mysql_fetch_array($dbqry))
{
echo "<tr>";
echo "<td align='center'>" . $row['c_id'] . "</td>";
echo "<td align='center'>" . $row['cat_name'] ."</td>";
$cid = $row['c_id'];
$aprv = $row['approval'];
echo "<td align='center'><a href='remove.php?action=cat_remove&cid=$cid'>Remove</a></td>";
echo "<td align='center'>
<a href='Update-form.php?action=cat_update&cid=$cid'>Update</a></td>";
?>
<td align="center">
<input type='checkbox' name='approval' value='approval' id ="<? echo $cid; ?>" class="check"/>
</td>
</tr>
<?
}
echo "</table>";
echo '<br/>';
echo "<a href='add.php?action=cat_add'>Add New Category</a>";
}
else
{
die(mysql_error());
}
?>`
handle.php
`<?php
include 'include/connection.php';
$action = $_POST['action'];
$id = $_POST['id'];
//echo $action;
if($action == "checked")
{
$query = "update job_category set approval=1 where c_id=$id";
$result = mysql_query($query);
if(!$result)
{
echo die(mysql_error());
}
}
else if($action == "unchecked")
{
$query = "update job_category set approval=0 where c_id=$id";
$result = mysql_query($query);
if(!$result)
{
echo die(mysql_error());
}
}
?>`
Its working but when i refresh the page or seletc the URL and press enter then all the checked data appears unchecked even after that it does not change value of approval from 1 to 0 in database, but still it make me confuse about which items are checked or unchecked. Any suggestion will be appreciated.
modify checkbox line to show checked if approval=1 in database... try this
<input type='checkbox' name='approval' value='approval' <?php if($row["approval"]==1){ echo "checked=\"checked\"";}) ?> id ="<? echo $cid; ?>" class="check"/>
html
<input type='checkbox' name='approval' value='approval' <?php echo $row["approval"]?"checked=\"checked\"":'';?> id ="<? echo $cid; ?>" class="check"/>
then add js
$(function(){
$('input[type=checkbox]').each(function(){
if($(this).attr('checked') && $(this).attr('checked')=='checked'){
$(this).attr('checked', true);
}else{
$(this).attr('checked', false);
}
})
})
and
if($("input.check").is(':checked'))
to
if($(this).is(':checked'))

Categories