使用mysql数据库

时间:2017-09-21 09:08:07

标签: javascript php jquery mysql ajax

我正在一个项目中,我将日期和时间(in_time和out_time)存储在网页的数据库表中。我真正想要的是每两分钟后向out_time列添加2分钟,直到网页关闭或者用户以任何方式离开页面。我一直在提出AJAX次请求,但它没有显示对数据库的任何影响' out_time'柱。我可以知道我在这里做错了吗?

这是我在网页上使用的脚本,我想记录它的时间:

   <?php include 'blocks/leftnavInc.php' ; ?> 
<?php

    if(isset($_REQUEST['chapterId']) && $_REQUEST['chapterId']!='')
    {
        $chapterId      =   $fnc->decode(trim($_REQUEST['chapterId']));
    }
    if(isset($_REQUEST['moduleId']) && $_REQUEST['moduleId']!='')
    {
        $moduleId=$fnc->decode(trim($_REQUEST['moduleId']));
    }
  $uid      =   $_SESSION['session_user_id'];

  ?>
      </div>

<script>

$(document).ready(function(){
    function out_ajax(){
    $.ajax({ 
      type:"POST",
      url:"out_time.php",
      success:function(html){
       }   
        });
    }

    window.setInterval(function(){
        out_ajax();
    }, 120000);
});
    </script> 
  <?php 
  $inTime       =   date("Y-m-d H:i:s");
    $outTime    =   date("Y-m-d h:i:s");    
    $difference =   strtotime($outTime)-strtotime($inTime);
    $created    =   date("Y-m-d");
    $dataTime   =   array("user_id"=>$uid, "module_id"=>$moduleId, "chapter_id"=>$chapterId,"in_time"=>$inTime, "out_time"=>$outTime, "created"=>$created);
    $db->query_insert("tbl_ppt_time", $dataTime);
  $_SESSION["last_id"]  = mysql_insert_id(); 
  ?> 
  </div>
    <div class="col-xs-12 col-sm-9 page-right">
      <div class="panel panel-primary">
        <div class="panel-heading"><?php echo $db->idToField("tbl_question_module", "title", $moduleId) ; ?>
          <a href='studyMaterial.php?moduleId=<?php echo $_REQUEST['moduleId'];?>' class="btn btn-danger" style="margin-left: 75%;margin-top: -24px;"><i class="glyphicon glyphicon-ban-circle"></i> Go Back Study Material</a>
      </div>
        <div class="panel-body">
          <div class="col-sm-9">
<div id="viewer" class="pdf-viewer"  data-url="../sadmin/studyMaterial/<?php echo $db->idToField("tbl_studymaterials", "file_ppt", $chapterId) ; ?>"> </div>
 </div>  
<div class="col-sm-3">
            <div id="left_panel" class="clearfix left">
              <div class="leftNavigation">
                <ul id="menu-content" class="menu-content">
<?php
$sq             =   $db->query("SELECT id, title from tbl_question_module WHERE status = 1");
while($rowM=mysql_fetch_array($sq))
{
?> 
    <li  data-toggle="collapse" data-target="#rr<?php echo $rowM['id'] ; ?>" class="collapsed "> <a href="#"><?php echo $rowM['title'] ; ?> <span class="arrow"></span></a>
                    <ul class="sub-menu collapse" id="rr<?php echo $rowM['id'] ; ?>">
<?php $fnc->getModuleChaptersppt($rowM['id']) ; ?>  
</ul>
</li>
<?php } ?> 
</ul>
</div>
 </div>
</div>
</div>
        </div></div>
    </div>

另外我的out_time.php看起来像这样:

<?php
@session_start();
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
  die('Could not connect: ' . mysql_error());
}
echo '';
mysql_select_db( 'dbname' );

$uid = $_SESSION['session_user_id'];
$moduleId = $_REQUEST['module_id'];
$chapterId = $_REQUEST['chapter_id'];
$last_id = $_SESSION["last_id"];

$update_time = mysql_query("UPDATE tbl_ppt_time SET out_time = now() WHERE id='$last_id'  AND user_id = '$uid' AND module_id='$moduleId' AND chapter_id='$chapterId'");
?>

任何帮助都将受到高度赞赏。感谢社区提前。

1 个答案:

答案 0 :(得分:0)

由于您的主页面从请求中获取章节ID和模块ID,并且它们不会在页面的整个生命周期中进行更改,因此您可以将这些内容简单地注入到Javascript中,以便以后用作参数来发送ajax请求:

$.ajax({ 
  type:"POST",
  url:"out_time.php",
  data: { "chapter_id": "<?php echo $chapterId; ?>", "module_id": "<?php echo $moduleId; ?>" },
  success:function(html){
  }   
});

P.P.S您的主页检查查询字符串中是否存在章节ID和模块ID,然后对其进行解码。但如果他们不是呢?它只是继续,我想它可能会崩溃。它应该有更好的错误处理,但这是一项单独的任务。