1 回答

TA貢獻1810條經驗 獲得超4個贊
在以下代碼行之后:
header("Location:?posts.php?source=edit_posts&p_id=$post_id");
用戶將被重定向到新頁面,并且不會看到在 header 指令之后執行的代碼。要顯示消息,您必須將消息作為 GET 或 POST 參數提交。而第一個選項將更容易。
您的代碼對SQL 注入是完全開放的,應該使用參數化的準備語句。您可以使用PDO或MySQLi。我使用 PDO 構建解決方案,但取決于您。
因此,您可以按如下方式調整腳本:
<?php
try{
? ? //Create new PDO Object
? ? $conn = new PDO("mysql:host=HOST;port=PORT;dbname=DBNAME", USERNAME, PASSWORD);
? ? //Define query
? ? $query = $this->conn->prepare("UPDATE posts SET post_image = :postimage WHERE?
? ? post_id = :postid AND LENGTH(post_image) > 0 AND post_image <> :postimage");
? ? $query->bindValue("postimage", $post_image);
? ? $query->bindValue("postid", $post_id);
? ? $query->execute();
? ? //Redirect user and add success message as GET parameter
? ? header("Location: posts.php?source=edit_posts&p_id=$post_id&update=success");
? ? //Make sure script is terminated
? ? exit();
}? catch(Exception $ex){
? ?//Log error
? ?error_log($ex->getMessage());
? ?//Show user custom error message
? ?echo "Something went wrong";
? ?//Make sure script is terminated
? ?exit();
}
?>
在目標頁面 (posts.php) 上,您可以插入如下代碼片段:
<?php?
if(isset($_GET['update']) && $_GET['update'] == "success"){
? ? echo "<p class='alert alert-success'><strong>Post Updated!</strong> <a href='../post.php?p_id=$post_id' class='alert-link' target='blank'>View Post</a><a href='' class='close' data-dismiss='alert'>x</a></p>";
}
?>
- 1 回答
- 0 關注
- 117 瀏覽
添加回答
舉報