简体   繁体   English

仅需选择1个即可从数据库的不同列中提取2个值

[英]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 ! 解决的问题:感谢@ dont-panic和所有帮助我的人,这就是解决方案!

while($row = mysql_fetch_assoc($get)) {
    $option .= '<option id="opcion" value="'.$row['nombre'].'">'
                    .$row['nombre']
              .'</option>';
    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). 表格:Postres-postreID(商品ID),nombre(商品名称),price(商品价格)。

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. 我还有另一个名为“ ventas”的表,我想根据日期条件存储我所有的销售额,该表已经完成。

Table: Ventas -- id(sale id), fecha(date of registered sale), postre_vendido(name of the article sold), ganancia(the article price). 表:Ventas-id(销售编号),fecha(注册销售日期),postre_vendido(出售的商品名称),ganancia(商品价格)。

This is my code to register my sales: 这是注册销售的代码:

<?php
$dbhost = 'db641973975.db.alansete.com';
$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>';
}
?>
<html>
<body>
    <?php
    $postrevendido = $_POST['postre'];
    $preciodelpostre = $_POST['precio'];
    if(isset($_POST['agregar'])){
        $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>';            
        mysql_close($conn);

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

                ?>
            </select>
            <input name = "agregar" type = "submit" id = "agregar" 
            value = "Agregar Venta">
        </p>
    </form>
    <?php  }?>
</body>
</html>

At the end it only captures the first 3 columns from(id,fecha,postre_vendido,ganancia) and asigns column "ganancia" a value of 0. 最后,它仅捕获(id,fecha,postre_vendido,ganancia)的前3列,并将“ ganancia”列赋值为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. 价格不会插入,因为<form>中没有包含价格的控件,但是我认为解决此问题的方法实际上是添加此类控件。 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> . 一种方法是在获取要显示在<select>的选项时进行设置。

while($row = mysql_fetch_assoc($get)) {
    $option .= '<option id="opcion" value="'.$row['postreID'].'">'
                    .$row['nombre']
              .'</option>';
    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']; 如果您采用这种方式,请确保删除$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. 由于表单上没有名为'precio'控件,因此它将用null覆盖之前的值,该值在您插入的行中最终为0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM