如何在点击时执行mysql请求?

时间:2013-06-05 01:30:04

标签: php mysql sql html

<?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 =&#34; one&#34;或id =&#34;两个&#34;执行这个sql查询(如果单击div一个(两个相同的东西))

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

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

由于

2 个答案:

答案 0 :(得分: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();
    }
    ?>

基本上这就是整件事。如果你愿意的话,我也可以把这些碎片放在一起。

答案 1 :(得分: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文件。