2016-11-21 148 views
0

我正在製作一個個人網站,我只是想讓它更容易地添加/編輯我的帖子,而無需手動進入phpmyadmin。PHP和MySQL - 更新查詢不更新數據庫

當我進入edit_post.php頁面並按更新時,我在pid旁邊得到一個「=」符號,例如。(foo.php?pid = 3),如果我讓它重定向到博客頁面,不更新它。

博客頁面

<?php 
 
\t session_start(); 
 
\t include_once("../IncBlog/db.php"); 
 
?> 
 
<?php include "../Includes/navHead.php"; ?> 
 
\t \t <title>Adam Brickhill - Life Journal </title> 
 
\t </head> 
 
\t <body> 
 
\t \t <div class="box"> 
 
\t \t \t <div class="header"> 
 
\t \t \t \t <nav class="nav"><p class="title"><a class="postLink" href="../IncBlog/post.php">- Lone Tree -</a></p></nav> 
 
\t \t \t </div> 
 
\t \t \t <!-- JOURNAL !--> 
 
\t \t \t <?php 
 
\t \t \t \t require_once("../nbbc/nbbc.php"); 
 

 
\t \t \t \t $bbcode = new BBCode; 
 

 
\t \t \t \t $sql = "SELECT * FROM posts ORDER BY id DESC"; 
 

 
\t \t \t \t $res = mysqli_query($db, $sql) or die(mysqli_error()); 
 

 
\t \t \t \t $posts = ""; 
 

 
\t \t \t \t if (mysqli_num_rows($res) > 0) { 
 
\t \t \t \t \t while ($row = mysqli_fetch_assoc($res)) { 
 
\t \t \t \t \t \t $id = \t \t $row['id']; 
 
\t \t \t \t \t \t $title = \t $row['title']; 
 
\t \t \t \t \t \t $img = \t \t $row['img']; 
 
\t \t \t \t \t \t $content = \t $row['content']; 
 
\t \t \t \t \t \t $date = \t $row['date']; 
 

 
\t \t \t \t \t \t $admin = "<div><a href='../IncBlog/del_post.php?pid$id'>Delete</a>&nbsp;<div><a href='../IncBlog/edit_post.php?pid$id'>Edit</a>"; 
 

 
\t \t \t \t \t \t $output = $bbcode->Parse($content); 
 

 
\t \t \t \t \t \t ob_start(); 
 
\t \t \t \t \t \t \t include('../IncBlog/blogSkel.php'); 
 
\t \t \t \t \t \t \t $posts .= ob_get_contents(); 
 
\t \t \t \t \t \t ob_end_clean(); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t echo $posts; 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t echo "There are no posts to display"; 
 
\t \t \t \t } 
 
\t \t \t ?> 
 
\t \t \t <div class="journal"> 
 
\t \t \t \t <div class="catagories"> 
 
\t \t \t \t 
 
\t \t \t \t </div> 
 
\t \t \t \t <!-- Date !--> 
 
\t \t \t \t <!-- Picture !--> 
 
\t \t \t \t <!-- Description !--> 
 
\t \t \t </div> 
 
\t \t \t 
 

 
\t \t \t <?php include "../Includes/navFooter.php"; ?> 
 
\t \t \t <!-- SCRIPTS !--> 
 
\t \t \t <?php include "../Includes/navScriptImport.php"; ?> 
 
\t \t </div> 
 
\t </body> 
 
</html>

編輯頁面

<?php 
 
\t session_start(); 
 
\t include_once("db.php"); 
 

 
\t if (!isset($_SESSION['username'])) { 
 
\t \t header("Location: login.php"); 
 
\t \t return; 
 
\t } 
 

 
\t $pid = $_SERVER['REQUEST_URI']; 
 
\t $pid = trim($pid, "/IncBlog/edit_post.php?pid"); 
 

 
\t $pid = strip_tags($pid); 
 
\t $pid = stripslashes($pid); 
 
\t $pid = mysqli_real_escape_string($db, $pid); 
 
\t //echo "$pid"; 
 

 
\t if ($pid == "") { 
 
\t \t header("Location: ../Nav/life.php"); 
 
\t } 
 

 
\t if (isset($_POST['update'])) { 
 
\t \t $title = strip_tags($_POST['title']); 
 
\t \t $content = strip_tags($_POST['content']); 
 
\t \t $img = strip_tags($_POST['image']); 
 

 
\t \t $title = mysqli_real_escape_string($db, $title); 
 
\t \t $content = mysqli_real_escape_string($db, $content); 
 
\t \t $img = mysqli_real_escape_string($db, $img); 
 

 
\t \t $date = date("l jS \of F Y h:i:s A"); 
 

 
\t \t $sql = "UPDATE posts SET title='$title', content='$content', img='$img', date='$date' WHERE id=$pid"; 
 

 
\t \t if ($title == "" || $content == "") { 
 
\t \t \t echo "The database is hungry you can't feed it nothing!"; 
 
\t \t \t return; 
 
\t \t } 
 

 
\t \t mysqli_query($db, $sql); 
 

 
\t \t header("Location: ../Nav/life.php"); 
 
\t } 
 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>Blog - Post</title> 
 
</head> 
 
<body> 
 
\t \t <?php 
 
\t \t \t $sql_get = "SELECT * FROM posts WHERE id=$pid LIMIT 1"; 
 
\t \t \t $res = mysqli_query($db, $sql_get); 
 

 
\t \t \t if (mysqli_num_rows($res) > 0) { 
 
\t \t \t \t while ($row = mysqli_fetch_assoc($res)) { 
 
\t \t \t \t \t $title = $row['title']; 
 
\t \t \t \t \t $content = $row['content']; 
 
\t \t \t \t \t $img = $row['image']; 
 

 
\t \t \t \t \t echo "<form action='edit_post.php?pid=$pid' method='post' enctype='multipart/form-data'>"; 
 
\t \t \t \t \t echo " \t <input placeholder='Title' type='text' name='title' value='$title' autofocus size='48'><br /><br />"; 
 
\t \t \t \t \t echo " \t <input placeholder='Image' type='text' name='image' value='$img' autofocus size='48'><br /><br />"; 
 
\t \t \t \t \t echo " \t <textarea placeholder='Content' name='content' rows='40' cols='40'>$content</textarea><br />"; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t ?> 
 

 
\t \t <input type="submit" name="update" value="Update"> 
 
\t </form> 
 
</body> 
 
</html>

+0

你需要通過php和查詢來檢查錯誤,你沒有這樣做。和var_dump的ID和其他變量 –

+0

我已經有了,ID是1,PID是1點擊更新,什麼也沒有發生。 –

+0

我在編輯中刪除了標題中的RESOLVED。如果下面的答案解決了它,然後接受一個答案。如果沒有人沒有,那麼你可以發佈你自己的答案。接受答案會自動告訴系統問題已經解決;您不需要採取進一步的行動。 –

回答

0

您必須設置列並設置列的值。

UPDATE posts SET (title, content, img, date) VALUES ($title, $content, $img, $date) WHERE id = $pid; 
+0

這不起作用或者不幸 –

0
$date = date("l jS \of F Y h:i:s A"); 

使用的,而不是上面一行

$date = date('Y-m-d H:i:s', strtotime()); 

$sql = 'UPDATE posts SET title='".$title."', content='".$content."', img='".$img."', date='".$date."' WHERE id='".$pid."'"; 
+0

不,也不工作,兩個日期都有效。這不是我的問題,問題是當我點擊更新來更新帖子時,它不會應用更改。 –

0

好了,所以我想通了,很小的程序員失明,但嘿,我們都明白這一點。

66行echo "<form action='edit_post.php?pid$pid' method='post' enctype='multipart/form-data'>";
它說「?pid $ pid」我只需要刪除我在那裏的「=」。