为什么我的第二个按钮(删除按钮)与我的第一个按钮具有相同的功能(添加按钮)

问题描述 投票:-1回答:1

我的网站是使用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'>";
}


}
?>
php mysql switch-statement case
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.