简体   繁体   English

PHP PDO导致“数组”值而不是实际值

[英]PHP PDO resulting in “Array” value rather than actual value

I have created a form which queries a MySQL database and displays results. 我创建了一个查询MySQL数据库并显示结果的表单。 Now I want to be able to filter my results. 现在,我希望能够过滤我的结果。 I have: 我有:

<input type="checkbox" name="multi_cif[]" value="<?php echo $g1 ?>"><font size="1" face="Arial, Helvetica, sans-serif">

In the output which is displaying all results via foreach and the variable $g1 is a MySQL query value (address). 在输出中,该输出通过foreach显示所有结果,变量$ g1是MySQL查询值(地址)。 I want to be able to click these check boxes next to the results so when the user clicks the button labeled "Filter" only the results checked are displayed. 我希望能够单击结果旁边的这些复选框,以便当用户单击标有“过滤器”的按钮时,仅显示选中的结果。

So far my code is as follows: 到目前为止,我的代码如下:

<?PHP
if (isset($_POST['Submit2']))
{
$stmt = $dbh->prepare('SELECT * FROM CIF WHERE address LIKE ?');
$stmt->execute(array("%$_POST[multi_cif]%"));
//$stmt->execute(array(":term" => "%" . $_POST["multi_cif"] . "%"));
//$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
/*while ($results = $stmt->fetch())  //WILL UNCOMENT AND ADD OR LIKE AFTER SINGLE QUERY WORKING
{
    echo $results['address'];
    echo $results['alternativeid'];
}*/
print_r($_POST);
$results = $stmt->fetch();
echo $results['address'];
echo $results['alternativeid'];
}
?>

The commented out stuff is other things I have tried. 评论出来的东西是我尝试过的其他东西。 I am very close to my results. 我非常接近我的结果。 The results of the following code ends in: 以下代码的结果以:

 [multi_cif] => Array ( [0] => test.13ann.com [1] => testfortestltd444557.com.tw ) ) coralarray.ruhttp://mirror3.malwaredomains.com/files/domains.txt

So clearly "Array" is being passed as a value instead of the desired address assigned to multi[]. 显然,“数组”是作为值而不是分配给multi []的所需地址传递的。 Could someone please explain why this is and help me fix it? 有人可以解释为什么会这样,并帮助我解决吗? I am new to PDO as of yesterday but have chosen to use it and re-work my other statements to implement prepared statements instead of dynamically building them. 截止到昨天,我是PDO的新手,但是选择使用它并重新处理其他语句以实现准备好的语句,而不是动态构建它们。 Thanks in advance! 提前致谢!

Edited: I took some of Brad's advice but kept the statement without the additional "OR address LIKE ?" 编辑:我接受了布拉德的一些建议,但保留了该声明,未添加其他“或地址类似?”。 as right now I am only clicking one checkbox but still getting "Array" instead of "test.13.ann.com" Once I figure out why "Array" vs. value I will add the additional OR --thanks Brad for pointing out! 现在,我只单击一个复选框,但仍然得到“ Array”而不是“ test.13.ann.com”。一旦弄清为什么“ Array”与值相对,我将添加额外的OR-感谢Brad指出!

Try accessing the first array value, rather than the outer array - 尝试访问第一个数组值,而不是外部数组-

$stmt->execute(array("%{$_POST['multi_cif'][0]}%"));

to do it dynamically you could try 动态地做,你可以尝试

// create n number of placeholders based off number of $_POST['multi_cif']
$place_holders = implode(' OR', array_fill(0, count($_POST['multi_cif']), ' address LIKE ?'));

// create n number of values based off number of $_POST['multi_cif']
$values = '"%'. implode('%","%', $_POST['multi_cif']).'%"';
// explode the values into an array
$values = explode(',',$values);

$stmt = $dbh->prepare("SELECT * FROM CIF WHERE $place_holders");
$stmt->execute(array($values));

You were nearly there. 你快到了 You didn't concatenate your parameter correctly. 您没有正确连接参数。

Try 尝试

<?php
if (isset($_POST['Submit2']))
{
   $term = "%" . $_POST["multi_cif"] . "%";
   $stmt = $dbh->prepare('SELECT * FROM CIF WHERE address LIKE ?');
   $stmt->execute(array($term));
   $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
   while ($rows = $stmt->fetch())  
   {
       echo $results['address'];
       echo $results['alternativeid'];
   }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM