ARRAY DATABASE

Asked

Viewed 42 times

2

Personal created a form where I will put the quantity of products and send to the database, except that when I click to send is not saved only saved from one to one, someone could help me I’m trying to create this for days and I’m not getting.

here is the table created, What I need is to send them all at once. Can someone help me? this code below is working perfectly only that sends 1 in 1 need to send all at once.

 <tr>
             <td><input type="text" readonly class="form-control" name="codigo[]" id="inlineFormInputGroup" value="091822"></td>
             <td><input type="text" readonly class="form-control" name="material[]" id="inlineFormInputGroup" value="TOMATE GRAPE 180"></td>
             <td><input type="number" class="form-control" name="quantidade[]" id="inlineFormInputGroup" placeholder="Valor" ></td>
      
 </tr>
 <tr>
             <td><input type="text" readonly class="form-control" name="codigo[]" id="inlineFormInputGroup" value="091822"></td>
             <td><input type="text" readonly class="form-control" name="material[]" id="inlineFormInputGroup" value="TOMATE GRAPE 180"></td>
             <td><input type="number" class="form-control" name="quantidade[]" id="inlineFormInputGroup" placeholder="Valor" ></td>
      
 </tr>[![inserir a descrição da imagem aqui][1]][1]

<?php include_once"conexão.php";?>
<html>
<body>
<?php 

$material = $_POST["material"];
$codigo = $_POST["codigo"];
$quantidade= $_POST["quantidade"];

$conn = mysqli_connect($servidor,$dbusuario,$dbsenha,$dbname);
mysqli_select_db($conn,'$dbname');
$sql = "INSERT INTO tblavitaperdas (material,codigo,quantidade) VALUES ('$material','$codigo','$quantidade')";
if (mysqli_query($conn, $sql)) {
echo "<script>alert('Salvei seus dados !'); window.location = 'perdaslavita.php';</script>";

}else{
 echo "Deu errro: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
</body>
</html>
  • Good night, it’s been a while since I’ve touched php, but when you give the post the data, you need to go through it with a foreach and then enter it into the database, and in this case, you’re entering it once for all I can see.

1 answer

1


In your CODE, MATERIAL and QUANTITY input you can receive multiple values, then you will receive one ARRAY in PHP.

Example of use, using your case:

$material = $_POST["material"];
$codigo = $_POST["codigo"];
$quantidade= $_POST["quantidade"];
$total_codigo = count($codigo);

$conn = mysqli_connect($servidor,$dbusuario,$dbsenha,$dbname);
mysqli_select_db($conn,'$dbname');

for($i = 0; $i < $total_codigo; $i++){
   $sql = "INSERT INTO tblavitaperdas (material,codigo,quantidade) VALUES 
           ('$material[$i]','$codigo[$i]','$quantidade[$i]')";
   
   mysqli_query($conn, $sql)

}

mysqli_close($conn);
  • Very obg guy worked out!

  • 1

    Good, if you can mark as correct answer I thank.

  • Felipe man please could you help me more on an expensive thing ? this change you made is perfect, I’m just not able to send the right way yet, I just opened a new poll there in the feed of questions could help me there again?

  • in my table have several input fields only I’m wanting to pick the ones I want marked amount is greater than 0, in my database records all, summarized just wanted to record how I mark the relevant quantity greater than 0, I was wanting you mark the comets but not getting you helped me very much worth!!

Browser other questions tagged

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