我在functions.php中创建了三个函数,一个在数据库中创建列,另一个在datatable中显示它们的函数都工作正常,但删除函数给我错误未定义索引:id
function show()
{
global $connection;
//select queries into database
$query = "SELECT * FROM employees";
//connect query with database
$result = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
$address = $row['address'];
$phone = $row['phone'];
echo "<tr>
<td>
<span class='custom-checkbox'>
<input type='checkbox' id='checkbox1' name='options[]' value='1'>
<label for='checkbox1'></label>
</span>
</td>
<td>$id</td>
<td>$name</td>
<td>$email</td>
<td>$address</td>
<td>$phone</td>
<td>
<a href='#editEmployeeModal' class='edit' data-toggle='modal'><i class='material-icons' data-toggle='tooltip' title='Edit'></i></a>
<a href='#deleteEmployeeModal' class='delete' data-toggle='modal'><i class='material-icons' data-toggle='tooltip' title='Delete'></i></a>
</td>
</tr>";
}
}
function create()
{
global $connection;
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$query = $query = "INSERT INTO employees(name, email, address, phone) VALUES ('$name', '$email', '$address', '$phone')";
$result = mysqli_query($connection, $query);
}
function delete(){
global $connection;
$id = $_POST['id'];
$query = "DELETE FROM employees ";
$query .= "WHERE id = $id ";
mysqli_query($connection,$query);
}
?>
在另一个index.php文件中调用所有这些函数
if (isset($_POST['submit'])) {
create();
}
if (isset($_POST['delete'])) {
delete();
echo "it works";
}
这是index.php文件中的模态删除
<div id="deleteEmployeeModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form method="post" action="index.php">
<div class="modal-header">
<h4 class="modal-title">Delete Employee</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete these Records?</p>
<p class="text-warning"><small>This action cannot be undone.</small></p>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<input type="submit" class="btn btn-danger" name="delete" name="delete" value="Delete">
</div>
</form>
</div>
</div>
</div>