我为新闻社门户网站创建了author_create_post.php,并将数据条目插入到mysql数据库中。 php脚本和表单依赖于同一页面。表格还使用了验证规则。在我点击提交后最终填写所有表单条目后我没有任何错误但是当我检查我的数据库时没有插入条目,有什么问题?怎么解决?这是我的完整代码...
<?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 :(得分: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不验证查询是否已成功执行。并记住execute()
返回true或false所以你需要根据布尔值(true或false)进行检查