我正在尝试在网页上显示表格。表从 mysql 表中获取内容。我的表格中每一行都有一个删除按钮。我希望每当我单击删除按钮时,该行都应该从数据库和网页中删除。
这是我的代码
<script>
function ajaxFunction(str){
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax-example.php?name="+str,true);
xmlhttp.send();
}
</script>
<?php
// Connect to the database
$dbLink = new mysqli('127.0.0.1', 'root', 'root', 'test');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo " <table width=100%>
<tr>
<td><b>Name</b></td>
<td><b>Mime</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
</tr>";
// Print each file
while($row = $result->fetch_assoc()){
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
<td><a href='get_file.php?id={$row['id']}'>Download</a></td>
<td><input type='checkbox' class='checkbox'></td>
<td><input type='button' class= 'button' value='delete' name='delete' onClick='ajaxFunction({$row['name']})'></td>
<td><input type='button' class='button' value='edit' name='edit'></td>
</tr>";
}
// Close table
echo " </table>";
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
echo "<div id='txtHint'>Yours</div>";
$dbLink->close();
?>
这是我的 ajax-example.php
<?php
// Connect to the database
$name=$_GET['name'];
$dbLink = new mysqli('127.0.0.1', 'root', 'root', 'test');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file` where `name`="$name"';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>Mime</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
<td><a href='get_file.php?id={$row['id']}'>Download</a></td>
<td><input type='checkbox' class='checkbox'><input type='button' style='display:none' class= 'button' value='delete' name='delete'><input type='button' style='display:none' class='button' value='edit' name='edit'></td>
</tr>";
}
// Close table
echo "</table>";
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
?>
该文件实际上是尝试打印我单击删除按钮所在的行。我试图先做到这一点。如果该行被选中,那么删除它不会有问题。但是网页只显示表格,按下删除按钮不会改变“你的”内容。我不知道该怎么办。我在哪里犯错?
/Note/ 此功能是功能的一部分。实际上,当我单击复选框时,那些删除按钮和编辑按钮应该是可见的。为此,我使用 javascript。但在这里我没有展示它。我所能找到的只是 ajaxFunction() 没有被调用。因为我尝试在其中使用 alert("hi") 并且单击删除按钮时网页上没有任何反应。
有什么帮助吗?
谢谢