Data being multiplied in the table

Asked

Viewed 91 times

0

I have a register of recipes and this register has a button that clones the fields "amount of ingredients" and the "combo box of ingredients". If I add only a quantity and ingredient, the registration is saved correctly, but not 2 or more ingredients. Any suggestions?

Inputs:

<div id= "clonardiv">
    <div class="form-group">
<label class="col-md-4 control-label" for="idMedida">Quantidade:</label>  
<div class="col-md-5">
<input id="idMedida" name="idMedida[]" type="text" placeholder="Insira aqui a quantidade de ingredientes necessários" class="form-control input-md" required="">
 </div>
</div>

    <div class= "form-group">
        <label for="ingredientes" class="col-md-4 control-label">Escolha os ingredientes</label>
        <select name="ingredientes[]" id="ingredientes" class="col-md-4 control-label" required="1">
            <option>Selecione...</option>
            <?php
            while($dados3 = mysql_fetch_array($q1)) {   
            ?>
            <option value="<?= $dados3['id_ingredientes'] ?>">
                <?= $dados3['nome'] ?>
            </option>
            <?php
            }
            ?>
        </select>
    </div>
    </div>
        <div id= "clonar" ></div>

    <div class="form-group">
    <div class="col-md-4">
    <input type="button" value="+" onclick="clonar()"; class="btn btn-outlined btn-success" />
    </div>
    </div>
    <br><br><br>

PHP:

<html>
<head>
</head>
<body>
<?php
$titulo= $_POST["idTituloReceita"];
$imagem= $_FILES['imagem']['name'];
$culinaria= $_POST["culinaria"];
$tempopreparo= $_POST["idTempopreparo"];
$porcao=$_POST["idServePorcao"];
session_start();
$emailr = $_SESSION['emailLogin'];
$descricao =$_POST["idPreparo"]; 
$video = $_POST["idVideo"];

$con = mysqli_connect("localhost", "root", "usbw", "culinaria");

foreach($_POST["idMedida"] as $medida) {

foreach ($_POST["ingredientes"] as $ingredientes){

$sql="INSERT INTO receitas(email,id_tipoculinaria,serve_porcao,tempo_preparo,titulo ,imagem ,video) 
VALUES ('$emailr','$culinaria','$porcao','$tempopreparo','$titulo','$imagem','$video')";

$query = $con->query($sql) or die( mysqli_error( $con ) );

$id= $con->insert_id;

//echo "$id";

$sql="INSERT INTO ingredientes_da_receita(id_receitas, id_ingredientes, quantidade, descricao) 
VALUES ('$id' ,".$ingredientes.",".$medida.",'$descricao')";

$query = $con->query($sql) or die( mysqli_error( $con ) );

echo "<script>alert('Receita Cadastrada com sucesso');window.location='cadastroreceita.php' </script>"; 
}
}
mysqli_close($con);

?>

</body>
</html>

controleclone.js:

function clonar() {
    var destino = document.getElementById("clonar");
    var novadiv = document.createElement("div");
    var conteudo = document.getElementById("clonardiv");
    novadiv.innerHTML = conteudo.innerHTML;
    destino.appendChild(novadiv);
}
  • can post this clone function()?

  • @Clear leocaracciolo, this in the body of the question.

  • boy I have to leave now but in your select is missing Multiple see this tutorial http://www.criaweb.com/artigos/180.php

  • @Leocaracciolo I will see, thank you very much

  • I realized what makes the function clone, so Multiple in select has nothing to do with

  • @Leocaracciolo some other tip?

  • Some things I didn’t understand. Why a foreach in the recipes table? In this table there is an auto increment field?

  • @Leocaracciolo Cara, was that the problem did not duplicate more but the problem is now in quantity, it does not update correctly. Example: 300 g of rice and 500 g of meat. In the table is something like this: 300 g of rice and meat. What could be?

  • chat

Show 5 more comments

1 answer

1


No need for two foreach.

Use array_combine to combine the two arrays $idMedida $ingredientes and make a foreach to insert into the database the keys and values of the resulting array

$titulo= $_POST["idTituloReceita"]; 
$imagem= $_FILES['imagem']['name']; 
$culinaria= $_POST["culinaria"]; 
$tempopreparo= $_POST["idTempopreparo"]; 
$porcao=$_POST["idServePorcao"]; 
session_start(); 
$emailr = $_SESSION['emailLogin']; 
$descricao =$_POST["idPreparo"]; 
$video = $_POST["idVideo"]; 

$con = mysqli_connect("localhost", "root", "usbw", "culinaria"); 

$sql="INSERT INTO receitas(email,id_tipoculinaria,serve_porcao,tempo_preparo,titulo ,imagem ,video) 
VALUES ('$emailr','$culinaria','$porcao','$tempopreparo','$titulo','$imagem','$video')"; 

$query = $con->query($sql) or die( mysqli_error( $con ) ); 

$idMedida=$_POST["idMedida"]; 
$ingredientes = $_POST["ingredientes"]; 

$combArray=array_combine($ingredientes,$idMedida); 

$id= $con->insert_id; 

foreach($combArray as $key => $value) 
{ 
   $sql2="INSERT INTO ingredientes_da_receita(id_receitas, id_ingredientes, quantidade, descricao) VALUES ('$id','$key','$value','$descricao')"; 
   $query = $con->query($sql2) or die( mysqli_error( $con ) ); 
} 

mysqli_close($con);
  • Thank you very much, that’s right!!!!

Browser other questions tagged

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