2017-03-03 72 views
0
<?php 
    if(isset($_POST['create_post'])) 
    { 
     $post_title = $_POST['title']; 
     $post_author = $_POST['post_author']; 
     $post_category_id = $_POST['post_category_id']; 
     $post_status = $_POST['post_status']; 
     $post_image = $_FILES['image']['name']; 
     $post_image_temp = $_FILES['image']['tmp_name']; 
     $post_tags = $_POST['post_tags']; 
     $post_content = $_POST['post_content']; 
     $post_date = date('d-m-y'); 
     $post_comment_count = 4; 


     move_uploaded_file($post_image_temp, "../image/ $post_image"); 

     $query = "INSERT INTO posts(post_category_id, post_title, post_author, post_date, post_image, post_content, post_tags, post_comment_count, post_status) "; 

     $query .= "Values ($post_category_id, '$post_title', '$post_author',now(), '$post_image', '$post_content', '$post_tags', '$post_comment_count', '$post_status') "; 

     $connet_query_post = mysqli_query($connection, $query); 

     if(!$connet_query_post) 
     { 

      die("Query Failed" . mysqli_error($connection)); 
     } 



    } 

?> 

<h1 class="page-header"> 
         Wellcome to Admin 
         <small>author</small> 
        </h1> 
<form action="" method="post" enctype="multipart/form-data"> 
    <div class="form-group"> 
     <label for="title">Post title</label> 
     <input type="text" class="form-control" name="title" > 
    </div> 
    <div class="form-group"> 
     <label for="post_category">Post Category Id </label> 
     <input type="text" class="form-control" name="post_category_id" > 
    </div> 
    <div class="form-group"> 
     <label for="post_author">Post Author </label> 
     <input type="text" class="form-control" name="post_author"> 
    </div> 
    <div class="form-group"> 
     <label for="post_status">Post Status </label> 
     <input type="text" class="form-control" name="post_status" > 
    </div> 
    <div class="form-group"> 
     <label for="post_image">Post Image</label> 
     <input type="file" class="form-control" name="image" > 
    </div> 
    <div class="form-group"> 
     <label for="post_tags">Post Tags </label> 
     <input type="text" class="form-control" name="post_tags" > 
    </div> 
    <div class="form-group"> 
     <label for="post_content">Post Contents</label> 
     <textarea class="form-control" name="post_content" id="" cols="30" rows="10"></textarea> 
    </div> 
    <div class="form-group"> 
     <label for="post_tags">Post Tags </label> 
     <input type="text" name="create_post" class="form-control"> 
    </div> 
    <div class="form-group"> 
     <input class="btn btn-primary" type="submit" value="Publish" name="create_post" > 
    </div> 
</form> 

这里是我负责的,我得到一个错误,多数民众赞成的形式是为什么我的插入查询不起作用?

Query FailedYou have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' '', '',now(), '', '', '', '4', '')' at line 1

我检查了我的查询它根据我看起来是正确的

+0

是什么回声$查询的输出一定的价值? 可以请你把它的原始输出的echo $查询? –

+0

您在查询中插入的值存在问题。显示用实际数据替换变量的示例查询。另外请注意,由于您没有清理数据,因此您已经开放SQL注入。了解准备好的陈述,因为你仍然在学习 –

+0

@Ankitvadariya我正在管理面板,所以我创建了一个窗体,试图填充它,但它显示错误,我无法理解如何删除这个概念填充窗体,然后它必须提交到数据库 –

回答

0

您可能正在使用在一个php变量中使用撇号。使用mysqli_real_escape_string()函数对从窗体获得的每个变量进行操作。你应该一直这样做,因为SQL注入。

0

什么是您使用的这些

“post_category_id,POST_TITLE,post_author,POST_DATE,post_image,POST_CONTENT,post_tags,post_comment_count,post_status” 列类型。

对于字符串也使用mysqli_real_escape_string(),您需要传递活动连接作为第一个参数。

0
move_uploaded_file($post_image_temp, "../image/ $post_image"); 

$query = "INSERT INTO posts(post_category_id, post_title, post_author, post_date, post_image, post_content, post_tags, post_comment_count, post_status) "; 

$query .= "Values ($post_category_id, '$post_title', '$post_author','".now()."', '$post_image', '$post_content', '$post_tags', '$post_comment_count', '$post_status') "; 

$connet_query_post = mysqli_query($connection, $query); 

      if(!$connet_query_post) 
      { 

       die("Query Failed" . mysqli_error($connection)); 
      } 

now()是函数,你不能使用它作为字符串。

Query FailedYou have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' '', '',now(), '', '', '', '4', '')' at line 1 
在你的错误,你现在可以看到()

现在instated的()应该有串

+0

我正在看什么样的价值教程,教练做了同样的事情,他没有通过任何值 –

+0

只是用'“.now()。''替换now()并尝试 –

+0

致命错误:现在调用未定义的函数C:\ xampp \ htdocs \ cms \ admin \ includes \ add_post.php on line 18 –