使用表中的按钮从数据库中删除行(PHP)

时间:2016-08-06 05:20:12

标签: php mysql

我有这个问题,我有一个表显示数据的表,在这个表的每一行(动态生成)我有一个按钮用于从DB中删除行,所以我表中的行将是消失。但我不知道如何捕获数据库中的ID或值的名称。

这是我的PHP代码,直到现在:

$del = new USER();

$stmt = $find->runQuery("SELECT  course_id , course_name , course_description , course_begin_date , course_price  FROM courses");
$stmt->execute();

if(isset($_POST['delete_course'])) 
{
$cid = strip_tags($_POST['txt_id']);

try 
{
    if ($del->deleteCourse($cid)) {
            $del->redirect('courses.php');
        }   
} catch (Exception $e) {
    echo $e->getMessage();
}
}

这是我的HTML代码

<table class="table">
                <tr>
                    <th>ID</th>
                    <th>Nombre</th>
                    <th>Descripción</th>
                    <th>Fecha de inicio</th>
                    <th>Costo</th>
                    <th></th>
                </tr>

                <?php 
                foreach($stmt->FetchAll() as $results) {                        
                    echo "<tr>";
                    echo '<td name="txt_id">' . $results['course_id'] . '</td>';
                    echo '<td>' . $results['course_name'] . '</td>';
                    echo '<td>' . $results['course_description'] . '</td>';
                    echo '<td>' . $results['course_begin_date'] . '</td>';
                    echo '<td>' . $results['course_price'] . '</td>';
                    echo '<td class="del">
                          <button type="submit" name="delete_course" class="btn btn-danger">Borrar</button>
                          </td>';
                    echo '</form>';
                    echo "</tr>";

                }
                ?>
            </table>

我的deleteCourse方法

public function deleteCourse($cid){

    try {
        $stmt = $this->conn->prepare("DELETE FROM courses WHERE course_id=:cid ");

    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

你为什么要使用表格?你可以使用锚标记并将当前行的id传递给它,例如

    del = new USER();

    $stmt = $find->runQuery("SELECT  course_id , course_name , course_description , course_begin_date , course_price  FROM courses");
    $stmt->execute();

    if(isset($_GET['delete_course'])) 
    {


    try 
    {
        if ($del->deleteCourse($_GET['delete_course'])) {
                $del->redirect('courses.php');
            }   
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    }
?>

    <table class="table">
                    <tr>
                        <th>ID</th>
                        <th>Nombre</th>
                        <th>Descripción</th>
                        <th>Fecha de inicio</th>
                        <th>Costo</th>
                        <th></th>
                    </tr>

                    <?php 
                    foreach($stmt->FetchAll() as $results) { ?>                       
                       <tr>
                        <td><?php echo $results['course_id']; ?></td>
                        <td><?php echo $results['course_name']; ?> </td>
                       <td><?php echo $results['course_description']; ?></td>
                       <td><?php echo $results['course_begin_date'] ; ?></td>
                       <td><?php echo $results['course_price'] ; ?></td>
                        <td class="del">
                              <button type="button"  class="btn btn-danger" onclick="window.location.href = 'filename.php?delete_course=<?php echo $results['course_id']; ?>'" >Borrar</button>
                              </td>

                       </tr>
                <?php
}
                ?>
            </table>

<?php 
    public function deleteCourse($cid){

        try {
            $stmt = $this->conn->prepare("DELETE FROM courses WHERE course_id=$cid ");

        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }


Try like this. Let me know is it working or not.

答案 1 :(得分:-1)

/*-------------------------------------
COUNT SELECTED
--------------------------------------*/

function countSelected() {
    $(".player").on("click", function(){

        // Count the number of players with stars
        var starredGoaltenders = $(".player--goalie").find(".picked.is-active").length;
        var starredDefencemen = $(".player--defencemen").find(".picked.is-active").length;
        var starredForwards = $(".player--forward").find(".picked.is-active").length;

        console.log(starredGoaltenders, starredDefencemen, starredForwards);

        // The number of starred players for each position cannot exceed the following numbers
        var maxGoaltenders = 2;
        var maxDefencemen = 6;
        var maxFowards = 12;

        // If the number of starred players hits its max, the other unstarred players in that group cannot be clicked unless one is deselected first

        if (starredGoaltenders === maxGoaltenders) {
            $(".checkmark--goalie").addClass("is-completed");
            $(".player--goalie").find(".picked.is-inactive").css("pointer-events", "none");
        } else if (starredDefencemen === maxDefencemen) {
            $(".checkmark--defencemen").addClass("is-completed");
            $(".player--defencemen").find(".picked.is-inactive").css("pointer-events", "none");
        } else if (starredForwards === maxFowards) {
            // Not working unless done first?
            $(".checkmark--forward").addClass("is-completed");
            $(".player--forward").find(".picked.is-inactive").css("pointer-events", "none");
        }
    });
} countSelected();

答案 2 :(得分:-1)

如果你不想使用Ajax

,可以通过两种方式实现

1.GET方法

2.POST方法

你可以通过在url example -

中传递id来使用GET方法
echo '<a href="http://www.yourwebsite.com/yourfile.php?uid='.$results['course_id'].'"></a>';

然后你可以得到这个值。示例 -

if(isset($_GET['uid'])){
$courseid = $_GET['uid'];
}

或者您可以使用POST方法。然后您需要创建一个表单并在隐藏字段中发送值。 例子 -

<form method="post" action="">
    <input type="hidden" name="delete_course_id" value="<?php echo $results['course_id']; ?>">
 <button type="submit" name="delete_course" class="btn btn-danger">Borrar</button>
</form>

并且您将在

之类的表单后获得此ID
if(isset($_POST['delete_course'])) 
{
$courseid = strip_tags($_POST['delete_course_id']);
}