Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用表单中未包含的复选框和按钮从数据库中删除记录_Php_Html_Mysql - Fatal编程技术网

Php 如何使用表单中未包含的复选框和按钮从数据库中删除记录

Php 如何使用表单中未包含的复选框和按钮从数据库中删除记录,php,html,mysql,Php,Html,Mysql,我在product.php中有这样的代码: 编辑: 和按钮: <button class="btn-floating btn-large waves-effect waves-light red" type="submit" value="Delete" name="delete"> <a><i class="material-icons">clear</i></a> </b

我在product.php中有这样的代码: 编辑:

和按钮:

<button class="btn-floating btn-large waves-effect waves-light red" type="submit" value="Delete" name="delete">
                <a><i class="material-icons">clear</i></a>
            </button>
我使用readAll函数在index.php中的表中显示数据库的内容。在第一列中,我有一个复选框。页面上还有几个按钮,其中一个按钮用于从数据库中删除选定的记录。我可以用这样的形式:

<form action="" method="post">
<input type="text" name="id" id="id" placeholder="Please Enter Id"/><br /><br />
<input type="submit" value="Delete" name="delete" id="delete"/><br />

但是,我如何删除记录,而不以任何形式写入其id,只需使用product.php中的复选框和index.php中不在表单中的按钮?

使用ajax,您可以向服务器代码发送一组要删除的id。使用javascript绑定到DeleteButtons onClick事件,即纯jQuery

或者您也可以触发表单提交并将id设置为隐藏输入。并以非ajax的方式进行

使用name=checkbox[]和id=checkbox.$id

使用复选框[],您将获得多个选中的复选框,其id以逗号分隔

class Product {
private $conn;
private $id;
private $name;
private $description;
private $price;
private $category_id;
private $category_name;
private $created;


public function __construct($db) {
    $this->conn = $db;
}

public function readAll()
{
    $stmt = $this->conn->prepare('SELECT id, name, description, price, CategoryID, created FROM products');
    $stmt->execute();
    echo " <form action="./objects/product.php" method="post">
 <table class=\"highlight responsive-table\">
        <thead>
            <tr>
                <th data-field=\"empty\"> </th>
                <th data-field=\"name\">Name</th>
                <th data-field=\"description\">Description</th>
                <th data-field=\"price\">Price</th>
                <th data-field=\"category\">Category</th>
                <th data-field=\"action\">Action</th>
            </tr>
        </thead>";

    while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $id = $result['id'];
        $n = $result['name'];
        $d = $result['description'];
        $p = $result['price'];
        $ca = $result['CategoryID'];
        $c = $result['created'];

        echo "<tbody>
             <tr>
             <td style=\"width:10%;\">

                        <input type=\"checkbox\" id=\"checkbox_".$id."\" name=\"checkbox[]\" />
                        <label for=\"checkbox".$id."\"></label>

                </td>



                <td style=\"width:15%;\">" .$n. "</td>
                <td style=\"width:30%;\">" . $d. "</td>
                <td style=\"width:10%;\">" ."$".$p. "</td>
                <td style=\"width:15%;\">" . $ca. "</td>
                <td style=\"width:20%;\"> 
                    <a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">mode_edit</i></a>
                    <a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">delete</i></a>
                </td>";
    }
    echo " <input type="submit" value="Delete" name="delete" id="delete"/></form>";
    echo "</tbody> </table>";


}

public function deleteSelected($ids) { 
    $query = 'DELETE FROM products WHERE id=?';

    $stmt = $this->conn->prepare($query);

    if (is_array($ids)) {
        foreach ($ids as $id)
            $stmt->execute([$id]);
    }
    else {
        $stmt->execute([$ids]);
    }
}

使用java脚本提交表单要容易得多。这使您能够更好地控制提交哪些数据,并在发送之前对其进行操作。我正试图使用您所说的第二种方式来实现这一点,表单中没有的按钮是删除记录,但仍然只有在我将id写入form@artur47wien对于第二种方法,您需要在视图文件中编写表单。或者可能在运行时追加。ohhh仍然不工作!!让我知道你在哪里面临错误?我一定会帮助你的@Artur47Wien您是否在此处添加了您编辑的代码?如果没有,那么请添加它,以便我可以从那里检查和调试它@Artur47Wien首先你有没有检查过数据是否来了?请通过print\u r$\u POST进行检查,并将数组输出给我@Arturwieni知道你的问题。您只需要创建一个公共删除按钮,提交表单并重定向到product.php页面,以便在该页面上获得post数据。现在它是空的,因为您没有使用表单传递数据@Artur47Wien从代码中,单击“删除”后只能删除一条记录。
class Product {
private $conn;
private $id;
private $name;
private $description;
private $price;
private $category_id;
private $category_name;
private $created;


public function __construct($db) {
    $this->conn = $db;
}

public function readAll()
{
    $stmt = $this->conn->prepare('SELECT id, name, description, price, CategoryID, created FROM products');
    $stmt->execute();
    echo " <form action="./objects/product.php" method="post">
 <table class=\"highlight responsive-table\">
        <thead>
            <tr>
                <th data-field=\"empty\"> </th>
                <th data-field=\"name\">Name</th>
                <th data-field=\"description\">Description</th>
                <th data-field=\"price\">Price</th>
                <th data-field=\"category\">Category</th>
                <th data-field=\"action\">Action</th>
            </tr>
        </thead>";

    while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $id = $result['id'];
        $n = $result['name'];
        $d = $result['description'];
        $p = $result['price'];
        $ca = $result['CategoryID'];
        $c = $result['created'];

        echo "<tbody>
             <tr>
             <td style=\"width:10%;\">

                        <input type=\"checkbox\" id=\"checkbox_".$id."\" name=\"checkbox[]\" />
                        <label for=\"checkbox".$id."\"></label>

                </td>



                <td style=\"width:15%;\">" .$n. "</td>
                <td style=\"width:30%;\">" . $d. "</td>
                <td style=\"width:10%;\">" ."$".$p. "</td>
                <td style=\"width:15%;\">" . $ca. "</td>
                <td style=\"width:20%;\"> 
                    <a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">mode_edit</i></a>
                    <a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">delete</i></a>
                </td>";
    }
    echo " <input type="submit" value="Delete" name="delete" id="delete"/></form>";
    echo "</tbody> </table>";


}

public function deleteSelected($ids) { 
    $query = 'DELETE FROM products WHERE id=?';

    $stmt = $this->conn->prepare($query);

    if (is_array($ids)) {
        foreach ($ids as $id)
            $stmt->execute([$id]);
    }
    else {
        $stmt->execute([$ids]);
    }
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if ( isset( $_POST['delete']) && !empty( $_POST['checkbox']) ) {
        $checkboxArr = $_POST['checkbox'];
        foreach($checkboxArr as $id)
        {
            $cat = new Product($conn);
            //$id = filter_input ( INPUT_POST , 'id', FILTER_SANITIZE_NUMBER_INT );
            $cat->deleteSelected($id);
        }
    }
}