在按钮上执行MySQL删除单击

时间:2016-05-10 15:41:31

标签: php mysql database

在我的代码中,我在数据库中显示一个名为staff表格的表格,我希望在HTML表格的每一行中添加一个删除按钮,单击该表格时,它将删除与其关联的记录。< / p>

基于搜索其他解决方案,我的代码如下所示:

staff.php:

require_once('../connection.php');

          //delete row on button click
          if(isset($_GET["del"])){
                    $idc = $_GET["del"];
                    if($VisitorManagement->query("DELETE FROM staff WHERE id=$idc")){
                         header('Location: delete-thankyou.php');
                    } else { 
                        echo "Failed to delete staff member.";
                    }    
                }  

            $result = mysqli_query($VisitorManagement, "SELECT * FROM staff ORDER BY fullName");

            echo "<table id='staff'>
            <thead>
            <tr>
            <th>Name</th>
            <th>Email</th>
            <th></th>
            </tr>
            </thead>";



            while($row = mysqli_fetch_array($result))
            {   
                echo "<tbody>";
                echo "<tr>";
                echo "<td>" . $row['fullName'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
                echo "<td><a class='button alert' href='staff.php?del=".$row["idc"]."'>Delete</a></td>";
                echo "</tr>";
            }
         echo "</tbody>";   
         echo "</table>";

connection.php:

$hostname_VisitorManagement = "localhost";
$database_VisitorManagement = "visitor-management";
$username_VisitorManagement = "***";
$password_VisitorManagement = "***";
$VisitorManagement = mysqli_connect($hostname_VisitorManagement, $username_VisitorManagement, $password_VisitorManagement, $database_VisitorManagement);

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

date_default_timezone_set('America/New_York');

不幸的是,当我单击其中一个按钮时,它无法删除记录并回显staff.php中预定义的错误消息。我错过了一些让这个工作的东西吗?

1 个答案:

答案 0 :(得分:0)

我能够通过将idc的所有实例更改为id来修复它。

新代码是:

Select ci.Id, ci.Header, co.Header,* from Cities ci
inner join Locations l on l.id = ci.LocationId 
inner join Venues v on v.id = l.VenueId 
inner join Collections co on co.Id = v.CollectionId -- this is going to be a problem