duancaiyi7567 2017-03-31 11:50
浏览 65

为什么没有在mysql数据库中插入数据没有错误出现在屏幕上?

I make a author_create_post.php for a News Agency Portal and Insert the data entries into mysql database. php script and form relies on same page. Form also used validation rules. After fill all form entries finally when I hit submit I got no any error but when I checked my database there is no insert entry, what is the problemb? How to solve it? Here is my complete code...

<?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>
  • 写回答

1条回答 默认 最新

  • dsgsgs30201 2017-03-31 11:54
    关注

    change this:

    $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 )";
    

    to

    $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 )";
    

    You had a $ sign before your table name

    Also when verifying the success or failure of the execute function, do this

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

    isset does not verify that the query was successfully executed. And remember execute() returns true or false So you need to check based on the boolean values(true or false)

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)