4
I have a form with a field multiple select
:
<form action="processa-chose.php" method="post">
<select id="selecionar" name="fornecedor[]"
data-placeholder="Fabricantes"
style="width:350px;"
multiple class="chosen-select" tabindex="8">
<option>XXX</option>
<option>XXX</option>
<option>XXX</option>
<option>XXX</option>
<option>XXX</option>
<option>XXX</option>
<option>XXX</option>
<option>XXX</option>
</select>
<input type="submit" value="envie" />
And I want to select in the table provider column the data belonging to the array and then bring the column with the Ids.
I tried this script and it didn’t work:
<?php
$conect = mysqli_connect("localhost","root","","XXXXXXXXX");
$fornecedor = $_POST['fornecedor'];
$dados = implode(",",$fornecedor);
$sql = "SELECT * FROM fornecedores WHERE fornecedor IN ('$dados')";
$result = mysqli_query($conect,$sql);
while($row = mysqli_fetch_assoc($result)){
echo $row["id"];
}
?>
This is the table:
CREATE TABLE IF NOT EXISTS `fornecedores` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`fornecedor` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
-- Extraindo dados da tabela `fornecedores`
--
INSERT INTO `fornecedores` (`id`, `fornecedor`) VALUES
(1, 'XXX'),
(2, 'XXX'),
(3, 'XXX'),
(4, 'XXX'),
(5, 'XXX'),
(6, 'XXX'),
(7, 'XXX'),
(8, 'XXX');
Your options don’t have
value
! There’s a good reason...– bfavaretto
It would be the lolusion, but I can’t use the array inside the SELECT,
– user14579
Your query is correct, just enter the values. For example:
<option value="1">bla</option>
.– bfavaretto
@bfavaretto if the ID is used as option value, clause
where
will need to be amended as well:SELECT * FROM fornecedores WHERE id IN ('$dados')
– gmsantos
True, @gmsantos, I hadn’t noticed. I would actually use the id instead of the name.
– bfavaretto
True! with id worked! I just don’t know why it doesn’t work with strings...
– user14579
It doesn’t work because you’re riding
IN('foo, bar')
instead ofIN('foo', 'bar')
.– bfavaretto