我的网站是使用MVC(模型,视图,控制器)构建的,我的第二个按钮(被删除)与我的第一个按钮(添加)具有相同的功能。因此,它删除的只是添加数据。
这是我的control.php
if(isset($_GET['action']))
{switch($_GET['action']){
case 'del': delete_person_add();
case 'add': add_person(); break;
case 'find': find_person(); break;
case 'view': view_person(); break;
case 'delete': delete_person(); break;
case 'update': update_person(); break;
case 'medical': medical_person(); break;
default:
include "model/persons_model.php";
$persons = view_person_model();
include "admin.php";
}
function delete_person_add(){
include "delete.php";
$persons = delete_person_models();
include "add.php";
}
function add_person(){
include "model/persons_model.php";
$persons = add_person_model();
include "add.php";
}
这是我在addview.php中的按钮的代码
<button type="submit" name="add" class="btn btn-primary">Add
Applicant</button>
这也是我的视图add.php的代码,用于删除按钮,它位于表中
<tbody>
<?php
if(isset($persons)){
foreach ($persons as $p){
echo '
<tr>
<td>'.$p['id'].'</td>
<td>'.$p['firstname'].'</td>
<td>'.$p['middlename'].'</td>
<td>'.$p['lastname'].'</td>
<td>'.$p['age'].'</td>
<td>'.$p['contactno'].'</td>
<td>'.$p['agent'].'</td>
<td>'.$p['dateapplied'].'</td>
<td>'.$p['datemedical'].'</td>
<td>'.$p['foreignagency'].'</td>
<td>'. "<input type='submit' name='del'/>".'</td>
</tr>';
}
}
?>
</tbody>
最后这是我的模型delete.php代码。
<?php
function delete_person_models(){
//connect to the server
$conn= mysqli_connect("localhost","root","","people");
//check the connection
if(mysqli_connect_errno($conn)){
echo "Error";
}
else{
//echo "connect Ok";
}
if(isset($_GET['del']))
{
$id = $_GET['del'];
$sql1 = "DELETE FROM persons WHERE id='$id'";
$res = mysqli_query($sql1) or die ("Failed".mysqli_error());
echo "<meta http-equiv='refresh' content='0; url=persons.php'>";
}
}
?>
答案 0 :(得分:1)
您的按钮定义错误。我应该是:
<button type="submit" name="action" value="add" class="btn btn-primary">
Add Applicant
</button>
<button type="submit" name="action" value="del" class="btn btn-primary">
Delete Applicant
</button>