Php 从mysql下拉列表
我有一个MySQL数据库,它有4个表:客户、产品、订单和订单详情。 我正在尝试获取具有下拉列表的html或php表单: 1-客户:从客户表中提取 2-项目:从产品表中提取SF 3类:从产品表中提取 然后将信息发布到订单和订单详细信息中。 我得到了从视图中提取数据的表单,但我需要它来发布数据\ 代码如下:Php 从mysql下拉列表,php,forms,post,Php,Forms,Post,我有一个MySQL数据库,它有4个表:客户、产品、订单和订单详情。 我正在尝试获取具有下拉列表的html或php表单: 1-客户:从客户表中提取 2-项目:从产品表中提取SF 3类:从产品表中提取 然后将信息发布到订单和订单详细信息中。 我得到了从视图中提取数据的表单,但我需要它来发布数据\ 代码如下: <?php include ('connect.php'); mysql_select_db("db") or die(mysql_error()); $sql = "SELEC
<?php
include ('connect.php');
mysql_select_db("db") or die(mysql_error());
$sql = "SELECT DISTINCT Company FROM `values` LIMIT 0, 30 ";
$result=mysql_query($sql);
echo "<select Company='Company'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Company'] ."'>" . $row['Company'] ."</option>";
}
echo "</select>";
$sql = "SELECT DISTINCT Item, Type FROM `values` LIMIT 0, 30 ";
$result=mysql_query($sql);
echo "<select Item='Item'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Item'] ."'>" . $row['Item'] ."</option>";
}
echo "</select>";
$sql = "SELECT DISTINCT Type FROM `values` LIMIT 0, 30 ";
$result=mysql_query($sql);
echo "<select Type='Type'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Type'] ."'>" . $row['Type'] ."</option>";
}
echo "</select>";
?>
<input type="Submit">
在您的问题中,您没有提到表的结构。 让你有自己的公司桌子,或者自己制作
company table
+--------+-----------+-------------------+
| id | company | some other field |
+--------+-----------+-------------------+
| 1 | abc | xxxx |
+--------+-----------+-------------------+
| 2 | def | xxxx |
+--------+-----------+-------------------+
//id应该是主键
your product table (each product is made by some company)
+--------+-----------+-------------------+
| id | product | company_id |
+--------+-----------+-------------------+
| 1 | p1 | 2 |
+--------+-----------+-------------------+
| 2 | p2 | 1 |
+--------+-----------+-------------------+
现在你可以做喜欢的事了
//code to show list of companies
$sql = "SELECT `id`, `company` FROM `company` LIMIT 0, 30 ";
$result=mysql_query($sql);
echo "<select Company='Company'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['id'] ."'>" . $row['Company'] ."</option>";
}
echo "</select>";
让您的orders表具有如下结构
+--------+--------------+-------------+--------------+
| id | company_id | product_id | category_id |
+--------+--------------+-------------+--------------+
| 1 | 1 | 2 | 8 |
+--------+--------------+-------------+--------------+
同样,这里id是主键和
company_id=company表格中的id、product_id和category_id也是其表格的唯一id
=>创建一个文件save.php并在此处处理表单发送的数据
<?php
//change according to your settings
mysql_connect('localhost','root','');
mysql_select_db("st") or die(mysql_error());
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['Submit'])) {
var_dump($_POST);
$company_id = $_POST['company_id'];
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
//Note: sanitize and validate data before saving in database
$sql = "INSERT INTO orders VALUES('', $company_id, $product_id, $category_id)";
$result = mysql_query($sql);
if($result) {
echo "data saved";
} else {
echo "error";
}
}?>
//阅读有关html中输入字段、php的PDO类、数据清理和验证以及sql查询的更多信息
问题:更新:
当我运行你的代码时。我对你的一些领域感到困惑。你想用这张表格做什么。我的理解
1客户可以添加订单。但为什么会有一个下拉列表来选择客户,而实际上,您是在显示带有客户标签的公司
2一些输入字段-
订单->用户提供订单名称或订单id如果订单id对db不好。bcoz用户可以两次生成相同的订单id。
日期->提交的日期、完成日期或当前日期,如果是当前日期,则应在后端完成。
复选框->为什么复选框不显示
空白和船舶->他们是什么
税务->用户对产品征税,可能是我错了
价格->用户给出每个产品的价格,好的,可能是我错了
总计->用户给出总计,或者按价格*数量+总税金计算。这样就不需要输入框了
在实施什么信息之前,要更加清楚。您希望从用户处获取信息,但该信息是否安全。或者您为用户生成它。因为表单向db注释用户,这可能会导致严重问题。这是我的表单:
<?php
//change according to your settings
include ('connect.php');
mysql_select_db("hachemal_compuworld") or die(mysql_error());
?>
<form action="Add_order.php" method = "POST">
<label for="Customer">Customer<br />
</label>
<select name="Cust_id" id="Cust_id" style="width: 126px">
<?php
$sql = "SELECT `Cust_id`, `Company` FROM `customer` LIMIT 0, 30 ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Cust_id'] ."'>" . $row['Company'] ."</option>";
}
?>
</select>
Order: <input name="Order_id" type="text" style="width: 52px"> Status <br>
Date:
<input type="text" id="txtDate" onclick="fnPopUpCalendar(txtDate,txtDate,'mm/dd/yyyy')" style="width: 72px" name="Date"/>
<span class="style2">.. <SELECT name="Status" style="width: 78px; height: 22px">
<OPTION value="Open" selected="">Open</OPTION>
<OPTION value="In Progress">In Progress</OPTION>
<OPTION value="Paid">Paid</OPTION>
<OPTION value="Past Due">Past Due</OPTION>
<option value="Cancelled">Cancelled</option>
<option value="Closed">Closed</option>
</SELECT>...............</span> <br>
<br>
<TABLE id="dataTable0" width="350px" cellspacing="1" class="style5">
<TR>
<TD style="height: 32px" class="style1"> </TD>
<TD style="height: 32px" class="style1"> </TD>
<TD style="height: 32px" class="style1"> </TD>
<TD style="height: 32px" class="style1"><span class="style2">.. .</span>Qty<span class="style2">...</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">.......</span>Product<span class="style2">........</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">....</span>Category<span class="style2">....
</span>
</TD>
<TD style="height: 32px" class="style1">
<span class="style2"></span>blank<span class="style2"></span>
</TD>
<TD style="height: 32px" class="style1">
<span class="style2">.</span>Price<span class="style2">.</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">...</span>Ship<span class="style2">..</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">..</span>Tax<span class="style2">...</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">..</span>Total<span class="style2">...
</TABLE>
<TABLE id="dataTable" width="350px" cellspacing="1" class="style5">
<TR><TD style="height: 32px"><INPUT type="checkbox" name="chk"/></TD>
<TD style="height: 32px"></TD>
<TD style="height: 32px"></TD>
<TD style="height: 32px">
<INPUT type="text" name="Qty" style="width: 37px"/></TD>
<TD style="height: 32px">
<select name="Product" id="Product" style="width: 87px">
<?php
$sql = "SELECT `Product`, `Product` FROM `Products` LIMIT 0, 30 ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Product'] ."'>" . $row['Product'] ."</option>";
}
?>
</select>
<TD style="height: 32px">
<select name="Category" id="Category" style="width: 89px">
<?php
$sql = "SELECT `Category`, `Category` FROM `Products` LIMIT 0, 30 ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Category'] ."'>" . $row['Category'] ."</option>";
}
?>
</select>
</TD>
</TD style="height: 32px">
</TD>
<TD style="height: 32px">
<input name="Text2" type="text" style="width: 37px"></TD>
<TD style="height: 32px">
<input name="Price" type="text" style="width: 39px"></TD>
<TD style="height: 32px">
<input name="Ship" type="text" style="width: 42px"></TD>
<TD style="height: 32px">
<input name="Tax" type="text" style="width: 39px"></TD>
</TD style="height: 32px">
</TD>
<TD style="height: 32px">
<input name="Total" type="text" style="width: 50px"></TD>
</TR>
</TABLE>
<br>
<input type="submit" name = "Submit"/>
</form>
<br>
</html>
我用公司id试过了,下拉列表是空的。然后我尝试了以下方法:$sql=从客户限制中选择不同的公司0,30$result=mysql\u query$sql;回响而$row=mysql_fetch_array$result{echo{$row['Company']};}echo;没有投递吗?有效吗?你的桌子是什么样子的?我猜是公司id/公司名称?我更新了答案。您可能需要将company_id更改为您使用的id名称,将company_name更改为您在表中使用的名称……好的,下拉列表显示了值,但仍然没有发布到表中。知道下拉列表从一个tablecompany中提取并发布到另一个tableorder。太好了!!工作完美。你真是个天才,谢谢。我需要做更多的事情。此表单发布到两个表:订单和订单详细信息。订单的订单id为自动递增。订单详细信息的交易id为自动增加,订单id为int。我希望刚才gbot在Orders中递增的order_id将其自身复制到order_详细信息中。此外,我还需要能够在表单列中添加行:数量|产品|类别|价格|税收谢谢。现在并不难。但是,首先执行>1-您的order_id of orders表是=我做的相同作业的orders表的id,两者都是自动递增的。2-我认为价格与产品一起存储在产品表中。在products tableprice中添加一个新列。3-税是固定的,正如我所想,或者您可以在products表中添加一个新列来存储税。去做吧。我会在几分钟内更新我的答案。它完全混淆了。我正在努力。请从phpMyAdmin>数据库>表>产品id-auto_increment,product,price,tax,…这是订单表:订单id int11无自动增量日期文本拉丁1瑞典ci客户id int11 Trans\u id int11总计小数10,0状态文本拉丁1瑞典CIT这是订单详细信息表:1 Trans\u id int11无自动增量2订单id int11 3数量int11是空4产品varchar60 5类别varchar30这是我的第一个数据库,请原谅我弄得一团糟:谢谢你的帮助
<?php
//change according to your settings
mysql_connect('localhost','root','');
mysql_select_db("st") or die(mysql_error());
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['Submit'])) {
var_dump($_POST);
$company_id = $_POST['company_id'];
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
//Note: sanitize and validate data before saving in database
$sql = "INSERT INTO orders VALUES('', $company_id, $product_id, $category_id)";
$result = mysql_query($sql);
if($result) {
echo "data saved";
} else {
echo "error";
}
}?>
<?php
//change according to your settings
include ('connect.php');
mysql_select_db("hachemal_compuworld") or die(mysql_error());
?>
<form action="Add_order.php" method = "POST">
<label for="Customer">Customer<br />
</label>
<select name="Cust_id" id="Cust_id" style="width: 126px">
<?php
$sql = "SELECT `Cust_id`, `Company` FROM `customer` LIMIT 0, 30 ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Cust_id'] ."'>" . $row['Company'] ."</option>";
}
?>
</select>
Order: <input name="Order_id" type="text" style="width: 52px"> Status <br>
Date:
<input type="text" id="txtDate" onclick="fnPopUpCalendar(txtDate,txtDate,'mm/dd/yyyy')" style="width: 72px" name="Date"/>
<span class="style2">.. <SELECT name="Status" style="width: 78px; height: 22px">
<OPTION value="Open" selected="">Open</OPTION>
<OPTION value="In Progress">In Progress</OPTION>
<OPTION value="Paid">Paid</OPTION>
<OPTION value="Past Due">Past Due</OPTION>
<option value="Cancelled">Cancelled</option>
<option value="Closed">Closed</option>
</SELECT>...............</span> <br>
<br>
<TABLE id="dataTable0" width="350px" cellspacing="1" class="style5">
<TR>
<TD style="height: 32px" class="style1"> </TD>
<TD style="height: 32px" class="style1"> </TD>
<TD style="height: 32px" class="style1"> </TD>
<TD style="height: 32px" class="style1"><span class="style2">.. .</span>Qty<span class="style2">...</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">.......</span>Product<span class="style2">........</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">....</span>Category<span class="style2">....
</span>
</TD>
<TD style="height: 32px" class="style1">
<span class="style2"></span>blank<span class="style2"></span>
</TD>
<TD style="height: 32px" class="style1">
<span class="style2">.</span>Price<span class="style2">.</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">...</span>Ship<span class="style2">..</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">..</span>Tax<span class="style2">...</span></TD>
<TD style="height: 32px" class="style1">
<span class="style2">..</span>Total<span class="style2">...
</TABLE>
<TABLE id="dataTable" width="350px" cellspacing="1" class="style5">
<TR><TD style="height: 32px"><INPUT type="checkbox" name="chk"/></TD>
<TD style="height: 32px"></TD>
<TD style="height: 32px"></TD>
<TD style="height: 32px">
<INPUT type="text" name="Qty" style="width: 37px"/></TD>
<TD style="height: 32px">
<select name="Product" id="Product" style="width: 87px">
<?php
$sql = "SELECT `Product`, `Product` FROM `Products` LIMIT 0, 30 ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Product'] ."'>" . $row['Product'] ."</option>";
}
?>
</select>
<TD style="height: 32px">
<select name="Category" id="Category" style="width: 89px">
<?php
$sql = "SELECT `Category`, `Category` FROM `Products` LIMIT 0, 30 ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Category'] ."'>" . $row['Category'] ."</option>";
}
?>
</select>
</TD>
</TD style="height: 32px">
</TD>
<TD style="height: 32px">
<input name="Text2" type="text" style="width: 37px"></TD>
<TD style="height: 32px">
<input name="Price" type="text" style="width: 39px"></TD>
<TD style="height: 32px">
<input name="Ship" type="text" style="width: 42px"></TD>
<TD style="height: 32px">
<input name="Tax" type="text" style="width: 39px"></TD>
</TD style="height: 32px">
</TD>
<TD style="height: 32px">
<input name="Total" type="text" style="width: 50px"></TD>
</TR>
</TABLE>
<br>
<input type="submit" name = "Submit"/>
</form>
<br>
</html>