php - Data not inserting into database unless I refresh the page? -
i have following files , do:
input.php: grabs data xml feed, connects , inputs database.
output.php: connects database , creates variables outputted.
index.php: includes output.php
, uses variables output data.
refresh.js: ajax refresh divs update data.
input.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 connect db: {$db->connect_error}"; exit( 1 ); } $match = "#^(?:[^\?]*\?url=)(https?://)(?:m(?:obile)?\.)?(.*)$#ui"; $replace = '$1$2'; foreach( $items $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 `read`(`title`,`url`) values\n ".implode( "\n,",array_reverse( $sql_values ) ); if( $db->query( $sql ) ){ } else { print "failed insert: [{$db->errno}] {$db->error}"; } $db->set_charset( 'utf8' );
output.php
$db = new mysqli( 'localhost','username','password','database' ); if( $db->connect_errno ){ print "failed connect db: {$db->connect_error}"; exit( 1 ); } $query = "select `title`,`url` `read` order `order` desc"; $result = $db->query( $query ); // error-handling: make sure query returned result if( $result ){ while( $row = $result->fetch_array() ){ // list gets values numeric array list( $title,$url ) = $row; // using [] (append) faster using array_push() $data[] = array( 'title'=>$title,'url'=>$url ); } $title = $data[0]['title']; $url = $data[0]['url']; }else{ //do }
index.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>
refresh.js
var auto_refresh = setinterval( function() { $('#read').load('index.php #read'); }, 2000);
the problem unless refresh input.php
(which screws increment count), data not inserted database. have no idea why happening. thoughts?
note: because starting learn more php , databases, i'd prefer input.php
, output.php
code minimally altered if need be.
basically current workflow is: execute input.php
once => use js execute output.php
in interval output. can see problem input kinda static , output dynamic.
you can modify refresh script alone to: execute input.php
=> execute output.php
=> after second execute input.php
again => execute output.php
again , on:
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, might wanna make number higher offload i/o
hope helps.
Comments
Post a Comment