简体   繁体   English

获取PDO :: FETCH_ASSOC多个复选框

[英]fetch PDO::FETCH_ASSOC multiple checkboxes

usually i help people with whatever they need, this time i'm asking for your help. 通常我会帮助人们提供他们需要的东西,这次我要求你的帮助。

i'm trying to get a specific row from my database after preforming multiple checkbox select i spend 50 hours on that and i couldn't manage to do that. 我正在尝试从预先形成多个复选框后从我的数据库中获取一个特定的行选择我花了50个小时,我无法做到这一点。 each time i'm changing something in my code i get a different ERROR. 每次我在我的代码中更改某些内容时,我都会遇到不同的错误。 i was looking for an answer in every HTML page that exist on the INTERNET ! 我正在寻找互联网上存在的每个HTML页面的答案!

please show me the light.. 请告诉我灯光..

here is a part of my form.... value means "size" of the toy 这是我的形式的一部分....价值意味着玩具的“大小”

    <div class=""><input type="checkbox" name="toys[]" value="6X2" /><label></label></div>
    <div class=""><input type="checkbox" name="toys[]" value="4X3" /><label></label></div>
    <div class=""><input type="checkbox" name="toys[]" value="8X2.5" /><label></label></div></strike>

here is the PHP code... 这是PHP代码......

  if (isset($_POST['toys'])) {

     foreach($_POST['toys'] as $each_check) {

      }
  }

  $query = $db->query = 'SELECT * FROM `toys` WHERE SIZE = '.$each_check;   


echo "<table>";
    echo "<tr>
            <th>ratio</th>
            <th>size</th>
            <th>built</th>
            <th>description</th>            
          </tr>";



while ($row = $query->fetch(PDO::FETCH_ASSOC)) 

        echo "<tr><td>" . $row['ratio'] .
            "</td><td>" . $row['size'] .
            "</td><td>" . $row['built'] .
            "</td><td>" . $row['description'] .
            "</td></tr>";
echo "</table>";        

This is so very far from being valid: 这远非有效:

if (isset($_POST['toys'])) {

    foreach($_POST['toys'] as $each_check) {

    }
}

$query = $db->query = 'SELECT * FROM `toys` WHERE SIZE = '.$each_check;

More like: 更像:

if (isset($_POST['toys'])) {
    foreach($_POST['toys'] as $each_check) {
        $query = $db->query("SELECT * FROM `toys` WHERE SIZE = '".$each_check."'");
    }
}

But should be more like: 应该更像是:

if (isset($_POST['toys'])) {
    $query = 'SELECT * FROM `toys` WHERE SIZE = ?';
    $sth = $db->prepare($query);
    foreach($_POST['toys'] as $each_check) {
        if( ! $sth->execute(array($each_check)) ) {
            die('MySQL Error: ' . var_export($sth->error_info(), TRUE);
        }
        while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
            // code here
        }
    }
}

You're assigning $db->query instead of using it as a function. 您正在分配$db->query而不是将其用作函数。 Change your query line to this: 将您的查询行更改为:

$query = $db->prepare('SELECT * FROM `toys` WHERE SIZE = :size');
$query->bindValue(':size',$each_check);
$query->execute();

Also, you're going through $_POST['toys'], but not assigning it to any value. 此外,您正在浏览$ _POST ['toys'],但不会将其分配给任何值。 I'm guessing you want to add all of your query and table code within the foreach. 我猜你想要在foreach中添加所有查询和表格代码。

if (isset($_POST['toys'])) {
    foreach($_POST['toys'] as $each_check) {
        // put everything else here
    }
}

I want to suggest that you use MySQL's IN (...) clause in your WHERE condition to retrieve all the rows with matching 'size' in just 1 query: 我想建议你在WHERE条件中使用MySQL的IN(...)子句来检索只有1个查询中匹配'size'的所有行:

SELECT * FROM toys WHERE size IN ( $chosenSizes )

To get the list of sizes, use PHP's implode function: 要获取大小列表,请使用PHP的implode函数:

$chosenSizes = implode(', ', $_POST['toys']);

You can then use PDO's fetchAll to fetch all rows into a result array. 然后,您可以使用PDO的fetchAll将所有行提取到结果数组中。

$resultRows = $sth->fetchAll();

Note: Only use this method when you are quite certain that the result arrays is not too big! 注意:只有在您确定结果数组不是太大时才使用此方法!

Hagay, the following should work for you: Hagay,以下应该适合你:

$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'my_name', 'my_pass');

if (isset($_POST['toys'])) {
    $sizes = implode(', ', array_map(array($pdo, 'quote'), $_POST['toys']));

    $sql = "SELECT * FROM toys WHERE size IN (" . $sizes . ")";

    echo '<table>', PHP_EOL;
    echo '<tr><th>ratio</th><th>size</th></tr>', PHP_EOL;

    foreach( $pdo->query($sql) as $row ) {
       echo '<tr><td>', $row['ratio'], '</td><td?', $row['size'],  '</td></tr>', PHP_EOL;
    }

    echo '</table>', PHP_EOL;
}

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

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