Hi I'm working on a project where I require PHP and Mysql. I have a problem I haven't been able to solve, I obtain user input via HTML and PHP makes a query to mysql. I arrange data on a Select posting into it the client_id
, and the Name of the client. However on the next page I'm trying to obtain data from the select, but only the client_id
, not the name, so I can insert data using the id.
Here is my code in advance.
<?php
$con=mysqli_connect("localhost","usr","pw","table");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$nombre= $_POST["nombre"];
$result = mysqli_query($con,"SELECT Clientes.cliente_id, Clientes.Nombre FROM Clientes WHERE INSTR(Nombre,'$nombre')");
echo "<form action='sumadb2.php' method='post'>";
echo "<select name='nombre'>";
while($row = mysqli_fetch_array($result))
{
echo "<option>";
echo $row['cliente_id']."-".$row[Nombre];
echo "</option>";
}
echo "<input type='text' name='saldo'>";
echo "<input type='submit' value='agregar'>";
echo "</select>";
echo "</form>";
mysqli_close($con);
?>
On the next page I'm trying to obtain just $row['cliente_id']
using $_POST
on php how can I do this?
Pls try with this,
while($row = mysqli_fetch_array($result))
{
echo "<option value='".$row['cliente_id']."'>".$row['cliente_id']."-".$row['Nombre']."</option>";
}
The value of the input fields will be posted to the form action page on submit. So you can use the id as the value of the select options.
while($row = mysqli_fetch_array($result))
{
echo "<option value='".$row['cliente_id']."'>";
echo $row['cliente_id']."-".$row[Nombre];
echo "</option>";
}
You can use value of Select Option
Corrected Code:
echo "<select name='nombre'>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='".$row['cliente_id']."'>>"; //**value added**
echo $row['cliente_id']."-".$row[Nombre];
echo "</option>";
}
echo "</select>";
If you want to user this ClientId in next page you can use simply use:
$_POST['nombre']
Replace this code in your while loop, or just take value as cliente_id
echo "<option value=".$row['cliente_id'].">";
echo $row['cliente_id']."-".$row[Nombre];
echo "</option>";