2015-11-11 44 views
-1

我有问题与此代码,不修改数据,查询工作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; 

>

这里是演示: http://213.146.54.224/pizzeria/ap/index.php 登录/密码:管理/ 123456 http://213.146.54.224/pizzeria/ap/news_admin.php

+0

http://php.net/manual/en/function.error-reporting.php –

+1

你应该已经发布,是HTML表单有了这个。可能未定义的索引通知。 –

+0

@ Fred-ii-我添加了HTML表单 – phoenix

回答

0

你是不是送在您的文章的消息ID值。在表单中添加一个隐藏的输入值,这样,然后再试一次:

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

谢谢!它现在工作! – phoenix

0

这里是HTML表单:

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

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

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

 
\t <title>edytuj artykul</title> 
 

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

 
\t <!-- Scripts --> 
 
\t <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
 
\t <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 
 
\t <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 --> 
 
\t \t \t <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 --> 
 
\t <div class="container-fluid"> 
 
\t \t <ol class="breadcrumb"> 
 
\t \t <li><i class="glyphicon glyphicon-home"></i><a href="home.php"> &nbsp;Start</a></li> 
 
\t \t \t <li><a href="articles.php">Artykuły</a></li> 
 
\t \t <li class="active">Edytuj Artykuł</li> 
 
\t \t </ol> 
 
\t </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(); 
 
    \t $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"> 
 
\t \t \t \t \t \t <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 --> 
 

 
\t <!-- FOOTER Start --> 
 
\t <nav class="navbar navbar-default navbar-fixed-bottom" role="navigation"> 
 
\t \t <div class="container-fluid"> 
 
\t \t \t \t <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> 
 
\t \t </div><!-- /.navbar-collapse --> 
 
\t </div> 
 
\t <!-- FOOTER End --> 
 
</body> 
 
</html>