问题已解决:这就是解决方案,感谢@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'];
}
} ?>
<小时/>
正如标题所说,我正在尝试为我的杂货店制作一个非常简单的会计网站,在那里我记录每日销售额。我已经制作了一个表单,使用这些表名称在数据库中添加新文章:
Table: Postres -- postreID(id of the article), nombre(name of the article), price(price of the article).
我还有另一个名为“ventas”的表,我想在其中存储基于日期标准的所有销售额,这已经完成了。
Table: Ventas -- id(sale id), fecha(date of registered sale), postre_vendido(name of the article sold), ganancia(the article price).
这是我用于注册销售的代码:
<?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>
最后,它仅捕获 (id,fecha,postre_vendido,ganancia) 的前 3 列,并将“ganancia”列指定为 0。
你们知道如何解决这个问题吗?提前致谢!
最佳答案
未插入价格,因为您的 <form>
中没有控件这里面有价格,但我认为解决这个问题的方法实际上是不添加这样的控件。在我看来,您确实不需要或不希望用户输入价格,您应该在发布表单时从产品表中读取价格。有多种方法可以做到这一点。一种方法是在您获得要在 <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'];
}
} ?>
如果您这样做,请务必删除 $preciodelpostre = $_POST['precio'];
稍后在代码中;因为没有名为 'precio'
的控件在表单上,这将用 null
覆盖以前的值最终结果为 0
在您插入的行中。
关于php - 仅使用 1 个 Select 从数据库的不同列中提取 2 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38794809/