使用表单中的PHP按钮根据变量ID从MYSQL数据库表中删除一行


Delete a row from MYSQL database table based on variable ID using PHP button in FORM

我在一个网站上工作,您可以在其中为视频添加标签。 此屏幕截图应概述结果应如下所示:

http://www.unistamp.ch/images/images.html

嵌入了一个带有评论的视频,右侧有一个 Iframe,其中可以从 MYSQL 数据库访问时间轴上评论的详细信息。这是video_comment表的屏幕截图。

我想根据他的评论ID从数据库中删除特定的评论。我尝试了这段代码,但通过单击 X 按钮传递注释 ID 不起作用:

<?php
include '../../../DB/MySql/connectVideoPhase.php';
//****** Delete Row in table
if(isset($_POST['X']))
{
    $comment_id = $_POST['comment_id'];
    //CHeck --> doesnt work
    echo "<pre> commentid= ".$comment_id . "</pre> ";
    $sql = "DELETE video_converted_comment ".
               "WHERE commentid = '"".$comment_id."'" ;";
    $retval = mysql_query( $sql);
    if(! $retval )
    {   
        die('Could not delete data: ' . mysql_error()); }
        echo "Deleted data successfully'n";
    }
    //**************************************
    //******* Get the comments  
    $video_id = $_REQUEST['VideoId'];
    // for testing use a specific video ID
    if (! $video_id) {
        $video_id = '153fb143';
    }
    $sqlSelectComment = "Select * from video_converted_comment WHERE videoid ='".$video_id."'";
    $sqlComments = mysql_query($sqlSelectComment);
    $i = mysql_num_rows($sqlSelectComment);
    // *************************
    //********* Fill Table
    echo "
<table width='"100'" border='"1'" >
<tr>
<td width='"25'"><b>start</b></td>
<td width='"25'"><b>end</b></td>
<td width='"75'"><b>Text</b></td>
<td width='"5'"><b>X</b></td>
<td width='"0'"><input type='"hidden'"></td>
</tr>"; 
    while ($comments = mysql_fetch_array($sqlComments))
    {
        echo "<tr><td>" .$comments['starttime'] ."</td>";
        echo "<td>" .$comments['endtime'] ."</td>";
        echo "<td>" .$comments['text'] ."</td>";
        echo "<td> <form method='"post'" action='"".$_PHP_SELF ."'"> 
        <input name='"comment_id'" type='"hidden'" value='"".$comments['id']."'">  
        <button name='"X'"  type='"submit'" value='"".$comments['id']."'">  </td>
        </form>";
    }
echo "</table> ";

任何关于使用 PHP 基于注释 ID 删除/向数据库添加注释的帮助将不胜感激!使用javascript会更容易吗?视频下方时间轴的可视化是用javascript完成的。

在玩了一段时间之后,代码现在可以工作了。

轻舔表中的 delte 按钮后,脚本将删除 MySQL 表中的行。

错误 1 是:整个表显示在 Iframe 中。 当它包含在网站上时

<iframe id='"commentTable'" src='"DB/MySql/CommentTableAndDelete.php?VideoId=".$ID."..

Id 在 url 中传递并由

                    $video_id = $_REQUEST['VideoId'];

当脚本在删除条目后加载自身时,这不起作用,因为表单的方法是"post"。

这可能不是最好的方法,但它有效:

        $video_id = $_REQUEST['VideoId'];
                // if script loads itself
                if (! $video_id) {
                $video_id = $_POST['VideoId'];
                }

错误 2 是形式。不知何故,由于拼写错误,它没有传递变量这有效:

<form method='"post'" action='"".$_PHP_SELF ."'"> <input id='"comment_id'" name='"comment_id'" type='"hidden'" value='"".$comments['commentid']."'">
<button name='"X'" type='"submit'" value='"X'">

错误 nr. 3 SQL 语句不正确。在将语句粘贴到 php 代码之前,我在 phpmyadmin 中对其进行了测试。

$sql = "DELETE FROM video_converted_comment WHERE commentid='".$comment_id."' ;";

工作代码:

        //******* Get the comments  
                    $video_id = $_REQUEST['VideoId'];
                    if (! $video_id) {
                    $video_id = $_POST['VideoId'];
                    }

                    $sqlSelectComment = "Select * from video_converted_comment WHERE videoid ='".$video_id."';";
                    $sqlComments = mysql_query($sqlSelectComment);
                    if(! $sqlComments )
                    {   die('Could not get Comments from DB: ' . mysql_error()); }
                    $i = mysql_num_rows($sqlSelectComment);
    // *************************
    //****** Delete Row in table
    //http://stackoverflow.com/questions/15843678/mssql-and-php-select-specific-row-based-on-which-button-is-clicked
        if(isset($_POST['X']))
        {
        $comment_id = $_POST['comment_id'];
        //CHeck
        //echo "<pre> commentid= ".$comment_id . "</pre> ";
        $sql = "DELETE FROM video_converted_comment WHERE commentid='".$comment_id."' ;";
        $retval = mysql_query( $sql);

        if(! $retval )
        {   die('Could not delete data: ' . mysql_error()); }
        echo "Deleted data successfully'n";
        }
        //**************************************
//********* Fill Table
echo "
<table width='"100'" border='"1'" >
<tr>
<td width='"25'"><p>start</p></td>
<td width='"25'"><p>end</p></td>
<td width='"100'"><p>Text</p></td>
<td width='"10'"><p>X</p></td>
</tr>"; 
    while ($comments = mysql_fetch_array($sqlComments))
        {
        echo "<tr><td>" . gmdate("i:s", $comments['starttime']) ."</td>";
        echo "<td>" . gmdate("i:s", $comments['endtime']) ."</td>";
        echo "<td>" .$comments['text']."</td>";
        echo "<td> <form method='"post'" action='"".$_PHP_SELF ."'"> 
                <input id='"comment_id'" name='"comment_id'" type='"hidden'" value='"".$comments['commentid']."'">  
                <button name='"X'"  type='"submit'" value='"X'">  </td>
                </form>";
        }
echo "</table> ";
?>