如何更新表中显示的数据库中的记录

时间:2016-05-23 19:56:08

标签: php html mysql

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

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[]\" value=".$id." />
                        <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 modal-trigger\" href=\"#modal2\" id=\"edit_".$id."\" name=\"edit[]\"><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\"/>
    <input type=\"submit\" value=\"update\" name=\"update\" id=\"update\"/>
    </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]);
    }
}

public function update() {
    $sql2= "UPDATE `products` SET `Name` = :name, `Description` = :description, `Price` = :price, `CategoryID` = :catid WHERE `products`.`ID` = :id";
    $stmt = $this->conn->prepare($sql2);
    $stmt->bindParam(':name', $_POST['name'], PDO::PARAM_STR);
    $stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
    $stmt->bindParam(':price', $_POST['price'], PDO::PARAM_STR);
    $stmt->bindParam(':catid', $_POST['catid'], PDO::PARAM_INT);

    $stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
    $stmt->execute();
}

} 
if($_SERVER['REQUEST_METHOD'] == 'POST'){

if ( isset( $_POST['delete']) && !empty( $_POST['checkbox']) ) {
    $checkboxArr = $_POST['checkbox'];
    foreach($checkboxArr as $id)
    {
        $cat = new Product($conn);

        $cat->deleteSelected($id);
    }
}

if ( isset( $_POST['update']) && !empty( $_POST['edit']) ) {
    $editArr = $_POST['edit'];

        $cat = new Product($conn);

        $cat->update();

}

}

index.php中的更新按钮:

<button class="btn waves-effect waves-light" type="submit" name="update" style="float:left; margin: 5px;">
         <i class="material-icons left">mode_edit</i>Update
</button>

我正在使用readAll函数在index.php的表中显示数据库的内容。在第1列我有复选框。在页面上也有几个按钮,其中一个按钮应该通过使用“更新”功能打开对话窗口进行编辑记录。我试图通过删除数据库中的记录来做同样的事情,但它不起作用。那段代码有什么问题?

1 个答案:

答案 0 :(得分:1)

代码中唯一错误的是逻辑。从上一个foreach我可以推断用户有产品列表,检查一些复选框,然后点击update按钮,当弹出窗口显示更新产品时。我倾向于认为弹出窗口一次只包含一个产品的字段,而不是用户检查的所有产品。

您应该为每个产品添加一个更新按钮,这样当用户点击它时,他们就会知道哪些产品会更新。此外,当单击该按钮时,可以使用要更新的现有数据填充弹出窗口。

在我建议的方案中,您不需要最后foreach,因为您只会更新单个产品。

<强>结论 错误发生在HTML中,以及发送产品数据的方式,而不是显示的PHP代码。