Alan Carlos Lopez Alan Carlos Lopez - 2 months ago 6x
HTML Question

Extracting 2 values from different columns from a DB with only 1 Select

Problem solved: This is the solution, thanks to @dont-panic and everyone who helped me !

while($row = mysql_fetch_assoc($get)) {
$option .= '<option id="opcion" value="'.$row['nombre'].'">'
if (isset($_POST['postre']) && $row['nombre'] == $_POST['postre']) {
$preciodelpostre = $row['precio'];
} ?>

as the title say, I'm trying to make a very simple accounting website for my grocery store where I keep my daily sales registered. I already made a form to add new articles in the Database with these table names:

Table: Postres -- postreID(id of the article), nombre(name of the
article), price(price of the article).

I also have another table called "ventas", which is where I wanna store all my sales based on a date criteria, which is already done.

Table: Ventas -- id(sale id), fecha(date of registered sale),
postre_vendido(name of the article sold), ganancia(the article price).

This is my code to register my sales:

$dbhost = '';
$dbuser = 'dbo675';
$dbpass = 'dotCos215';
$dbname = 'db643975';
$con = mysql_connect($dbhost,$dbuser,$dbpass);
$db = mysql_select_db($dbname,$con);
$get=mysql_query("SELECT * FROM Postres ORDER BY postreID ASC");
$option = '';
while($row = mysql_fetch_assoc($get))
$option .= '<option id="opcion" value = "'.$row['nombre'].'">'.$row['nombre'].'</option>';
$postrevendido = $_POST['postre'];
$preciodelpostre = $_POST['precio'];
$sql = "INSERT INTO Ventas (id,fecha,postre_vendido,ganancia) VALUES(NULL,NOW(),'$postrevendido','$preciodelpostre')";
$retval = mysql_query($sql);

if(! $retval ) {
die('Could not enter data: <p></p><a href="ventasdiarias.php" target="_self">Agregar otro articulo</a>' . mysql_error());

echo "Postre agregado exitosamente!!" . "Volviendo..." . '<p></p><a href="ventasdiarias.php" target="_self">Agregar otro articulo</a>';

}else {
<form method = "post" action = "<?php $_PHP_SELF ?>">
<p>Producto Vendido
<select name="postre" id="postre">
echo $option . '<p></p>';

<input name = "agregar" type = "submit" id = "agregar"
value = "Agregar Venta">
<?php }?>

At the end it only captures the first 3 columns from(id,fecha,postre_vendido,ganancia) and asigns column "ganancia" a value of 0.

You guys have any idea on how to solve this? Thanks in advance!


The price isn't inserting because you don't have a control in your <form> that has the price in it, but I think the way to fix this is actually not to add such a control. In my opinion you really don't need or want the user to enter the price, you should read it from your product table at the time the form is posted. There are various ways to do that. One way is to set it as you are getting the options to show in the <select>.

while($row = mysql_fetch_assoc($get)) {
    $option .= '<option id="opcion" value="'.$row['postreID'].'">'
    if (isset($_POST['postre']) && $row['postreID'] == $_POST['postre']) {
        $preciodelpostre = $row['price'];
} ?>

If you do it this way, be sure to remove the $preciodelpostre = $_POST['precio']; later in the code; since there is not control named 'precio' on the form, this will overwrite the previous value with null which will end up as 0 in your inserted row.