使用复选框从表中检索信息

时间:2014-05-20 10:29:30

标签: php mysql

我使用复选框将产品存储在名为products的表列中。它们都存储在单元格中并用逗号分隔。 现在我正在尝试从表中检索信息。如果我检查更多产品,它将显示所有拥有产品的用户。所有用户产品都保存在一个单元格中,并使用逗号分隔前产品(10,50,90,160)

html code
<input type="checkbox" name="proizvodi[]" value="10"><b>product1<b/> <br/> <br/>
<input type="checkbox" name="proizvodi[]" value="50"><b>product2<b/> <br/> <hr/>
<input type="checkbox" name="proizvodi[]" value="100"><b>product3</b> <br/>
<input type="checkbox" name="proizvodi[]" value="150"><b>product4</b> <br/>

php code
$variable=$_POST['proizvodi'];

print_r($_POST['proizvodi']);

var_dump ($variable); 

$upit= "SELECT * FROM `clanovi` WHERE FIND_IN_SET($variable[0],PROIZVODI)"; 

if(sizeof($variable)>1)
{
        for($i=1; $i<sizeof($variable); $i++)
        {
        $upit.=" OR FIND_IN_SET($variable[$i],PROIZVODI) ";
        }
}
print($upit);
$result=mysqli_query($con,$upit);

var_dump($upit);
?>
<?
    echo "<table border='5'>
    <tr>
    <th> </th>
    <th>ID</th>
    <th>NAZIV</th>
    <th>ADRESA</th>
    <th>DRZAVA</th>
    <th>GRAD</th>
    <th>EMAIL</th>
    <th>WEB</th>
    <th>TEL </th>
    <th>KONTAKT </th>
    <th>MOBITEL </th>
    <th>PROIZVODI </th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {

    echo "<tr>";
    echo '<td><input type="checkbox" name="ticked[]" value="' . $row['EMAIL'] . '"></td>';
    echo "<td>" . $row['ID'] . "</td>"; 
    echo "<td>" . $row['NAZIV'] . "</td>";
    echo "<td>" . $row['ADRESA'] . "</td>";
    echo "<td>" . $row['DRZAVA'] . "</td>";
    echo "<td>" . $row['GRAD'] . "</td>";
    echo "<td>" . $row['EMAIL'] . "</td>";
    echo "<td>" . $row['WEB'] . "</td>";
    echo "<td>" . $row['TEL1'] . "</td>"; 
    echo "<td>" . $row['KONTAKT-OSOBA'] . "</td>";
    echo "<td>" . $row['MOBITEL'] . "</td>";
    echo "<td>" . $row['PROIZVODI'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";

// PRINT RESULT

Array ( [0] => 10 [1] => 50 [2] => 100 [3] => 101 )

array
  0 => string '10' (length=2)
  1 => string '50' (length=2)
  2 => string '100' (length=3)
  3 => string '101' (length=3)

SELECT * FROM `clanovi` WHERE FIND_IN_SET(10,PROIZVODI) OR FIND_IN_SET(50,PROIZVODI) OR FIND_IN_SET(100,PROIZVODI) OR FIND_IN_SET(101,PROIZVODI) 

我认为问题出现在这里是因为更多的值存储在一个单元格中并用,...分隔,并且我总是看单元格中的特定值

感谢任何帮助或建议

4 个答案:

答案 0 :(得分:0)

对每个发布的proizvodi值使用Mysql的FIND_INSET函数(即使用foreach

SELECT FIND_IN_SET('a','a,b,c');

如,

proizvodi 字段有两行(10,11,12,13)和11,12,13,22

以下代码应该可以使用

<?php

$variable=array(11,13);

$upit= "SELECT * FROM `clanovi` WHERE FIND_IN_SET($variable[0],proizvodi)";

if(sizeof($variable)>1)
{
    for($i=1; $i<sizeof($variable); $i++)
    {
        $upit.=" OR FIND_IN_SET($variable[$i],proizvodi) ";
}
}
echo $upit;
echo "<br />";
$result=mysqli_query($con,$upit);
while ($row = mysql_fetch_array($result)) {
echo "<pre>";
print_r($row);
}


//
?>

上面的代码将输出

Array
(
    [0] => 1
    [id] => 1
    [1] => 10,11,12,13
    [proizvodi] => 10,11,12,13
)

Array
(
    [0] => 2
    [id] => 2
    [1] => 11,12,13,22
    [proizvodi] => 11,12,13,22
)


$variable=array(11,13);//here I hard coded but it will be posted data(checkboxes value)

enter image description here

答案 1 :(得分:0)

像这样使用MySQL的FIND_IN_SET字符串函数

SELECT * FROM `clanovi` WHERE 
FIND_IN_SET($variable[1],PROIZVODI) > 0 AND 
FIND_IN_SET($variable[2],PROIZVODI) > 0 ...

了解详情:MySQL Reference

答案 2 :(得分:0)

此代码适用于我的情况。 Iam发布以防其他人在将来遇到同样的问题

$variable=$_POST['proizvodi'];

print_r($variable);

var_dump ($variable); 
//SELECT * FROM CLANOVI WHERE OR PROIZVODI LIKE '%50%';

$upit= "SELECT * FROM `clanovi` WHERE PROIZVODI LIKE '%$variable[0]%'"; 

if(sizeof($variable)>1)
{
        for($i=1; $i<sizeof($variable); $i++)
        {
            $upit.=" OR PROIZVODI LIKE '%$variable[$i]%' ";
        }
}

我正在使用implode函数来存储产品,然后我再次使用复选框来检索数据。 FIND_IN_SET函数不起作用它只显示第一个值。

答案 3 :(得分:-1)

使用喜欢 对于ex“SELECT * FROM clanovi WHERE PROIZVODI喜欢'%$ variable [0]%'”