尝试使用PHP中的删除按钮从表中删除整行

时间:2018-01-12 07:36:47

标签: php phpmyadmin

我试图使用函数从数据库中删除表中的整行, 我在函数中写删除查询。但删除按钮不起作用。怎么做呢

这是我的代码

for tgtObject in document current Module do
{

    Object o = tgtObject
    Object t
    Link l

    for l in o -> "*" do
    {
        t = target(l)
        t <- o
        delete l
    }
    flushDeletions
}

提前致谢。

2 个答案:

答案 0 :(得分:2)

为了说明上面的注释 - 函数不知道变量$ id是什么,因为它没有在函数中定义。所以你可以指定为参数。另外,我没有立即发现,delete函数同样要求将$conn变量定义为参数或函数本身的全局变量。

生成的HTML不正确 - 结束表行标记NEEDS在循环内,th部分应在表格行内。

function delete($conn,$id){
    $delete1 =("DELETE FROM `usrdata` WHERE id = '$id'");
    $result = mysqli_query($conn,$delete1) or die(mysqli_error());
    echo "record deleted";
}

或在函数

中声明为global
function delete(){
    global $id;
    global $conn;
    $delete1 =("DELETE FROM `usrdata` WHERE id = '$id'");
    $result = mysqli_query($conn,$delete1) or die(mysqli_error());
    echo "record deleted";
}

首先需要定义变量,使用ID或其他方式定义变量的隐藏字段。

更好的是将prepared statement与上述结合使用

function delete( $conn=false, $id=false ){
    if( $conn && $id ){
        $sql='delete from `usrdata` where id = ?';
        $stmt=$conn->prepare( $sql );
        if( $stmt ){
            $stmt->bind_param('i',$id);
            $result=$stmt->execute();
            $stmt->close();
            echo $result ? 'record deleted' : 'error';
        }
    }
}

作为一种可能的解决方案,匆匆写下这样的借口错误,你或许可以这样做:

该表格完全包含在一个表单(delrecord)中,其中包含一个隐藏字段id,该字段使用dataset属性data-id按钮通过某些javascript分配值

<?php

    include 'conn.php';
    $result = ("SELECT * FROM usrdata"); 
    $count=mysqli_query( $conn, $result );


    if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['id'] ) ){

        function delete( $conn=false, $id=false ){
            if( $conn && $id ){
                $sql='delete from `usrdata` where id = ?';
                $stmt=$conn->prepare( $sql );
                if( $stmt ){
                    $stmt->bind_param('i',$id);
                    $result=$stmt->execute();
                    $stmt->close();
                    echo $result ? 'record deleted' : 'error';
                }
            }
        }

        $id=filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );
        call_user_func( 'delete', $conn, $id );
    }


    echo'
    <form name=\'delrecord\' method=\'post\'>

        <input type=\'hidden\' id=\'h_id\' name=\'id\' />

        <table border=1px>
            <tr>
                <th>id</th>
                <th>email</th>
                <th>Password</th>
                <th>&nbsp;</th>
            </tr>';

    while( $data = mysqli_fetch_array( $count ) ) {
        echo"
            <tr>
                <td>{$data['id']}</td>
                <td>{$data['email']}</td>
                <td>{$data['password']}</td>
                <td><input data-id='{$data['id']}' type='button' name='delete' value='delete'></td>
            </tr>";
    }

    echo '
        </table>
    </form>

    <script>
        var col=Array.prototype.slice.call( document.querySelectorAll("input[type=\'button\'][name=\'delete\']") );
            col.forEach(function(bttn){
                bttn.onclick=function(event){
                    document.getElementById("h_id").value=this.dataset.id;
                    document.forms.delrecord.submit();
                }.bind(bttn)
            })
    </script>';
?>

答案 1 :(得分:1)

1.您需要有一个id为记录的表单并提交按钮以提交内容。

2.您需要将id以及db comnnection object传递给delete函数。

你必须这样做: -

<?php
    include 'conn.php';
    $result = ("SELECT * FROM usrdata"); 
    $count=mysqli_query($conn,$result);

    echo'<table border=1px>';
    echo'<tr><th>id</th><th>email</th><th>Password</th><th>Action</th></tr>'; 

    while($data = mysqli_fetch_array($count)){
        echo'<tr>';
        echo '<td>'.$data['id'].'</td><td>'.$data['email'].'</td>  <td>'.$data['password'].'</td>'; 
        echo '<td><form method="post"><input type="hidden" name = "id" value ="'.$data['id'].'"><input type="submit" name="delete" value="delete"></form></td>';//add form with id hidden field and submit button
        echo'</tr>';
    }


    echo '</table>';
?>
<?php
    if(isset($_POST['delete'])){
        delete($_POST['id']); //pass the id to function
    }

    function delete($id,$conn)
    { // function with id as a parameter
        $delete1 =("DELETE FROM `usrdata` WHERE id = '$id'");
        $result = mysqli_query($conn,$delete1) or die(mysqli_error($conn));

        echo "record deleted";

    }
?>

注意: - 您的代码是SQL INJECTION的开放式代码。防止使用prepared statements

<强> 参考: -

mysqli prepared statements

PDO prepared statements