传递数据库字段ID的运行SQL取决于字段值

时间:2013-03-25 23:41:02

标签: php jquery mysql ajax database

我正在尝试构建一个CMS,我可以点击星形图标,它会更改我的数据库中的art_featured值,所以如果art_featured值为0,我点击我的星形图标我想将字段的值从0更改为1.我有点工作,但我不知道如何传递art_featured值,通常我只是id我的span art_featured但我已经在使用它,所以我可以告诉我需要更改哪篇文章,那么如何才能将SQL的值赋予我的SQL语句,以便我可以运行if art_featured和else语句然后运行某个art_id art_title art_company art_featured 1 lorem 1 lorem ipsum 1 1 2 lorem 2 lorem ipsum 2 0 语句,具体取决于<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>

的值

提前感谢您的帮助!

        $(".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 {

            }  
        }
        });

    });

HTML / 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";
}

jQuery

{{1}}

mySQL / PHP

{{1}}

1 个答案:

答案 0 :(得分: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语句是不安全的,所以你应该阅读数据卫生。但这应该做到。