PHP& MySQL - 更新查询不更新数据库

时间:2016-11-21 11:49:05

标签: php mysql database web

我正在创建一个个人网站,我只是想让我更容易添加/编辑我的帖子,而无需手动进入phpmyadmin。

当我进入edit_post.php页面并按更新时,我在pid旁边得到一个“=”符号,例如。(foo.php?pid = 3),如果我让它重定向到博客页面,它就不会不要更新它。

博客页面

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

				$bbcode = new BBCode;

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

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

				$posts = "";

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

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

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

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

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

编辑页面

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

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

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

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

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

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

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

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

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

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

		mysqli_query($db, $sql);

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

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

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

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

3 个答案:

答案 0 :(得分:0)

您必须设置列并设置列的值。

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

答案 1 :(得分: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."'";

答案 2 :(得分:0)

好的,所以我想出来了,非常小的程序员失明但是嘿,我们都明白了。

第66行echo "<form action='edit_post.php?pid$pid' method='post' enctype='multipart/form-data'>";
在哪里说“?pid $ pid”我只需要删除我在那里的“=”。