Home  >  Article  >  Backend Development  >  使用Ajax请求的数据为何保存不到数据库中?后台用php写,各位大神支个招

使用Ajax请求的数据为何保存不到数据库中?后台用php写,各位大神支个招

WBOY
WBOYOriginal
2016-06-13 12:19:491299browse

使用Ajax请求的数据为什么保存不到数据库中?后台用php写,各位大神支个招

<br /><?php<br />	if(isset($_GET['content'])){<br />		//从请求URL地址中获取参数<br />		$view_name=$_GET["name"];<br />		$view_content=$_GET["content"];<br />		$view_time=$_GET["time"];<br />		$linkview=mysqli_connect("localhost", "root", "xxxxxxx", "wordstv") or die("数据库连接失败:".mysql_errno());<br />		$queryview="insert into wordstv_user_view (user_name,user_content,time,) values ('".$view_name."','".$view_content."','".$view_time."')";<br />		[email&#160;protected]_query($linkview, $queryview);<br />		if($resultview){<br />			echo $view_name."&&&".$view_content."&&&".$view_time;<br />		}<br />		mysqli_close($linkview);<br />	}<br />?><br />

------解决思路----------------------
先确定ajax提交的数据是否正确获取。
在php 最前加
$data = $_REQUEST;
file_get_contents('test.log', json_encode($data), true);

看看能否获取到数据,如果可以,再在入库的语句中找问题。
否则在js ajax提交的语句中找问题。

[email protected]_query($linkview, $queryview) or die(mysql_error()); 这里也加上看看。
------解决思路----------------------
content 通常表示正文,一般比较大
如果整个传送的数据可能超过 get 方式的 2K 限制的话,应使用 post 方式
从减少信息泄露的角度上看,使用 POST 方式也是较为妥当的

你直接将传入的数据入库了,如果传入的数据中含有特殊字符,就将导致插入失败

你只在插入成功时输出了信息,却没在插入失败时做出响应
无论是操作成功与失败都应有信息返回,这样才方便判断问题的所在
------解决思路----------------------

引用:
Quote: 引用:

先确定ajax提交的数据是否正确获取。
在php 最前加
$data = $_REQUEST;
file_get_contents('test.log', json_encode($data), true);

看看能否获取到数据,如果可以,再在入库的语句中找问题。
否则在js ajax提交的语句中找问题。

[email protected]_query($linkview, $queryview) or die(mysql_error()); 这里也加上看看。

谢谢啊,已经解决了


如问题解决了请结贴。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn