2013-03-28 64 views
1

我有一個MySQL數據庫,有4個表:客戶,產品,訂單& order_details。 我試圖讓一個表單(HTML或PHP),有下拉菜單: 1-客戶:從客戶表中提取 2項:拉sfrom產品表 3類別:從產品表下拉從MySQL拉扯

然後再換將這些信息發佈成訂單& 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"> 

回答

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:

好極了,現在您的下拉列表顯示了compay列表。 但我想你錯過了<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是主鍵,在運營商表,PRODUCT_ID和CATEGORY_ID 的company_id = 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那麼它是不好的分貝。 bcoz用戶可以使相同的訂單ID兩次。
日期 - >什麼日期,完成日期或當前提交日期,如果當前日期那麼你應該在後端完成。
複選框 - >爲什麼複選框沒有得到
空白和船舶 - >它們是什麼
- >用戶提供有關產品的稅收,其確定(可能是我錯了)
價格 - >用戶給每個產品的價格,好吧(可能是我錯了)
總數 - >用戶給出的總數,或它應該計算爲價格*數量+總稅。那麼就不需要輸入框

在實施什麼信息之前使它更清楚。你想從用戶那裏得到安全與否。或者您爲用戶生成。因爲表單會將用戶評論爲可導致嚴重問題的db。

+0

好的,下拉列表顯示值但仍未發佈到表格中。 知道下拉從一個表(公司)和帖子拉到另一個表(訂單)。 – user31026

+0

@ user31026 - 我編輯我的答案。我希望現在能夠正常工作。 – netsmertia

+0

太棒了!作品完美。你是一位天才,謝謝。我需要做更多的事情。這個表單發佈到2個表格:orders&order_detail。訂單有Order_id作爲自動增量。 Order_details將Trans_id作爲自動iuncrement&Order_id作爲int。我希望訂單中的訂單ID可以在訂單中遞增,以將其自身複製到訂單訂單中。此外,我需要能夠在表格欄中添加「行」:數量|產品|類別|價格|稅收 – user31026

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> 
+0

我COMPANY_ID試了一下下拉來到空。那麼我試過這個: $ sql =「SELECT DISTINCT Company FROM'customer' LIMIT 0,30」; $結果= mysql_query($ SQL); echo「」; 它沒有發佈?那有效嗎? – user31026

+0

你的桌子是什麼樣的?我假設它是company_id/Company(名稱)? – tunedin

+0

我更新了我的答案。您可能必須將company_id更改爲您使用的ID名稱,並將company_Name更改爲您在表格中使用的名稱... – tunedin

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> 
+0

它完全混淆了。我正在做。你可以請粘貼你的每個表結構從phpMyAdmin>數據庫>表>結構 – netsmertia

+0

喜歡的產品(id-auto_increment,產品,價格,稅收,...) – netsmertia

+0

這是Orders表: ORDER_ID INT(11)否無AUTO_INCREMENT 日期文本latin1_swedish_ci CUST_ID INT(11) TRANS_ID INT(11) 總十進制(10,0) 狀態文本latin1_swedish_ci – user31026