PHP PDO MySQL函数不编辑数据,查询工作

时间:2015-11-11 14:51:23

标签: php mysql pdo

我的代码存在问题,不编辑数据,查询工作

<?php
$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "acp";
try {
  $dbh = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
  $dbh -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


  //$news_id = isset($_POST['news_id']) ? $_POST['news_id'] : NULL;
  $news_id             = $_POST['news_id'];
  $news_title          = $_POST['news_title'];
  $news_content    = $_POST['news_content'];
  // query
  $sql = "UPDATE `news` SET `news_title`=?, `news_content`=? WHERE news_id=?";
  $sth = $dbh -> prepare($sql);
  $sth -> execute(array($news_title,$news_content,$news_id));

  header('Location: ../news_admin.php');
}
catch(PDOException $e) {
    echo $e->getMessage();
}
$dbh = null;

&GT;

这是演示: http://213.146.54.224/pizzeria/ap/index.php login / pass:admin / 123456 http://213.146.54.224/pizzeria/ap/news_admin.php

2 个答案:

答案 0 :(得分:0)

这里是HTML FORM:

<?php
	include_once '/config/dbconnect.php';
	if(!$user->is_loggedin()) {
		$user->redirect('index.php');
	}

	$user_id = $_SESSION['user_session'];
	$stmt = $DB_con->prepare("SELECT * FROM users WHERE user_id=:user_id");
	$stmt->execute(array(":user_id"=>$user_id));
	$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="pl_PL">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

	<title>edytuj artykul</title>

	<!-- Stylesheets -->
	<link href="css/style.css" rel="stylesheet">
	<link href="css/bootstrap.min.css" rel="stylesheet">

	<!-- Scripts -->
	<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
	<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
	<script src="js/bootstrap.min.js"></script>

</head>

<body>
  <!-- Nawigacja Start-->
  <nav class="navbar navbar-default">
    <div class="container">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="home.php">AdminCP</a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
			<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
          <li><a><i class="glyphicon glyphicon-user"></i> welcome : <b><?php print($userRow['user_name']); ?></b></a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="menu" aria-haspopup="dLabel"><i class="glyphicon glyphicon-list-alt"></i> Artykuły <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="news_admin.php"><i class="glyphicon glyphicon-pencil"></i> Zarządzaj Artykułami</a></li>
              <li><a href="add_news.php"><i class="glyphicon glyphicon-plus"></i> Dodaj Artykuł</a></li>
            </ul>
          </li>
          <li ><a href="#">Link <span class="sr-only">(current)</span></a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
          <li><a href="logout.php?logout=true"><i class="glyphicon glyphicon-log-out"></i> logout</a></li>
        </ul>
      </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
  </nav>
  <!-- Nawigacja End -->
	<div class="container-fluid">
		<ol class="breadcrumb">
		  <li><i class="glyphicon glyphicon-home"></i><a href="home.php"> &nbsp;Start</a></li>
			<li><a href="articles.php">Artykuły</a></li>
		  <li class="active">Edytuj Artykuł</li>
		</ol>
	</div>
  <!-- NEWS ADD Start -->
  <?php
      $id = isset($_GET['news_id']) ? $_GET['news_id'] : NULL;
      $sth = $DB_con -> prepare("SELECT `news_id`, `news_title`, `news_content` FROM `news` WHERE `news_id` = :id");
      $sth->bindParam(':id', $id, PDO::PARAM_INT);
      $sth->setFetchMode(PDO::FETCH_OBJ);
      $sth->execute();
    	$row = $sth->fetch();
  ?>
  <div class="jumbotron"><center>
    <h1>Edytuj artykuł!</h1>
    <form action="includes/edit.php" method="POST" class="form-horizontal col-md-12" role="form">
        <div class="form-group">
          <label class="control-label col-sm-2" for="name">Tytuł Artykułu:</label>
          <div class="col-sm-10">
						<textarea class="form-control" rows="1" id="news_title" name="news_title" ><?php echo $row->news_title; ?></textarea>
          </div>
        </div>
        <div class="form-group">
          <label class="control-label col-sm-2" for="comment">Tresc Artykułu:</label>
          <div class="col-sm-10">
              <textarea class="form-control" rows="5" id="news_content" name="news_content" ><?php echo $row->news_content; ?></textarea>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-7">
            <button type="submit" class="btn btn-default">Edytuj Artykuł</button>
          </div>
        </div>
    </form>
  </center>
  </div>
  <!-- NEWS ADD End -->

	<!-- FOOTER Start -->
	<nav class="navbar navbar-default navbar-fixed-bottom" role="navigation">
		<div class="container-fluid">
				<p class="text-muted">CMS stworzony przez <a href="https://www.facebook.com/jozwik.przemyslaw">Przemysław Jozwik <i class="glyphicon glyphicon-thumbs-up"></i></a>.</p>
		</div><!-- /.navbar-collapse -->
	</div>
	<!-- FOOTER End -->
</body>
</html>

答案 1 :(得分:0)

您没有在帖子中发送新闻ID值。在表单中添加隐藏的输入值,然后重试:

<input type="hidden" name="news_id" value="<?php echo $_GET['news_id'];?>">