我要删除我的数据库中的特定行,但是当我删除按钮时它会告诉我在第54行的deletebookajax.php中显示未定义的内容。 那么请你告诉我哪里出错了...... 这是我的代码 book.php用于显示整本书的详细信息......
<?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>
答案 0 :(得分:0)
更改您的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']);
$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>