mardi 14 avril 2015

php: get option select value by mysql

I'm trying to have an altas.php file for various html forms. This code is a html form with some mysql columns:



<div><label for="categoria">Categoría principal</label><select type="text" name="categoria">
<?php
$query = "select id, categoria from categorias";
$result = mysqli_query($mysql,$query);
if(!$result) echo 'Muy mal....';
$num_filas = mysqli_num_rows($result);
for ($i = 0; $i < $num_filas; $i++){
$row = mysqli_fetch_array($result);
?>
<option value "<?php echo $row['id']?>"><?php echo $row['categoria']?></option><?php
}
?>
</select></div>


On the other hand, if I make this:



foreach($_POST as $campo => $valor){
echo $campo ." = ". $valor ."\n";
}


I have no data in $row['categoria']. It should return the value, which is the categorias's id.


Another question:


How can I use the "foreach" whith an insert sentence?


For example:



foreach($_POST as $campo => $valor){
$query ="INSERT INTO $tabla ($campo) VALUES ('$valor')";
}


$tabla is sent via $_GET. This code make a new row for each $campo. Any idea?


I have try some suggestions, but nothing works.


Aucun commentaire:

Enregistrer un commentaire