lundi 13 avril 2015

php: get option select value by mysql

I'm sorry for my english. I hope you to understand me. I'm trying to have an altas.php file for various html forms . so, I have this code in a html form with many other 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>


For the other hand, if I make this:



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


I have no results for categoria. Why not? I should have got the value, wich it's the categorias's id.


And something else: 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 passed with $_GET. This code make a new row for each $campo, of course. Any idea? The most of the times, I find the answer to my questions, but this time, I'd try some suggestions, and nothing works.


Many thanks,


Aucun commentaire:

Enregistrer un commentaire