通过php将字符串写入postgres,托管在heroku上
我使用php通过ajax接收歌曲请求,并将其保存到postgres db 一切都在heroku中,我可以使用pgadmin连接到db fine 我得到以下错误-从get请求获取字符串并写入db的正确方法是什么 2014-08-24T01:43:32.062128+00:00应用程序[网站1]:[周日8月24日] 01:43:31.864706 2014][proxy_fcgi:error][pid 64:tid 139700481152768] [client 10.53.42.218:51189]AH01071:get error'PHP message:PHP 警告:pg_query():查询失败:错误:语法错误位于或接近 “\”\n第1行:插入歌曲(请求)值(\'song\)\n通过php将字符串写入postgres,托管在heroku上,php,postgresql,heroku,Php,Postgresql,Heroku,我使用php通过ajax接收歌曲请求,并将其保存到postgres db 一切都在heroku中,我可以使用pgadmin连接到db fine 我得到以下错误-从get请求获取字符串并写入db的正确方法是什么 2014-08-24T01:43:32.062128+00:00应用程序[网站1]:[周日8月24日] 01:43:31.864706 2014][proxy_fcgi:error][pid 64:tid 139700481152768] [client 10.53.42.218:5118
^在第21行的/app/songs.php中\n',参考: 2014-08-24T01:43:32.062130+00:00应用程序[web.1]:[24-Aug-2014 01:43:31] 警告:[pool www]child 61对stderr说:“注意:PHP消息: PHP警告:pg_query():查询失败:错误:或处的语法错误 靠近“\” 2014-08-24T01:43:32.062132+00:00应用程序[web.1]:[24-Aug-2014 01:43:31] 警告:[pool www]child 61对stderr说:“第1行:插入到 歌曲(请求)值(\'song\')” 2014-08-24T01:43:32.062134+00:00应用程序[web.1]:[24-Aug-2014 01:43:31] 警告:[pool www]孩子61对stderr说:“
^在第21行的/app/songs.php中” 代码如下:
CREATE TABLE songs
(
request character varying[] NOT NULL,
created bigint,
id bigserial NOT NULL,
CONSTRAINT "Pk" PRIMARY KEY (id)
)
$.ajax({
type: "GET",
url: "songs.php",
data: q,
success: function(resp){
// console.log(q)
// console.log(resp)
try{var song_resp = JSON.parse(resp)}
catch(err){var song_resp = err}
// console.log(song_resp)
// console.log(target)
if (song_resp.pass == true){
$("#nice_choice").slideDown(250)
// $("#nice_choice").css("-webkit-animation-play-state","running")
// $("#nice_choice").css("-animation-play-state","running")
}
else {
$("#something_wrong").slideDown(250)
// $("#something_wrong").css("-webkit-animation-play-state","running")
// $("#something_wrong").css("-animation-play-state","running")
}
},
error: function (jqXHR, exception){
$("#something_wrong").slideDown(300)
// $("#something_wrong").css("-webkit-animation-play-state","running")
// $("#something_wrong").css("-animation-play-state","running")
}
}); // Ajax Call
<?php
if ($_GET['q']){
$song = $_GET["q"];
$dbconn = pg_connect("host=ec2-54-247-111-1.eu-west-1.compute.amazonaws.com
dbname=dbname
user=user
password=pw")
or die('Could not connect: ' . pg_last_error());
$result = pg_query($dbconn, "INSERT INTO songs (request) VALUES(\'song\')");
//dump the result object
if ($result == false) {
echo false;
}
else{
echo true;
}
// Closing connection
pg_close($dbconn);
}
?>
创建表格歌曲
(
请求字符[]不为空,
创建了bigint,
id bigserial不为空,
约束“Pk”主键(id)
)
$.ajax({
键入:“获取”,
url:“songs.php”,
数据:q,
成功:功能(resp){
//控制台日志(q)
//控制台日志(resp)
试试{var song_resp=JSON.parse(resp)}
catch(err){var song_resp=err}
//控制台日志(song_resp)
//console.log(目标)
如果(song_resp.pass==真){
$(“不错的选择”)。向下滑动(250)
//$(“#不错的选择”).css(“-webkit动画播放状态”,“正在运行”)
//$(“#不错的选择”).css(“-animation play state”,“running”)
}
否则{
$(“#有问题”)。向下滑动(250)
//$(“#有问题”).css(“-webkit动画播放状态”,“正在运行”)
//$(“#有问题”).css(“-animation play state”,“running”)
}
},
错误:函数(jqXHR,异常){
$(“#有问题”)。向下滑动(300)
//$(“#有问题”).css(“-webkit动画播放状态”,“正在运行”)
//$(“#有问题”).css(“-animation play state”,“running”)
}
}); // Ajax调用
使用pg_query_params
或最好使用PDO。不要自己引用。曾经和
您最好使用PDO
(直接的问题是您的引号是错误的。您使用了\'
来转义不需要转义的单引号,因此反斜杠保留在最终查询字符串中。您可以在错误消息中看到这一点)