Insert data into the database with foreach

Asked

Viewed 74 times

0

I have an array that receives data from a session and I am trying to insert it into the database using foreach but nothing happens. What may be wrong?

Follow an example of the array:

array(3) {
  [0]=>
  array(4) {
    ["id_produto"]=>
    int(1)
    ["quantidade"]=>
    int(1)
    ["preco"]=>
    string(5) "10,00"
    ["total"]=>
    string(5) "10,00"
  }
  [1]=>
  array(4) {
    ["id_produto"]=>
    int(2)
    ["quantidade"]=>
    int(2)
    ["preco"]=>
    string(5) "45,50"
    ["total"]=>
    string(5) "91,00"
  }
}

Follow an example of my code:

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

foreach($_SESSION['dados'] as $produto){
    require("conexao.php");
    mysqli_query($conexao, "INSERT INTO tb_pedido (
    id,id_produto,
    id_usuario,
    id_categoria,
    quantidade,
    preco,
    total,
    data_cadastro,
    status) 
    VALUES (
    NULL,
    '".$produto['id_produto']."', 
    '0.0', '0.0', 
    '".$produto['quantidade']."', 
    '".$produto['preco']."', 
    '".$produto['total']."', 
    NOW(), 
    '0')") or die(mysql_error());
}

As my bank is: inserir a descrição da imagem aqui

1 answer

1


I can not comment yet, so I will reply, I see that:

1 - ID is auto increment, do not pass in query, remove id, and value; 2 - It is passing float with comma, must be point.

I believe it’s a start, a solution

  • It was exactly the question 2. FLOAT . Thank you. Sometimes we do not pay attention to small things hehe.

  • It happens a lot! Sometimes we lose hours and it ends with a "Was that it?!" does not get angry or relieves.. hehe

Browser other questions tagged

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