Adding multiple items

2

I would like to know how I can make this my code work to insert data in MySQL multiple.

In the first file apply a repetition of 5 items, the problem is that I do not know if I made the right 2 file that is not inserting the data in SQL. I need in the case that when doing the saving apply the saving of all the data placed in the 5 fields put in formulario.php so that they are sent properly taking all their parameters and inserting them in the SQL when they are passed to the enviar.php

File formulario.php

<form id="form1" name="form1" method="post" action="/enviar.php">
<?php

$valor = 5;

for ($repeticao = 1; $repeticao <= $valor; $repeticao++) { ?>


  <label>
    <input name="numero2" type="text" id="numero" value="" size="10" />
  </label>
  <label>

  </label>
<select name="cat" style="width:150px;" id="input">
<option value="--- Escolha ---" selected="selected">----- Escolha -----</option>
<?
include("/../config.php");
$select = mysql_query("SELECT * FROM 'medias_subcategoria' WHERE 'cat'='1' ORDER BY 'nome'");
while($dados = mysql_fetch_array($select)){ ?>
<option value='<?php $dados["id"];?>><?php $dados["nome"];?></option>
<? } ?>
</select>
<? }  ?>
<input type="submit" name="button" id="button" value="Enviar" />
</form>

File enviar.php

<?php 
include("/../config.php");

$cat = $_POST["cat"];


foreach($cat as $cod => $value){

$select = mysql_query("SELECT * FROM 'medias_subcategoria' WHERE 'id'='".$cod."'");
$subcat = mysql_fetch_array($select);

$cat = $subcat['cat'];
$subcat = $subcat['id'];
$medias_categoria_url = $subcat['medias_categoria_url'];
$medias_subcategoria_url = $subcat['medias_subcategoria_url'];

$sql = 
      "insert into medias_medias (cat,subcat,medias_categoria_url,medias_subcategoria_url) 
       values('',
              '$cat[$cod]',
              '$subcat[$cod]',
              '$medias_categoria_url[$cod]',
              '$medias_subcategoria_url[$cod]')";
$consulta = mysql_query($sql) or die(mysql_error());
if($consulta) {
echo "<br/><center><div id=\"Aviso_ok\">Media Cadastradq com Sucesso</div></center><br/>";
}else{
echo "<br/><center><div id=\"Aviso_erro\">Erro ao Cadastrar a Media</div></center><br/>";}
} ?>
    
asked by anonymous 08.04.2015 / 07:54

1 answer

2

The problem is that you are using the same name for all the check boxes. You may want to use name different, or an array .

Example with array :

<select name="categorias[]">

And then yes, you can go to category category:

$categorias = $_POST["categorias"];   

foreach( $categorias as $key => $catID ) 
{
    echo "O value da categoria selecionada na box $key é $catID";
}
    
08.04.2015 / 10:39