1
<?php
        require 'connect.inc.php';
        $query = "SELECT `item`, `up`, `down` FROM mytable ORDER BY RAND() LIMIT 1";
        $query_run = mysql_query($query);

        if ($query_run = mysql_query($query)) {

            while ($query_row = mysql_fetch_assoc($query_run)) {
                $item = $query_row['item'];
                $up = $query_row['up'];
                $down = $query_row['down'];
    ?>  
    <div id="one">
        <div id="votes">
            <?php
                echo "$up votes";
            ?>
        </div>
    </div>
    <div id="or">
        <div id="item">
            <?php echo $item ?>
        </div>
    </div>
    <div id="two">
        <div id="votes">
            <?php
                echo "$down votes";
            ?>
        </div>
    </div>
    <?php       }

        } else {
            echo mysql_error();
        }
        ?>

这是我的代码。我需要这样做,因此当用户单击 div id="one" 或 id="two" 时执行此 sql 查询(如果单击 div 一个(两个相同))

UPDATE mytable SET up=$up+1 WHERE id="1(can be any id)"

另外,我可以在 sql 查询中进行数学运算吗?

谢谢

4

2 回答 2

2

好的,所以这包括 3 个部分:

1.html。2.JS/ajax。3.php。

1.) HTML。

<div data-action="up" class="clicker" id="<?php echo $item; ?>">
    <div id="votes">
        <?php
            echo "$up votes";
        ?>
    </div>
</div>
<div id="or">
    <div id="item">
        <?php echo $item ?>
    </div>
</div>
<div data-action="down" class="clicker" id="<?php echo $item; ?>">
    <div id="votes">
        <?php
            echo "$down votes";
        ?>
    </div>
</div>

2.) JS/Ajax。

 <script type='text/javascript' >
$('.clicker').click(function (){ 
    doAjaxCall($(this).attr('id'), $(this).attr('data-action'));
});

function doAjaxCall(varID, vote){
    var pageUrl = document.URL;
    ar post = '{"ajax":"yes", "varID":' + varID + ', "vote":' + vote + '}';// Incase you want to pass dynamic ID
    $.post(pageUrl,post,function(data){
        var response = $.parseJSON( data )
        if(response.success){
            //do whatever you like.
            alert('baaaam');
        }
    }); 
}
</script>

3.) php。

if($_POST['ajax'] == 'yes'){
    $field = $_POST['vote'];
    $query = "UPDATE mytable SET `$field` = `$field` +1 WHERE id=".$_POST['var_id'];
    $query_run = mysql_query($query);
    $return = array();
    $return['success'] = false;
    if($query_run){
       $return['success'] = true;
    }
    echo json_encode($return);
    die()
}

更新整个页面

if($_POST['ajax'] == 'yes'){
    $field = $_POST['vote'];
    $query = "UPDATE mytable SET `$field` = `$field` +1 WHERE id=".$_POST['var_id'];
    $query_run = mysql_query($query);
    $return = array();
    $return['success'] = false;
    if($query_run){
       $return['success'] = true;
    }
    echo json_encode($return);
    die()
}


$query = "SELECT `item`, `up`, `down` FROM mytable ORDER BY RAND() LIMIT 1";
$query_run = mysql_query($query);

if ($query_run = mysql_query($query)) {

while ($query_row = mysql_fetch_assoc($query_run)) {
$item = $query_row['item'];
$up = $query_row['up'];
$down = $query_row['down'];

?>
<div data-action="up" class="clicker" id="<?php echo $item; ?>">
    <div id="votes">
        <?php
            echo "$up votes";
        ?>
    </div>
</div>
<div id="or">
    <div id="item">
        <?php echo $item ?>
    </div>
</div>
<div data-action="down" class="clicker" id="<?php echo $item; ?>">
    <div id="votes">
        <?php
            echo "$down votes";
        ?>
    </div>
</div>

<script type='text/javascript' >
$('.clicker').click(function (){ 
    doAjaxCall($(this).attr('id'), $(this).attr('data-action'));
});

function doAjaxCall(varID, vote){
    var pageUrl = document.URL;
    var post = '{"ajax":"yes", "varID":' + varID + ', "vote":' + vote + '}';// Incase you want to pass dynamic ID
    $.post(pageUrl,post,function(data){
        var response = $.parseJSON( data )
        if(response.success){
            //do whatever you like.
            alert('baaaam');
        }
    }); 
}
</script>
<?php       }

    } else {
        echo mysql_error();
    }
    ?>

基本上这就是全部。如果你愿意,我也可以为你拼凑起来。

于 2013-06-05T02:07:44.677 回答
0

您可以在 javascript/jquery 中添加和onClick()事件。然后,做一个这样的 POST

$("#div1").onClick(function(){
   var info = $(this).val();
   $.post("fileWithQuery.php",{toUpdate: info},function(data){  // (fileName to post, variables to send via POST, callBack function)
      // Show a message
   });
});

您只需要创建该 php 文件。

于 2013-06-05T01:47:26.433 回答