0

I am trying to insert a row to the DB via ajax request, but the row is not inserting to the 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>

And the 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

But the row is not inserting to the DB

Andrew
  • 13
  • 1
  • 10

2 Answers2

0

In your insert statement, remove the single quotes around the comment. What you need is back ticks to properly escape if you are trying to access a MySQL reserved keyword

Rotimi
  • 4,783
  • 4
  • 18
  • 27
0

Your problem is that you have quoted the column name. Use

$insert_row = "INSERT INTO comments (comment) VALUES 
('".$contentToSave."')";
Starx
  • 77,474
  • 47
  • 185
  • 261
  • not worked at all, headers sent HTTP/1.1 500 error – Andrew Jun 29 '17 at 09:05
  • @Andrew, you need to give me some error responses. Regarding the query itself in my answer is not the problem. – Starx Jun 29 '17 at 09:09
  • While inserting the page refreshes, however it not has to refresh the page – Andrew Jun 29 '17 at 09:10
  • @Andrew, that's a different question, let's focus on "not inserting row" in this question. If you have another problem you ask another question, that's how it works in Stackoverflow. – Starx Jun 29 '17 at 09:12