2017-06-29 88 views
0

我想通過Ajax請求插入一行到數據庫,但該行不插入到數據庫PHP,AJAX,mysqli的,不插入一行

//Simple form 
<div class="form_style"> 
<form action="#" method="POST"> 
<textarea name="content_txt" id="contentText" cols="45" rows="5" 
placeholder="Enter some text"></textarea> 
<button id="FormSubmit">Add record</button> 
</form> 
</div> 

而且response.php

if(isset($_POST["content_txt"])){ 
//MYSQL CLASS 
include('includes/mysql.inc.php'); 
file_get_contents('php://input'); 
//Accessing the POST variables 
$contentToSave = $_POST["content_txt"]; 
//Inserting the row  
$insert_row = "INSERT INTO comments (comment) VALUES 
('".$contentToSave."')"; 
$r = mysqli_query($dbc, $insert_row);  
} 
else 
{ 
//This block is executing right the moment, seems like $_POST datas not 
POSTING 
header('HTTP/1.1 500 Error occurred, Could not process request!'); 
exit(); 
} //seems like maybe the jquery include file is not proper? but i checked it connecting not locally to the jquery library 

但該行不插入到數據庫

+0

那麼你得到的實際錯誤是什麼? –

+0

在每個階段調試腳本並找到實際問題。 –

+0

列名不應該被單引號括起來。 「INSERT INTO評論(‘評論’)VALUES (「」。$ contentToSave。「」)」 – JYoThI

回答

0

在你插入語句,刪除註釋周圍的單引號。如果您試圖訪問MySQL保留關鍵字,您需要的是返回勾號以正確地轉義。

+0

根本沒有工作,頭髮送HTTP/1.1 500錯誤 – Andrew

0

您的問題是您引用了列名。使用

$insert_row = "INSERT INTO comments (comment) VALUES 
('".$contentToSave."')"; 
+0

沒有在所有工作,頭髮送HTTP/1.1 500錯誤 – Andrew

+0

@Andrew,你需要給我一些錯誤r esponses。關於我的答案中的查詢本身不是問題。 – Starx

+0

雖然插入頁面刷新,但它不必刷新頁面 – Andrew