【问题标题】:PHP, AJAX, MYSQLI, Not inserting a row [duplicate]PHP,AJAX,MYSQLI,不插入行[重复]
【发布时间】:2017-12-02 21:19:20
【问题描述】:

我正在尝试通过 ajax 请求向数据库插入一行,但该行没有插入数据库

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

但是该行没有插入到数据库中

【问题讨论】:

  • 那么你得到的实际错误是什么?
  • 在每个阶段调试您的脚本并找到实际问题..
  • 列名不能用单引号括起来。 "INSERT INTO cmets ('comment') VALUES ('".$contentToSave."')"
  • @AndyHolmes 仅刷新页面即可
  • @JYoThI 让我试试

标签: php jquery mysql ajax mysqli


【解决方案1】:

在您的插入语句中,删除注释周围的单引号。如果您尝试访问 MySQL 保留关键字,您需要的是反引号以正确转义

【讨论】:

  • 根本不起作用,标头发送 HTTP/1.1 500 错误
【解决方案2】:

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

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

【讨论】:

  • 根本不起作用,标头发送 HTTP/1.1 500 错误
  • @Andrew,你需要给我一些错误回复。关于我回答中的查询本身不是问题。
  • 插入页面时刷新,但不一定要刷新页面
  • @Andrew,这是一个不同的问题,让我们关注这个问题中的“不插入行”。如果您有其他问题,请提出其他问题,这就是 Stackoverflow 中的工作原理。
猜你喜欢
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 2018-10-27
  • 2017-10-20
  • 1970-01-01
相关资源
最近更新 更多