对pg_query()有困难;在PHP中 [英] Having difficulties with pg_query(); in php

查看:94
本文介绍了对pg_query()有困难;在PHP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我在PHP中使用Postgres。我正在编写一个界面,用户可以在其中填写用户,以便将其添加到数据库中用户的总记录中。 (非常简单)。



最近我在使查询开始工作时遇到了很多麻烦。逃避似乎一直都是必需的。



我在以下查询中遇到麻烦;

  $ query = pg_query($ dbconnection, INSERT INTO client('clientid','name','ipaddress','status')VALUES('$ clientid','$ name' ,'$ ipaddress','$ status'); 

这不起作用。我猜



如果没有,我该如何准确地将这个查询转换为一个第三方工具?



可用吗?

解决方案

使用 pg_query_params()处理转义并避免SQL注入:

  $ query = pg_query_params(
$ dbconnection,
'插入客户端(clientid,name,ipaddress,status)VALUES($ 1,$ 2,$ 3,$ 4);',//占位符
数组($ clientid,$ name,$ ipaddress,$ stat我们)//内容
);


Currently I am using Postgres with PHP. I'm writing an interface where a user can fill in a user, so it gets added to the total record of users in the database. (Very simple).

Recently I've been having alot of trouble getting the queries to work. Escaping seems to be required all the time. Just the query itself between double quotes isnt enough anymore.

I'm having trouble with the following query;

$query = pg_query($dbconnection,"INSERT INTO clients('clientid', 'name', 'ipaddress',     'status') VALUES ('$clientid', '$name', '$ipaddress', '$status' ");

This doesn't work. I guess there's more escaping needed. Is there any third party tool available to check if these postgres syntaxes are valid or not?

If not, how can I exactly turn this query into a useable one?

解决方案

Use pg_query_params() to handle escaping and avoid SQL injection:

$query = pg_query_params(
  $dbconnection,
  'INSERT INTO clients(clientid, name, ipaddress,status) VALUES ($1, $2, $3, $4);', // placeholders
   array($clientid, $name, $ipaddress, $status) // content
);

这篇关于对pg_query()有困难;在PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆