PHP,AJAX,MYSQLI,不插入行

时间:2017-06-29 08:59:19

标签: php jquery mysql ajax mysqli

我试图通过ajax请求向DB插入一行,但该行没有插入到数据库中

 //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

但该行未插入数据库

2 个答案:

答案 0 :(得分:0)

在insert语句中,删除注释周围的单引号。如果您正在尝试访问MySQL保留关键字

,那么您需要的是正确转义

答案 1 :(得分:0)

您的问题是您引用了列名称。使用

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