Addition of multiple items

Asked

Viewed 180 times

2

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

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

Filing cabinet 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>

Filing cabinet 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/>";}
} ?>
  • 1

    I don’t have time to answer the question itself, but just watching quickly, at least change mysql_query to mysqli_query becausemysql_* is obsolete. Another point is to mix layers.. Try to understand the basics of MVC.

  • Rodrigo reversed the question to stay the original so that the answer is valid.

2 answers

3


The problem is you’re using the same name for all check boxes. You should use name different, or a array.

Example with array:

<select name="categorias[]">

And then you can go category by category like this:

$categorias = $_POST["categorias"];   

foreach( $categorias as $key => $catID ) 
{
    echo "O value da categoria selecionada na box $key é $catID";
}
  • In the case that parameter cat[ ] does what and if it changes this way by putting it in name in the first code will run the Insert properly ? Could be a little clearer ?

  • Wait I’ll give you an example

  • in the case before the.php form code has one that you in the case put the number of times you want the loop to repeat to make several items appear in the form here set to 5 more here instead of $value = 5 has in the case $_POST["number"]

  • Get this straight @Rodrigo

  • @Rodrigo this is not important. If everyone has name="categorias[]" all will be added in order to the array.

  • I updated the code see if and the right result if no color bother and I appreciate the help. I’m still beginner and have a lot to learn.

  • 1

    Thanks I got to do what I wanted.

Show 2 more comments

-1

$categorias = $_POST["categorias"];   

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

Browser other questions tagged

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