使用HTML表单值添加到数据库中(从加代替.php)时,我尝试在页面加载时向数据库发送值(目标是实时提醒用户在该页面上)。
我认为这需要修改该文件是add.php,尤其是这样的:
"<?php require('common.php');
if($_POST && !empty($_POST['title'])){
$result = $db->add_news(strip_tags($_POST['title']));
}?>"
我remplaced由:
$link = mysqli_connect("localhost", "user", "mypass", "db");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "INSERT INTO news (title) VALUES ('This page has been loaded !')";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
问题是自动刷新不工作。我想这是因为该行从add.php丢失:
"$result = $db->add_news(strip_tags($_POST['title']));"
我怎样才能在SQL表增值后要求自动刷新? 我想我必须使用从db.php函数register_changes,但我不知道如何以及在哪里..我是初学者,并失去了!
非常感谢您的帮助。
这里是原始文件:
add.php:
if($_POST && !empty($_POST['title'])){
$result = $db->add_news(strip_tags($_POST['title']));
}?>
Add news - Demo for Ajax AutorefreshThis is a demo for post Ajax Auto Refresh - Volume II
Open list of messages in new window, add new message with this form and look at that list. Don't refresh it manually. It should refresh automatically after 20 seconds.
if($result==TRUE){
echo '
Success
';}else{
echo '
Error
';}
}else{?>
的common.php:
require_once ('db.php'); //get our database class
$db = new db();
/* end of file common.php */
db.php中:
/**
* Class db for Ajax Auto Refresh - Volume II - demo
*
* @author Eliza Witkowska
* @link http://blog.codebusters.pl/en/entry/ajax-auto-refresh-volume-ii
*/
class db{
/**
* db
*
* @var $ public $db;
*/
public $db;
/**
* __construct
*
* @return void
*/
function __construct(){
$this->db_connect('localhost','root','alamakota','test');
}
/**
* db_connect
*
* Connect with database
*
* @param mixed $host
* @param mixed $user
* @param mixed $pass
* @param mixed $database
* @return void
*/
function db_connect($host,$user,$pass,$database){
$this->db = new mysqli($host, $user, $pass, $database);
if($this->db->connect_errno > 0){
die('Unable to connect to database [' . $this->db->connect_error . ']');
}
}
/**
* check_changes
*
* Get counter value from database
*
* @return void
*/
function check_changes(){
$result = $this->db->query('SELECT counting FROM news WHERE id=1');
if($result = $result->fetch_object()){
return $result->counting;
}
return 0;
}
/**
* register_changes
*
* Increase value of counter in database. Should be called everytime when
* something change (add,edit or delete)
*
* @return void
*/
function register_changes(){
$this->db->query('UPDATE news SET counting = counting + 1 WHERE id=1');
}
/**
* get_news
*
* Get list of news
*
* @return void
*/
function get_news(){
if($result = $this->db->query('SELECT * FROM news WHERE id<>1 ORDER BY add_date DESC LIMIT 50')){
$return = '';
while($r = $result->fetch_object()){
$return .= '
id: '.$r->id.' | '.htmlspecialchars($r->title).'
';$return .= '
';
}
return $return;
}
}
/**
* add_news
*
* Add new message
*
* @param mixed $title
* @return void
*/
function add_news($title){
$title = $this->db->real_escape_string($title);
if($this->db->query('INSERT into news (title) VALUES ("'.$title.'")')){
$this->register_changes();
return TRUE;
}
return FALSE;
}
}
/* End of file db.php */
的index.php :
Demo for Ajax Auto Refresh/* AJAX request to checker */
function check(){
$.ajax({
type: 'POST',
url: 'checker.php',
dataType: 'json',
data: {
counter:$('#message-list').data('counter')
}
}).done(function(response) {
/* update counter */
$('#message-list').data('counter',response.current);
/* check if with response we got a new update */
if(response.update==true){
$('#message-list').html(response.news);
}
});
}
//Every 20 sec check if there is new update
setInterval(check,20000);
This is a demo for post Ajax Auto Refresh - Volume II
<?php echo $db->get_news();?>
checker.php:
//get current counter
$data['current'] = (int)$db->check_changes();
//set initial value of update to false
$data['update'] = false;
//check if it's ajax call with POST containing current (for user) counter;
//and check if that counter is diffrent from the one in database
if(isset($_POST) && !empty($_POST['counter']) && (int)$_POST['counter']!=$data['current']){
//the counters are diffrent so get new message list
$data['news'] = '
OMG! It\'s alive!!! NEW UPDATE !!!
';$data['news'] .= $db->get_news();
$data['update'] = true;
}
//just echo as JSON
echo json_encode($data);
/* End of file checker.php */
2017-04-26
Francky
+0
我想你只是简单地添加插入查询获取路线 –