Vector make a mysql query

Asked

Viewed 305 times

0

Hello guys I’m trying to make a query using vector, but as far as I’ve learned the sql command does not hold the vector the way I’m doing so my result is empty variable.

$idcliente=$_SESSION['id_cliente'];
$loja=$_POST['loja'];
$quant[0]=$_POST['quant'];
$quant[1]=$_POST['quant2'];
$quant[2]=$_POST['quant3'];
$produtos[0]=$_POST['produto'];
$produtos[1]=$_POST['produto2'];
$produtos[2]=$_POST['produto3'];
$tipo=$_POST['ttipo'];
$acrescimo=$_POST['acrescimo'];
$pedido=$_POST['pedido'];
$entrega=$_POST['entrega'];
$montagem=$_POST['montagem'];
$tsmg=$_POST['tsmg'];

$x=mysqli_query($conn,"select*from produtos where descricao='$produtos'");
$prt=mysqli_fetch_array($x);


    $idproduto[]=$prt['id_produto'];
    $codigo[]=$prt['codigo'];
    $descricao[]=$prt['descricao']."";
    $cor[]=$prt['cores'];
    $marca[]=$prt['marca'];
    $valorm[]=$prt['valormontagem'];

  print_r($descricao);

Here is the excerpt that sends the data to the above commands.

    <td><input list='produtos' name='produto'/>
    <datalist id='produtos'><?php
    $sql= mysqli_query($conn,"select descricao from produtos order by descricao");
    while ($resp = mysqli_fetch_array($sql)) {

     $group=$resp['descricao'];
    echo "
   <option value='$group','$cor'>";
   }

   ?> </datalist></td>

I made with 2 products as a test.

   print_r($produtos);
   Array ( [0] => BERCO CHANTILY [1] => BERCO ALEGRIA [2] => )

   print_r($descricao);
   Array ( [0] => )
  • Where is the variable $produto? What is she?

  • It comes from a form. In this field it is reading a table and displaying for selection. I gave a print_r and in that part is exhibiting perfectly.

  • That is to say $produto is an array right? Please put the array format to help you

  • I’m a student, so I’m sorry about that stupid question. Apart from the content I’ve already shown I don’t know how to show you the format of the array, if you can give me a hint I would be very grateful.

  • You can put exactly the impression of print_r($produtos)

  • Done, edited to facilitate the organization.

  • And he wants to go for ex: the products whose descriptions are BERCO CHANTILY and BERCO ALEGRIA right?

  • Right friend exactly that.

  • See if this below will work

  • 1

    I withdrew the answer, since it wasn’t helping, basically corrected foreach($produtos as $prod) {&#xA; $query .= " descricao='$prod' OR";&#xA;}&#xA;$query = rtrim($query, ' OR');, of AND for OR. If I can’t do it I don’t see what else

  • 1

    Your answer was right @Miguel. Some detail I didn’t see either that didn’t work out.

  • Exactly did the tests and worked perfectly thanks to all who helped me learned something new and I am very grateful.

  • 1

    'Cause I tested it here and it was also @Andreicoelho, but it was being so bad accepted that I took it out thinking I was making a huge mistake

  • Relax when you corrected your remarks worked perfect.

  • No problem. Entertaining Daniloaraujo should accept @Andreicoelho’s answer that works well and is a solution to the problem

  • Actually @Miguel your answer is more correct. I would like those who have denied it to make some observation.

  • Thanks @Andreicoelho. Maybe they were negative at first, I made some syntax errors by mistake

  • Got it, can you. =)

Show 13 more comments

2 answers

3

Try to do it this way:

 <?php 

// abaixo são enviados 3 produtos

$produtos[0]=$_POST['produto'];
$produtos[1]=$_POST['produto2'];
$produtos[2]=$_POST['produto3'];

// contamos a quantidade

$quantidadeProdutos = count($produtos);

// agora vamos selecionar produto por produto e cada produto faremos a inserção nas arrays

for ($y = 0; $y < $quantidadeProdutos; $y++){

    $produto = $produtos[$y];
    $x=mysqli_query($conn,"select * from produtos where descricao='$produto'");
    while($prt=mysqli_fetch_array($x)){

        $idproduto[]=$prt['id_produto'];
        $codigo[]=$prt['codigo'];
        $descricao[]=$prt['descricao']."";
        $cor[]=$prt['cores'];
        $marca[]=$prt['marca'];
        $valorm[]=$prt['valormontagem'];

    }

}

print_r($descricao);

?>
  • If you can give a hint to improve @Andrei Coelho. There you are making a loop database call, it could be reduced to one for all. I’ll restore my answer just so you see an alternative

  • @Miguel, I have nothing to add. I liked your answer. Hug! =)

  • Thanks @Andrei, for you too

0


Try it like this:

...
$query = "select * from produtos where";
foreach($produtos as $prod) {
    $query .= " descricao='$prod' OR";
}
$query = rtrim($query, ' OR');
$x = mysqli_query($conn, $query);
while ($prt = mysqli_fetch_array($x)) {
    $idproduto[]=$prt['id_produto'];
    $codigo[]=$prt['codigo'];
    $descricao[]=$prt['descricao']."";
    $cor[]=$prt['cores'];
    $marca[]=$prt['marca'];
    $valorm[]=$prt['valormontagem'];
}

print_r($descricao);
  • I made some adjustments because I noticed small typos, but even so this error occurs inside the while Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, Boolean Given in.... / line28

  • corrected. I’m sorry it was a silly syntax error. corrected on the line ... = mysqli_query(... and in that of while (...

  • this is your code with the settings: $query = "select * from products Where"; foreach($products as $Prod) { $query= " Description='$Prod' AND"; } $query = rtrim($query, ' AND'); $x = mysqli_query($Conn,$query); while($prt=mysqli_fetch_array($x)){ $idproduct[]=$prt['id_product']; $codigo[]=$prt['codigo']; $Description[]=$prt['Description']; $color[]=$prt['colors']; $brand[]=$prt['brand']; $valorm[]=$prt['valuing']; }

  • Unfortunately friend, I noticed the mistakes in the code and I made the corrections. I even removed .= for = but even so I have a reported flaw within the while, that by the way in the several times that I tried to make the code work I had a few times the same condition.

  • But it’s supposed to stay that way .=. serves to increment text to the initial query

  • if I leave him he makes the command but does not give me the print_r. leaving the page completely empty.

  • Shows no error? try doing echo $query before the while and echo $prt['descricao']; within the while. What it prints echo $query?

  • I did in several places and before the while I return the following echo select * from products Where Description='BERCO 8020' AND Description='BERCO ALEGRIA'

  • And inside the while nothing? Maybe it is because there is no line to be returned, this query is right. There is no error there

  • But wait a minute... Now I’m thinking about something, how do you want there to be a product with two different descriptions?? It is not possible I will change the AND for OR. Corrected

  • unfortunately in the while nothing happens. I did along with the while or msqli_print error(). replicated the same error endlessly.

  • Already put with this correction? What is the same error? See that I changed the code above

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.