2017-03-31 127 views
-1

我爲一個新聞社門戶做一個author_create_post.php並將數據條目插入到mysql數據庫中。 PHP腳本和表單依賴於相同的頁面。表單也使用了驗證規則。當我點擊提交後最終填寫所有表單條目後,我沒有任何錯誤,但是當我檢查我的數據庫時沒有插入條目,問題是什麼?如何解決它?這是我的完整代碼...爲什麼數據沒有插入到mysql數據庫中沒有錯誤出現在屏幕上?

<?php 
include "../tools/db.php"; 
include "../tools/functions.php"; 
include "../tools/jdf.php"; 
FetchPage("check_permission.php"); 
Permission("author"); 
if(isset($_POST["save"])){ 
    if($_POST["title"]!="" && $_POST["text"]!="" && $_POST["category"]!="" && $_POST["slider"]!="" && $_POST["important"]!=""){ 
     $title = ($_POST["title"]); 
     $text = addslashes($_POST["text"]); 
     $date = jdate('c'); 
     $time = time(); 
     $author_id = $_SESSION["user_id"]; 
     $cat_id=($_POST["category"]); 
     $slider=($_POST["slider"]); 
     $important=($_POST["important"]); 

      switch($cat_id){ 
       case 0: 
        $cat_id = 0; 
        break; 
       case 1: 
        $cat_id = 1; 
        break; 
       case 2: 
        $cat_id = 2; 
        break; 
       case 3: 
        $cat_id = 3; 
        break; 
       default : 
        $cat_id = 4;  
      } 
      $s = "SELECT `title` FROM `$tbl_posts` WHERE `title`=:title"; 
      $res = $connect->prepare($s); 
      $res->bindParam(":title",$title); 
      $res->execute(); 
      if($res->rowCount()>=1){ 
       $exist ="عنوان پست تکراری می باشد!"; 
     }else{ 
      $r = $connect->query("SELECT * FROM `$tbl_posts` WHERE `important`=1"); 
      if($r->rowCount()>=5){ 
       $imp_count = "درحال حاضر بخش اخبار مهم تکمیل میباشد حداکثر ظرفیت این بخش 4 خبر است!"; 
      }else{ 
      $sql = "INSERT INTO `$tbl_posts` (`author_id`, `title`, `category_id`, `time`, `text`, `date`, `slider`, `important`) VALUES (:author_id, :title, :category_id, :time, :text, :date, :slider, :important)"; 
      $result = $connect->prepare($sql); 
      $result->execute(array(':author_id'=>$author_id, ':title'=>$title, ':category_id'=>$cat_id, ':time'=>$time, ':text'=>$text, ':date'=>$date, ':slider'=>$slider, ':important'=>$important)); 
      if(isset($result)){ 
       $success =" پست ".$title." در سایت منتشر شد ✓ "; 
      }else{ 
       echo "<script>alert('Wrong!');</script>"; 
      } 
     } 
    } 
    }else{ 
     if($_POST["title"]=="") $titleerr = "عنوان مطلب را درج نمایید!"; 
     if($_POST["text"]=="") $texterr = "متن پست خالی میباشد!"; 

    } 
} 
?> 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta name="description" content=""> 
    <meta name="author" content="mahdi"> 
    <script type="text/javascript" src="http://dl.20script.ir/img/website.js"></script> 
    <meta name="keyword" content="FlatLab, Dashboard, Bootstrap, Admin, Template, Theme, Responsive, Fluid, Retina"> 
    <link rel="shortcut icon" href="img/favicon.html"> 

    <title>ارسال مطلب جدید</title> 

    <!-- Bootstrap core CSS --> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 
    <link href="css/bootstrap-reset.css" rel="stylesheet"> 
    <!--external css--> 
    <link href="assets/font-awesome/css/font-awesome.css" rel="stylesheet" /> 
    <link href="assets/jquery-easy-pie-chart/jquery.easy-pie-chart.css" rel="stylesheet" type="text/css" media="screen"/> 
    <link rel="stylesheet" href="css/owl.carousel.css" type="text/css"> 
    <!-- Custom styles for this template --> 
    <link href="css/style.css" rel="stylesheet"> 
    <link href="css/style-responsive.css" rel="stylesheet" /> 

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 tooltipss and media queries --> 
    <!--[if lt IE 9]> 
     <script src="js/html5shiv.js"></script> 
     <script src="js/respond.min.js"></script> 
    <![endif]--> 
    <script type="text/javascript" src="editor/nicEdit.js"></script> 
    <script type="text/javascript">bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });</script> 
    </head> 

    <body> 

    <section id="container" class=""> 
     <!--header start--> 
     <?php FetchPage("top.php"); ?> 
     <!--header end--> 

     <!--sidebar start--> 
     <aside> 
     <?php FetchPage("right.php"); ?> 
     </aside> 
     <!--sidebar end--> 
     <!--main content start--> 
     <section id="main-content"> 
      <section class="wrapper"> 
       <!-- page start--> 
       <div class="row"> 
        <aside class="profile-info col-lg-9"> 
         <?php 
          if(isset($success) && $success!=""){ 
         ?> 
         <div class="alert alert-success fade in"> 
          <button data-dismiss="alert" class="close close-sm" type="button"> 
           <i class="icon-remove"></i> 
          </button> 
          <center><strong>تبریک ! </strong><?=$success;?></center> 
         </div> 
         <?php 
         } 
         ?> 

         <?php 
          if(isset($titleerr) && $titleerr!=""){ 
         ?> 
          <div class="alert alert-block alert-danger fade in"> 
           <button data-dismiss="alert" class="close close-sm" type="button"> 
            <i class="icon-remove"></i> 
           </button> 
           <center><strong>اخطار ! </strong> <?=$titleerr;?></center> 
          </div> 
         <?php 
         } 
         ?> 

         <?php 
          if(isset($imp_count) && $imp_count!=""){ 
         ?> 
          <div class="alert alert-block alert-danger fade in"> 
           <button data-dismiss="alert" class="close close-sm" type="button"> 
            <i class="icon-remove"></i> 
           </button> 
           <center><strong>اخطار ! </strong> <?=$imp_count;?></center> 
          </div> 
         <?php 
         } 
         ?> 

         <?php 
          if(isset($texterr) && $texterr!=""){ 
         ?> 
          <div class="alert alert-block alert-danger fade in"> 
           <button data-dismiss="alert" class="close close-sm" type="button"> 
            <i class="icon-remove"></i> 
           </button> 
           <center><strong>اخطار ! </strong> <?=$texterr;?></center> 
          </div> 
         <?php 
         } 
         ?> 
         <section class="panel"> 
          <div class="bio-graph-heading">ارسال مطلب جدید</div> 
          <div class="panel-body bio-graph-info"> 
           <form class="form-horizontal" role="form" method="POST"> 
            <div class="form-group"> 
             <label class="col-lg-2 control-label">عنوان مطلب</label> 
             <div class="col-lg-6"> 
              <input type="text" class="form-control" style="width: 620px;" name="title" placeholder=" "> 
             </div> 
            </div> 

            <div class="form-group"> 
             <label class="col-lg-2 control-label">متن پست</label> 
             <div class="col-lg-6"> 
               <textarea name = "text" style="width: 620px; height: 200px;"></textarea> 
             </div> 
            </div> 

            <div class="form-group"> 
             <label class="col-lg-2 control-label">دسته بندی</label> 
             <div class="col-lg-6"> 
              <select name="category"> 
               <?php 
               $sql = "SELECT * FROM `$tbl_category` ORDER BY `id` DESC"; 
               $result = $connect->query($sql); 
               while($rows = $result->fetch(PDO::FETCH_ASSOC)){ 
               ?> 
               <option value="<?=$rows["id"];?>"><?=$rows["title"];?></option> 
               <?php 
               } 
               ?> 
              </select> 
             </div> 
            </div> 

            <div class="form-group"> 
             <label class="col-lg-2 control-label">نمایش در اسلایدر</label> 
             <div class="col-lg-6"> 
              <select name="slider"> 
               <option value="0">غیرفعال</option> 
               <option value="1">فعال</option>   
              </select> 
             </div> 
            </div>         

            <div class="form-group"> 
             <label class="col-lg-2 control-label">نمایش در اخبار مهم</label> 
             <div class="col-lg-6"> 
              <select name="important"> 
               <option value="0">غیرفعال</option> 
               <option value="1">فعال</option>   
              </select> 
             </div> 
            </div> 

            <div class="form-group"> 
             <div class="col-lg-offset-2 col-lg-10"> 
              <input type="submit" name="save" class="btn btn-success" value="ثبت"> 
              <button type="reset" class="btn btn-default">انصراف</button> 
             </div> 
            </div> 
           </form> 
          </div> 
         </section> 
        </aside> 
       </div> 

       <!-- page end--> 
      </section> 
     </section> 
     <!--main content end--> 
    </section> 

    <!-- js placed at the end of the document so the pages load faster --> 
    <script src="js/jquery.js"></script> 
    <script src="js/jquery-1.8.3.min.js"></script> 
    <script src="js/bootstrap.min.js"></script> 
    <script src="js/jquery.scrollTo.min.js"></script> 
    <script src="js/jquery.nicescroll.js" type="text/javascript"></script> 
    <script src="js/jquery.sparkline.js" type="text/javascript"></script> 
    <script src="assets/jquery-easy-pie-chart/jquery.easy-pie-chart.js"></script> 
    <script src="js/owl.carousel.js" ></script> 
    <script src="js/jquery.customSelect.min.js" ></script> 

    <!--common script for all pages--> 
    <script src="js/common-scripts.js"></script> 

    <!--script for this page--> 
    <script src="js/sparkline-chart.js"></script> 
    <script src="js/easy-pie-chart.js"></script> 

    <script> 

     //owl carousel 

     $(document).ready(function() { 
      $("#owl-demo").owlCarousel({ 
       navigation : true, 
       slideSpeed : 300, 
       paginationSpeed : 400, 
       singleItem : true 

      }); 
     }); 

     //custom select box 

     $(function(){ 
      $('select.styled').customSelect(); 
     }); 

    </script> 

    </body> 
</html> 
+0

請破壞你的代碼到更多的塊和寫你已經嘗試過https://stackoverflow.com/help/how-爲了進行調試,請詢問 – Indra

+0

嘗試回顯查詢並手動將其插入到數據庫中。你會發現你的錯誤 – Indra

+0

是'$ tbl_post'實際上是一個爲表定義的變量? –

回答

0

改變這一點:

$sql = "INSERT INTO `$tbl_posts` (`author_id`, `title`, `category_id`, `time`, `text`, `date`, `slider`, `important`) VALUES (:author_id, :title, :category_id, :time, :text, :date, :slider, :important)"; 

$sql = "INSERT INTO `tbl_posts` (`author_id`, `title`, `category_id`, `time`, `text`, `date`, `slider`, `important`) VALUES (:author_id, :title, :category_id, :time, :text, :date, :slider, :important)"; 

您驗證當你的表名前有一個$標誌

而且執行功能的成功或失敗,請這樣做

if ($result === true) { 
     $success =" پست ".$title." در سایت منتشر شد ✓ "; 
    } else { 
     echo "<script>alert('Wrong!');</script>"; 
    } 

isset不驗證查詢是否成功執行。並記住​​返回true或false所以你需要檢查基於布爾值(真或假)

+0

尊敬的阿金 我在db.php中定義$ tbl_posts $ tbl_posts =「tbl_posts」; – MNF7

相關問題