Multiply mysql entry field and php form

0

Good I would like to do a calculation, between a field in my database and a data entered by a form, I can not get it out ... The database data is internal saved in one table and the other one that the user enters by a form.

    <?php
$usuario = "root";
$password = "";
$servidor = "localhost";
$basededatos = "prestashopprueba";

$conexion = mysqli_connect( $servidor, $usuario, "" ) or die ("No se ha podido conectar al servidor de Base de datos");
$db = mysqli_select_db( $conexion, $basededatos ) or die ( "Upps! Pues va a ser que no se ha podido conectar a la base de datos" );

$consulta = "SELECT * FROM ps_product";
$resultado = mysqli_query( $conexion, $consulta ) or die ( "Algo ha ido mal en la consulta a la base de datos");

echo "<table borde='2'>";
echo "<th>weight</th>";
echo "<th>num1</th>";
echo "<th>*</th>";

$num1=$_GET["num1"];
$multi=0;

// Bucle while que recorre cada registro y muestra cada campo en la tabla.
while ($row = mysqli_fetch_array( $resultado ))
{
$multi =  $num1 * $row['weight'];
echo "<tr>";
  echo "<td>" . $row['weight'] . "</td>";
  echo "<td>" . $num1 . "</td>";
  echo "<td>" . $multi . "</td>";
echo "</tr>";

}
//echo $multi;
echo "</table>"; 


mysqli_close( $conexion );


?>
<head>

</head>
<form method="post" action="new1.php">
Introduce los m²: <input type="number" name="num1" />
<!--Introduce el rendimiento: <input id="num2" type="num2" name="num2" />
Introduce el rendimiento: <input id="num2" type="number" />-->
<br /><br />
<input type="button" value="multiplicar" name="multi" /><br />
</table>
</form>
    
asked by Rafa Alvarez-Ossorio Martin 06.11.2017 в 20:35
source

1 answer

0

You need to correct the input :

<input name="num1" type="number" />

The name tag is the one used to retrieve the values by GET or POST .

Then, your table must be well constructed, otherwise it will not be displayed.

I will imagine that it is a table with three columns. You can adapt it. I removed the Name header because I do not see any relative data.

I'm not sure, but you may need to indicate that you want an associative array, to be able to find the values by their column names, that's why I indicated this in the fetch : MYSQLI_ASSOC

...

echo "<table borde='2'>";
echo "<th>weight</th>";
echo "<th>num1</th>";
echo "<th>*</th>";

$num1=$_GET["num1"];
$multi=0;

// Bucle while que recorre cada registro y muestra cada campo en la tabla.
while ($row = mysqli_fetch_array( $resultado, MYSQLI_ASSOC ))
{
    $multi =  $num1 * $row['weight'];
    echo "<tr>";
      echo "<td>" . $row['weight'] . "</td>";
      echo "<td>" . $num . "</td>";
      echo "<td>" . $multi . "</td>";
    echo "</tr>";

}
//echo $multi;
echo "</table>"; 

mysqli_close( $conexion );
    
answered by 06.11.2017 в 22:03