我有一个MySQL数据库,有4个表:客户,产品,订单和订单详细信息。 我想获得一个有下拉的表单(html或php): 1客户:从客户表中提取 2项:拉出产品表 3类别:从产品表中提取
然后将信息发布到订单&订单详细信息。 我得到了从视图中提取数据的表单,但我需要它来发布数据。\
这是代码:
<?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">
答案 0 :(得分:1)
<label for="company_id">Company:</label>
<select name="company_id" id="company_id">
<option value="0">-- Select Company--</option>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<option value='{$row[company_id']}'";
if ($errors && $_POST["company_id"] == $row[company_id']) {
echo 'selected="selected"';
}
echo ">{$row['company_name']}</option>";
}
?>
</select>
答案 1 :(得分:1)
在你的问题中你没有提到你桌子的结构。 让你的公司表像(或制作)
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>";
Edit:
太好了,现在你的下拉列表显示了这个列表。
但我认为您错过了问题中的<form action='' method = "POST">
或可能在您的档案中。
制作一个完整的功能形式。
<?php
//change according to your settings
mysql_connect('localhost','root','');
mysql_select_db("st") or die(mysql_error());
?>
<form action="st2.php" method = "POST">
<label for="company_id">Compay</label>
<select name="company_id" id="company_id">
<?php
$sql = "SELECT `id`, `company` FROM `company` LIMIT 0, 30 ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['id'] ."'>" . $row['company'] ."</option>";
}
?>
</select>
<label for="product_id">Product</label>
<select name="product_id" id="product_id">
<?php
$sql = "SELECT `id`, `product` FROM `product` LIMIT 0, 30 ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['id'] ."'>" . $row['product'] ."</option>";
}
?>
</select>
<label for="category_id">Category</label>
<select name="category_id" id="category_id">
<?php
$sql = "SELECT `id`, `category` FROM `category` LIMIT 0, 30 ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['id'] ."'>" . $row['category'] ."</option>";
}
?>
</select>
<input type="submit" name = "Submit"/>
</form>
让您的订单表具有这样的结构
+--------+--------------+-------------+--------------+
| id | company_id | product_id | category_id |
+--------+--------------+-------------+--------------+
| 1 | 1 | 2 | 8 |
+--------+--------------+-------------+--------------+
这里再次id是主键和 company_id =公司表中的id,product_id和category_id也是其表的id,对于该表是唯一的
=&GT;制作文件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
一些输入字段 -
的顺序强> - &GT;如果order_id,用户提供订单名称或order_id,那么它对db不利。 bcoz用户可以两次制作相同的订单ID
的日期强> - &GT;提交的日期,完成时间或当前日期,如果是当前日期,那么您应该在后端完成。
复选框 - &gt;为什么复选框没有获得
空白并发货 - &gt;他们是什么
税 - &gt;用户对产品征税,可以(可能是我错了)
价格 - &gt;用户给出每件产品的价格,好的(可能是我错了)
总计 - &gt;用户给出总数,或者应该计算为价格*数量+总税额。那么就不需要输入框了
在实施哪些信息之前让它更清晰。你想从用户那里获得它是否安全。或者为用户生成。因为表单将用户注释到db,这可能会导致严重的问题。
答案 2 :(得分:0)
这是我的表格:
<?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>