2017-06-16 2005 views
-1

在CMS中編輯帖子。 我收到錯誤。QUERY FAILED。您的SQL語法有錯誤;檢查相應的手冊

(查詢失敗。你在你的SQL語法錯誤;檢查對應於您的MariaDB的服務器版本正確的語法 使用近「post_tags =‘的事情,一下,學習手工 ’,POST_CONTENT = '的 美麗的東西阿布' 在1號線)

<?php 

if (isset($_GET['p_id'])) { 

$the_post_id = $_GET['p_id']; 

} 


$query = "SELECT * FROM posts WHERE post_id = $the_post_id "; 
$select_posts_by_id = mysqli_query($connection,$query); 

    while($row = mysqli_fetch_assoc($select_posts_by_id)) { 
    $post_id = $row['post_id']; 
    $post_title = $row['post_title']; 
    $post_category_id = $row['post_category_id']; 
    $post_image = $row['post_image']; 
    $post_content = $row['post_content']; 
    $post_date = $row['post_date']; 
    $post_tags = $row['post_tags']; 
    $post_comment_count = $row['post_comment_count']; 
    $post_status = $row['post_status']; 


} 


if(isset($_POST['update_post'])) { 


    $post_title = $_POST['post_title']; 
    $post_category_id = $_POST['post_category']; 
    $post_tags = $_POST['post_tags']; 
    $post_status = $_POST['post_status']; 
    $post_image = $_FILES['image']['name']; 
    $post_image_temp = $_FILES['image']['tmp_name']; 
    $post_content = $_POST['post_content']; 

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


     $query = "UPDATE posts SET "; 
     $query .="post_title = '{$post_title}', "; 
     $query .="post_category_id = '{$post_category_id}', "; 
     $query .="post_tags = '{$post_tags }', "; 
     $query .="post_status = '{$post_status }', "; 
     $query .="post_image = '{$post_image}' "; 
     $query .="post_tags = '{$post_tags }', "; 
     $query .="post_content = '{$post_content }', "; 
     $query .="post_comment_count = '{$post_comment_count }', "; 
     $query .="WHERE post_id = {$the_post_id} "; 

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

    confirmQuery($update_post); 

} 

?> 

<form action="" method="post" enctype="multipart/form-data"> 

<div class="form-group"> 
<label for="title">Post Title</label> 
<input value="<?php echo $post_title; ?>" type="text" class="form-control" name="post_title"> 
</div> 


    <div class="form-group"> 

    <select name="post_category" id=""> 

    <?php 

    $query = "SELECT * FROM categories"; 
    $select_categories = mysqli_query($connection,$query); 

    confirmQuery($select_categories);  

    while($row = mysqli_fetch_assoc($select_categories)) { 
    $cat_id = $row['cat_id']; 
    $cat_title = $row['cat_title']; 

    echo "<option value='$cat_id'>{$cat_title}</option>"; 

    } 

    ?> 


    </select> 
    </div> 






    <div class="form-group"> 
     <label for="post_tags">Post tags </label> 
     <input value="<?php echo $post_tags; ?>" type="text" class="form-control" name="post_tags"> 
     </div> 



    <div class="form-group"> 
     <label for="post_status">Post status</label> 
     <input value="<?php echo $post_status; ?>" type="text" class="form-control" name="post_status"> 
     </div> 



     <div class="form-group"> 
     <img width="100" src="../images/<?php echo $post_image; ?>" alt=""> 
     <input type="file" name="image"> 
     </div> 


     <div class="form-group"> 
     <label for="post_content">Post Content</label> 
     <textarea class="form-control "name="post_content" id"" cols="30" rows="10"><?php echo $post_content; ?></textarea> 
     </div> 



     <div class="form-group"> 
     <input class="btn btn-primary" type="submit" name="update_post" value="Update Post"> 
     </div> 



</form> 

回答

1

update語句缺少一個逗號

$query = "UPDATE posts SET "; 
    $query .="post_title = '{$post_title}', "; 
    $query .="post_category_id = '{$post_category_id}', "; 
    $query .="post_tags = '{$post_tags}', "; 
    $query .="post_status = '{$post_status}', "; 
    $query .="post_image = '{$post_image}', "; /* MISSING COMMA */ 
    $query .="post_tags = '{$post_tags}', "; 
    $query .="post_content = '{$post_content}', "; 
    $query .="post_comment_count = '{$post_comment_count}' "; /* EXTRA, uneeded comma */ 
    $query .="WHERE post_id = {$the_post_id}"; 

以及where子句之前的不需要的額外逗號。在關閉大括號之前額外的空格可能會導致問題。如果您要像這樣編寫sql,您可以更容易地發現問題。

$query = "UPDATE posts SET 
       post_title = '{$post_title}', 
       post_tags = '{$post_tags }', 
       post_status = '{$post_status}', 
       post_image = '{$post_image}', 
       post_tags = '{$post_tags}', 
       post_content = '{$post_content}', 
       post_comment_count = '{$post_comment_count}' 
       WHERE post_id = {$the_post_id}"; 
+0

非常感謝你RamRaider先生 –

相關問題