1

我有一个 MySQL 数据库,它有 4 个表:客户、产品、订单和 order_details。我正在尝试获取具有下拉列表的表单(html 或 php):1-客户:从客户表中提取 2-项目:从产品表中提取 3-类别:从产品表中提取

然后将信息发布到 order & order_details。我得到了从视图中提取数据的表单,但我需要它来发布数据。\

这是代码:

<?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">
4

3 回答 3

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>
于 2013-03-28T15:08:48.017 回答
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,对于该表来说是唯一的

=> 创建一个文件 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一些输入字段 -
订单-> 用户提供订单名称或 order_id 如果 order_id 则它对 db 不利。bcoz 用户可以两次制作相同的订单 ID。
日期-> 提交的日期、完成日期或当前日期,如果是当前日期,那么您应该在后端完成。
复选框->为什么复选框没有
空白并发货->它们是什么
->用户对产品征税,它可以(可能是我错了)
价格->用户给出每个产品的价格,好的(可能是我错误)
总计-> 用户给出总计,或者它应该计算为价格 * 数量 + 总税。那么就不需要输入框了

在实施什么信息之前更清楚。你想从用户那里得到它是否安全。或者您为用户生成它。因为表单评论用户到 db 这可能会导致严重的问题。

于 2013-03-28T16:06:06.340 回答
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">

&nbsp;<label for="Customer">Customer<br />
</label>

    &nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

Order:&nbsp; <input name="Order_id" type="text" style="width: 52px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Date:&nbsp;&nbsp;
<input type="text" id="txtDate" onclick="fnPopUpCalendar(txtDate,txtDate,'mm/dd/yyyy')" style="width: 72px" name="Date"/>&nbsp;
<span class="style2">..&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;



    <br>



<TABLE id="dataTable0" width="350px" cellspacing="1" class="style5">

    <TR>

        <TD style="height: 32px" class="style1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</TD>

        <TD style="height: 32px" class="style1">&nbsp;</TD>

        <TD style="height: 32px" class="style1">&nbsp;</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">....&nbsp;

            </span>

        </TD>

        <TD style="height: 32px" class="style1">

            <span class="style2"></span>blank<span class="style2"></span>&nbsp;

        </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">

            &nbsp;</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>
于 2013-03-29T06:23:36.380 回答