1 回答
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
在以下代碼行之后:
header("Location:?posts.php?source=edit_posts&p_id=$post_id");用戶將被重定向到新頁面,并且不會(huì)看到在 header 指令之后執(zhí)行的代碼。要顯示消息,您必須將消息作為 GET 或 POST 參數(shù)提交。而第一個(gè)選項(xiàng)將更容易。
您的代碼對(duì)SQL 注入是完全開放的,應(yīng)該使用參數(shù)化的準(zhǔn)備語句。您可以使用PDO或MySQLi。我使用 PDO 構(gòu)建解決方案,但取決于您。
因此,您可以按如下方式調(diào)整腳本:
<?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();
}
?>
在目標(biāo)頁面 (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 關(guān)注
- 142 瀏覽
添加回答
舉報(bào)
