2013-03-25 33 views
1

我試圖建立一個CMS,我可以點擊一個星形圖標,它會改變我的數據庫中的art_featured值,所以說如果art_featured值爲0,我點擊我的星形圖標我想將字段的值從0更改爲1.我有它的工作原理,但我不知道如何通過art_featured的值,通常我只是我的id在我的span,但我是已經在使用,因此我可以告訴哪篇文章需要更改,所以我如何獲得art_featured到我的SQL語句的值,以便我可以運行if和else語句,然後根據art_featured的值運行某個SQL語句,通過數據庫字段ID的運行SQL根據字段值

在此先感謝您的幫助!

TABLE

art_id art_title art_company art_featured 
1  lorem 1 lorem ipsum 1 1 
2  lorem 2 lorem ipsum 2 0 

HTML/PHP

<section class="row"> 
    <?php 
    $sql_categories = "SELECT art_title, art_company, art_id, art_featured FROM app_articles"; 

     if($result = query($sql_categories)){ 
      $list = array(); 

      while($data = mysqli_fetch_assoc($result)){ 
       array_push($list, $data); 
      } 

      foreach($list as $i => $row){ 
      ?> 
       <div class="row"> 
        <div class="column two"><p><?php echo $row['art_title']; ?></p></div> 
        <div class="column two"><p><?php echo $row['art_company']; ?></p></div> 
        <div class="column one"><span id="<?php echo $row['art_id']; ?>" class="icon-small star"></span></div> 
       </div> 
      <?php 
      } 
     } 
     else { 
      echo "FAIL"; 
     } 
    ?> 
    </section> 

jQuery的

 $(".star").click(function(){ 

     var art_id = $(this).attr('id'); 

     $.ajax({ 
     type: "POST", 
     data: {art_id:art_id}, 
     url: "ajax-feature.php", 
     success: function(data){ 
      if(data != false) { 

      } 
      else { 

      } 
     } 
     }); 

    }); 

的MySQL/PHP

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



    $sql_articles = "UPDATE `app_articles` SET `art_featured` = 1 WHERE `art_id` =".$_POST['art_id']; 

    if(query($sql_articles)) { 
     echo "YES"; 
    } 
    else { 
     echo "NO"; 
    } 
} 
else { 
    echo "FAIL"; 
} 

回答

0
$sql_detail = "SELECT * FROM app_articles 
WHERE art_id = " . $_POST['art_id']; 
$sql_result = mysql_query($sql_detail); 
if(mysql_num_rows($sql_result) > 0) { // the art_id supplied exists 

    while($sR = mysql_fetch_array($sql_result)) { 

     $art_title = $sR['art_title']; 
     $art_company = $sR['art_company']; 
     $art_featured = $sR['art_featured]; 

     // Do whatever you want with these variables 

    } 

} else { // the art_id supplied does not exist 

} 

將$ _POST ['art_id']直接傳遞到SQL語句是不安全的,因此您應該閱讀有關數據衛生的信息。但是這應該做。

+0

對不起,我已經有$(document).ready(function {});我忘了包括這一點。我需要知道的是如何將我的sql結果中的php變量傳遞給我正在更新數據庫的sql語句。 – user2201765 2013-03-26 00:14:08

+0

@ user2201765我更新了它。這是你需要的嗎? – 2013-03-26 00:51:39