我在使用pgsql查询时遇到了两个相同的PHP问题
我正在使用下拉菜单创建用户生成pgsql查询。它工作完美,但是,我现在正试图将其转换为多个选择。我遇到了两个不同的问题。当我向它添加多个时,它不再是一个下拉列表,它的行为更像一个滚动滚轮。注意,这些选项由初始db查询填充我在使用pgsql查询时遇到了两个相同的PHP问题,php,html,postgresql,Php,Html,Postgresql,我正在使用下拉菜单创建用户生成pgsql查询。它工作完美,但是,我现在正试图将其转换为多个选择。我遇到了两个不同的问题。当我向它添加多个时,它不再是一个下拉列表,它的行为更像一个滚动滚轮。注意,这些选项由初始db查询填充 <select multiple name="userSite" class="form-dropdown validate[required]"> -虽然我可以选择多个选项,但查询只返回第一个选定的选项,而不是所有选定选项的结果 ini_set('error
<select multiple name="userSite" class="form-dropdown validate[required]">
-虽然我可以选择多个选项,但查询只返回第一个选定的选项,而不是所有选定选项的结果
ini_set('error_reporting', E_ALL);
ini_set("display_errors", 1);
$site= $_POST["userSite"];
$datea= $_POST["userDatea"];
$table= $_POST["userTable"];
$datez= $_POST["userDatez"];
// You need to do all of this if and only if this is a post request
// Also this method of detecting a post request is more consistent
if( !empty($_SERVER['REQUEST_METHOD']) && (strcasecmp($_SERVER['REQUEST_METHOD'], 'post')===0) ) {
// Create connection
$conn = pg_connect("host=xxxxxxxxxx port=xxxx dbname=db user=xxx password=mypassword");
// Check connection
if (!$conn) {
echo "Did not connect.\n";
exit;
}
$result = pg_query($conn,
"SELECT *
FROM
db.$table
WHERE
$table.site_id = '$site' AND
$table.created_on BETWEEN '$datea' AND '$datez' AND
$table.soft_delete_id = '0';");
if (!$result) {
echo "Query failed.\n";
exit;
}
$num_fields = pg_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++)
{
$headers[] = pg_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result)
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="customreport.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = pg_fetch_row($result))
{
fputcsv($fp, array_values($row));
}
die;
}
exit('It works');
}
ini\u集('error\u reporting',E\u ALL);
ini设置(“显示错误”,1);
$site=$\u POST[“用户站点”];
$datea=$\u POST[“userDatea”];
$table=$\u POST[“userTable”];
$datez=$_POST[“userDatez”];
//当且仅当这是一个post请求时,您需要执行所有这些操作
//此外,这种检测post请求的方法更加一致
如果(!empty($服务器['REQUEST\u方法])&&($服务器['REQUEST\u方法],'post')==0)){
//创建连接
$conn=pg_connect(“主机=xxxxxxxxx端口=xxxxx数据库名=db用户=xxx密码=mypassword”);
//检查连接
如果(!$conn){
echo“未连接。\n”;
出口
}
$result=pg_查询($conn,
“选择*
从…起
db.$表
哪里
$table.site_id=“$site”和
$table.created_位于“$datea”和“$datez”之间,并且
$table.soft_delete_id='0';”;
如果(!$result){
echo“查询失败。\n”;
出口
}
$num_fields=pg_num_fields($result);
$headers=array();
对于($i=0;$i<$num_字段;$i++)
{
$headers[]=pg_字段_名称($result,$i);
}
$fp=fopen('php://output","w",;
如果($fp&&$result)
{
标题(“内容类型:文本/csv”);
标题('Content-Disposition:attachment;filename=“customreport.csv”);
标题('Pragma:no cache');
标题('Expires:0');
fputcsv($fp,$headers);
while($row=pg_fetch_row($result))
{
fputcsv($fp,数组_值($row));
}
死亡
}
退出(“它工作”);
}
我认为您需要将[]
添加到select元素的name属性中,在您的示例中,它将是name=“UserSite[]”
。这允许将多个值封装到一个作为$\u POST['UserSite']
访问的$\u POST变量中,该变量将是一个值数组,而不是单个值。根据建议,您需要将name属性设置为name=“UserSite[]”
。但是,这只是问题的一半。另一个问题是数据库查询。你不能用你的查询来搜索多个站点,它会打开你的代码进行SQL注入
请尝试以下方法:
$site= $_POST["userSite"]; // this will be an array.
$datea= $_POST["userDatea"];
$datez= $_POST["userDatez"];
$table= $_POST["userTable"];
// verify that $table is a valid name since it can't be parameterized in the query.
if( $table !== 'my_table'
|| $table !== 'my_other_table' ){
// exit due to possible sql injection.
exit();
}
$params = array();
$params[] = $datea; // $1
$params[] = $datez; // $2
// generate the site query params
// we need a string like this: $3, $4, $5, etc. (one for each site selected)
$site_param_ids = array();
foreach($site as $s){
$params[] = $s;
$site_param_ids[] = '$' . count($params);
}
$site_param_ids = implode(', ', $site_param_ids);
$sql = "SELECT
*
FROM db.$table t
WHERE t.soft_delete_id = '0'
AND t.created_on BETWEEN $1 AND $2
AND t.site_id in ($site_param_ids);";
//
$result = pg_query_params($conn, $sql, $params);
谢谢你的回复。我理解你在做什么,但似乎与“内爆”脱节了。。。我用[]设置了name属性,但在提交post…incompode()[function.incompode]:参数必须是第24Oops行上/home2/rightme1/public_html/networks/gsreports/custom.php中的数组时出现以下错误。我修复了丢失的参数。