Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
MANAGING MULTIPLE DATABASE TABLES } else { $sql = 'UPDATE blog SET image_id = NULL, title = ?, article = ? WHERE article_id = ?'; $stmt-prepare($sql); $stmt-bind_param('ssi', $_POST['title'], $_POST['article'], $_POST['article_id']); } $stmt-execute(); $done = $stmt-affected_rows; } The last two lines of this code block execute the prepared statement that updates the record in the blog table, and then assign the number of affected rows to $done. If you update a record, the affected_rows property is 1, which is treated as true. However, if you don t make any changes to the record, affected_rows is 0, which is treated as false. If. | MANAGING MULTIPLE DATABASE TABLES else sql UPDATE blog SET image_id NULL title article WHERE article_id stmt- prepare sql stmt- bind_param ssi _POST title _POST article _POST article_id stmt- execute done stmt- affected_rows The last two lines of this code block execute the prepared statement that updates the record in the blog table and then assign the number of affected rows to done. If you update a record the affected_rows property is 1 which is treated as true. However if you don t make any changes to the record affected_rows is 0 which is treated as false. If you update only the categories associated with a record without changing the record itself done is interpreted as false and you won t be returned to blog_list_mysqli.php. Delete the following line done stmt- affected_rows 6. Assign the return value of stmt- execute to done like this done stmt- execute The execute method returns true if the prepared statement is executed successfully even if it doesn t result in any changes to the record. 7. Immediately after the line you have just edited insert the code to delete existing values in the cross reference table and to insert the newly selected values like this done stmt- execute delete existing values in the cross-reference table sql DELETE FROM article2cat WHERE article_id stmt- prepare sql stmt- bind_param i _POST article_id stmt- execute insert the new values in articles2cat if isset _POST category is_numeric _POST article_id article_id int _POST article_id foreach _POST category as cat_id values article id . int cat id . if values sql INSERT INTO article2cat article_id cat_id VALUES . implode values if conn- query sql catError conn- error 451 CHAPTER 16 Download from Wow eBook www.wowebook.com This code needs little explanation. The DELETE query removes all entries in the cross-reference table that match article_id. The remaining code inserts the values selected in the update form. It s identical to the code in step 4 of PHP Solution 16-4. The key thing .