php - 除非我刷新页面,否则数据不会插入数据库?

标签 php mysql ajax database

我有以下文件,这是它们的作用:

input.php:从 XML 提要中获取数据,连接到数据库并将其输入到数据库中。
output.php: 连接到数据库并创建要输出的变量。
index.php: 包括 output.php 并使用变量输出数据。
refresh.js:Ajax刷新div来更新数据。


输入.php

$xml = simplexml_load_file( "http://domain.com/feed" );
$json = json_encode( $xml );
$array = json_decode( $json,TRUE );
$items = $array['channel']['item'];

$DB = new mysqli( 'localhost','USERNAME','PASSWORD','DATABASE' );
if( $DB->connect_errno ){
  print "failed to connect to DB: {$DB->connect_error}";
  exit( 1 );
}

$match = "#^(?:[^\?]*\?url=)(https?://)(?:m(?:obile)?\.)?(.*)$#ui";
$replace = '$1$2';

foreach( $items as $item ){
  $title = $item['title'];
  $url = preg_replace( $match,$replace,$item['link'] );
  $title_url[] = array( $title,$url );
  $sql_values[] = "('{$DB->real_escape_string( $title )}','{$DB->real_escape_string( $url )}')";
}
$SQL = "INSERT IGNORE INTO `read`(`title`,`url`) VALUES\n ".implode( "\n,",array_reverse( $sql_values ) );
if( $DB->query( $SQL ) ){
} else {
  print "failed to INSERT: [{$DB->errno}] {$DB->error}";
}
$DB->set_charset( 'utf8' );

输出.php

$DB = new mysqli( 'localhost','USERNAME','PASSWORD','DATABASE' );
if( $DB->connect_errno ){
  print "failed to connect to DB: {$DB->connect_error}";
  exit( 1 );
}

$query = "SELECT `title`,`url` FROM `read` ORDER BY `order` DESC";
$result = $DB->query( $query );
// error-handling: make sure query returned a result
if( $result ){
  while( $row = $result->fetch_array() ){
      // list gets values from a numeric array
      list( $title,$url ) = $row;
      // using [] (append) is much faster than using array_push()
      $data[] = array( 'title'=>$title,'url'=>$url );
}
$title = $data[0]['title'];
$url = $data[0]['url'];
}else{
  //do something
}

索引.php

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Recently</title>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="js/refresh.js"></script>
</head>

<body>
  <?php include_once "includes/output.php"; ?>

  <p class="title"><span>Read </span>
    <a  id="read" href="<?php echo $url; ?>" target="_blank"><?php echo $title; ?></a>  
  </p>

</body>
</html>

刷新.js

var auto_refresh = setInterval(
 function() {
   $('#read').load('index.php #read');
 }, 2000);

问题是,除非我刷新 input.php(这搞砸了增量计数),否则数据不会插入到数据库中。我不知道为什么会这样。有什么想法吗?

注意:因为我刚刚开始学习更多关于 PHP 和数据库的知识,所以我更喜欢我的 input.php 输出。 php 代码在需要时进行最小程度的改动

最佳答案

基本上你现在的工作流程是:执行一次input.php => 使用JS每隔一段时间执行一次output.php得到输出。所以你可以看到问题是输入有点静态而输出是动态的。

您可以单独修改您的刷新脚本:执行 input.php => 执行 output.php => 几秒钟后执行 input.php again => 再次执行 output.php 等等:

var auto_refresh = setInterval(function() {
    $.get('input.php', function () { // call input here
        $('#read').load('index.php #read'); // call output here
    });    
}, 2000); // run again after 2 seconds, you might wanna make this number higher to offload i/o

希望这对您有所帮助。

关于php - 除非我刷新页面,否则数据不会插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17717870/

相关文章:

php - ZEND 和 MySQL - 如何搜索和 ORDER BY(排序)相关性?

php - Wamp 和 Xampp "Your PHP installation appears to be missing the MySQL extension which is required by WordPress."

java - 将 JSON 字符串处理为 Java 数组

C++ Mysql查询ascii格式

javascript - ajax开始 ajax停止 : how to log requested url

php - Typeahead.js 出现问题

php - Zend CLI 不工作

python - 如何使用Python3.3访问在线MySQL数据库

ajax - .ajax JSONP 解析器错误

javascript - 停止对依赖于 JSON-Pretty 的 JavaScript 进行缓存