mysql query with php form variable

0

Error:

Part of the PHP code:

//Habiendo conexión, la base de datos puede no existir o ...
mysqli_select_db ($conexion, $basedatos) or die ("No se encuentra la base de datos");
mysqli_set_charset ($conexion,"utf-8");

//Haremos una consulta a la base de datos para mostrar el contenido
$consulta = "select cod, nombre from familia";
$resultado = mysqli_query($conexion,$consulta);

echo "<h1>Consulta noticias</h1>";
echo "<br>";
echo "<form action='mostrar.php' method='post'>";
  echo "<select name='producto'>";
while ($fila = mysqli_fetch_assoc($resultado)){
    echo "<option value='" . $fila['cod'] . "'>" . $fila['nombre'] . " </option>";
}
echo "</select><p><input type='submit' name='Enviar'/></p></form>";

Selection of the product in the database:

Part of the PHP code to display:

//Habiendo conexión, la base de datos puede no existir o ...

mysqli_select_db ($conexion, $basedatos) or die ("No se encuentra la base de datos");
mysqli_set_charset ($conexion,"utf-8");
$fam = $_POST['producto'];
echo "$fam";
//Haremos una consulta a la base de datos para mostrar el contenido
// SOSPECHO QUE EL ERROR ESTÁ EN LA VARIABLE $fam
$consulta = 'select cod, nombre_corto, pvp from productos where ' . $fam . '=familia';
$resultado = mysqli_query($conexion,$consulta);
while ($fila = mysqli_fetch_assoc($resultado)){
    print ("El codigo es " . $fila['cod'] . " el nombre corto es " . $fila['nombre_corto'] . " el pvp es " . $fila['pvp'] ."<br>");
}
    
asked by JTsuki 04.11.2017 в 19:21
source

3 answers

0

Try mysqli_fetch_array however the error is in the query:

Here is an example.

$consulta = "select cod, nombre_corto, pvp FROM productos where $fam ='familia'";
$resultado = mysqli_query($conexion,$consulta);
    $row = mysqli_fetch_array($resultado,MYSQLI_ASSOC);

    foreach ($row as $rows){
        echo "El codigo es " . $rows['cod'] . " el nombre corto es " . $rows['nombre_corto'] . " el pvp es " . $rows['pvp'] ."<br>";
    }
  

When you use double quotes "" it is not necessary to concatenate the variables.

In the second instance, I recommend using PDO, for security, fluency, etc.

    
answered by 04.11.2017 в 19:37
0

Modified the following lines:

$consulta = "select cod, nombre_corto, pvp from producto where familia='$fam'";
$resultado = mysqli_query($conexion,$consulta) or die(mysqli_error($conexion));

Adding "or die (mysqli_error ($ connection));" I could visualize the error and solve it.

Thanks for the answers.

    
answered by 04.11.2017 в 22:34
-2

family has to go in quotes herself is also saying:

$consulta = 'select cod, nombre_corto, pvp from productos where ' . $fam . '="familia"';

But I personally like it better without concatenating everything inside a string of double quotes and for other strings or variables inside I use the simple ones, that is:

$consulta = "select cod, nombre_corto, pvp from productos where $fam = 'familia'";

And I also add that the statement set_charset is wrong, utf8 is unscripted, that is:

mysqli_set_charset ($conexion,"utf8");
    
answered by 04.11.2017 в 20:00