0
我有一個連接在這裏建立到數據庫的db.php中文件無法將數據插入到表
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "aigsonlinedb";
$con = new mysqli($host,$user,$pass,$db_name);
function formatDate($date){
return date('g:i a', strtotime($date));
}
?>
這裏「的index.php」文件是該數據應發送的格式,並從數據庫
<?php
include 'db.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Chat System in PHP</title>
<link rel="stylesheet" href="style.css" media="all"/>
<script>
function ajax(){
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
document.getElementById('chat').innerHTML = req.responseText;
}
}
req.open('GET','chat.php',true);
req.send();
}
setInterval(function(){ajax()},1000);
</script>
</head>
<body onload="ajax();">
<div id="container">
<div id="chat_box">
<div id="chat"></div>
</div>
<form method="POST" action="index.php">
<input type="text" name="name" placeholder="enter name"/>
<textarea name="msg" placeholder="enter message"></textarea>
<input type="submit" name="submit" value="Send it"/>
</form>
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$msg = $_POST['msg'];
$query = "INSERT INTO chat (name,msg) VALUES ($name','$msg')";
$run = $con->query($query);
if($run){
echo "<embed loop='false' src='chat.wav' hidden='true' autoplay='true'/>";
}
}
?>
</div>
</body>
</html>
chat.php其中數據是從數據庫
<?php
include 'db.php';
$query = "SELECT * FROM chat ORDER BY id DESC";
$run = $con->query($query);
while($row = $run->fetch_array()) :
?>
<div id="chat_data">
<span style="color:green;"><?php echo $row['name']; ?></span> :
<span style="color:brown;"><?php echo $row['msg']; ?></span>
<span style="float:right;"><?php echo formatDate($row['date']); ?></span>
</div>
<?php endwhile;?>
取出檢索唯一的問題是第e數據不能發送到數據庫。
你有錯誤信息? –
這裏我們不能說太多,因爲你的代碼結構不清楚。請編輯您的問題,並顯示兩個代碼段如何鏈接。同時指定「不工作」的含義。 – arkascha
這段代碼對於mysql注入非常開放 - 這是教科書的例子。請使用準備好的語句。 – Ray