tailieunhanh - Giải pháp thiết kế web động với PHP - p 40

MANAGING CONTENT $stmt-fetch(); } } // if form has been submitted, update record if (isset($_POST ['update'])) { // prepare update query $sql = 'UPDATE blog SET title = ?, article = ? WHERE article_id = ?'; if ($stmt-prepare($sql)) { $stmt-bind_param('ssi', $_POST['title'], $_POST['article'], ➥ $_POST['article_id']); $done = $stmt-execute(); } } // redirect page on success or if $_GET['article_id']) not defined if ($done || !isset($_GET['article_id'])) { The UPDATE query is prepared with question mark placeholders where values are to be supplied from variables. The prepared statement has already been initialized in the code outside the conditional statement, so you can pass the SQL. | MANAGING CONTENT stmt- fetch if form has been submitted update record if isset _POST update prepare update query sql UPDATE blog SET title article WHERE article_id if stmt- prepare sql stmt- bind_param ssi _POST title _POST article _POST article_id done stmt- execute redirect page on success or if _GET article_id not defined if done isset _GET article_id The UPDATE query is prepared with question mark placeholders where values are to be supplied from variables. The prepared statement has already been initialized in the code outside the conditional statement so you can pass the SQL to the prepare method and bind the variables with stmt- bind_param . The first two variables are strings and the third is an integer so the first argument is ssi . If the UPDATE query succeeds the execute method returns true resetting the value of done. Unlike an INSERT query using the affected_rows property has little meaning because it returns zero if the user decides to click the Update Entry button without making any changes so we won t use it here. You need to add done to the condition in the redirect script. This ensures that the page is redirected either if the update succeeds or if someone tries to access the page directly. 10. Save and test it by loading selecting one of the EDIT links and making changes to the record that is displayed. When you click Update Entry you should be taken back to . You can verify that your changes were made by clicking the same EDIT link again. Check your code if necessary with . PHP Solution 13-5 Updating a record with PDO This PHP solution shows how to load an existing record into the update form and then send the edited details to the database for updating using PDO. To load the record you need to have created the management page that lists all records as described in PHP Solution 13-3. 1. Copy from the ch13 folder and save it in the .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.