0
我有一個表格,它只包含id &類別, 然後我有一個下拉框使用類別列填充表格。 下一步是當點擊提交按鈕時,它將所有數據發送到另一個表[product_table],所有數據提交到產品表中,而不是插入類別名稱插入它的id。 我試過多個修補程序無濟於事,我想最簡單的方法是將輸出類別'1','2','3'...並使用查詢將其轉換爲'帽子',襯衫',鞋'...使用category_table。 我不是100%確定如何雖然做到這一點,從一個表格插入下拉數據到另一個表格
的index.php
<div id="addProduct">
<?PHP require_once("addProduct.php"); ?>
<div id="pageSubTitle" style="display:block">
<form method="post" action="">
<h2>Add a Product</h2>
<table>
<tr>
<td><label>Product Name: </label> <input type="text" id="productName" name="productName" required></td>
<td><label id="categoryLabel"> Catagory: </label>
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("web_scripting") or die(mysql_error());
$query = "SELECT id,category FROM catagory_table ORDER BY category ASC";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select type="text" id="category" name="category">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['id']."'>'".$row['category']."'</option>";
}
?>
</select><p id="refreshCatagoryOnNewProduct"> New catagory not appearing?<br /> <a href="index.php">Click Here</a></p></td>
</tr>
<tr>
<td><label>Stock: </label> <input type="number" id="stock" name="stock" required></td>
<td><label id="costLabel">Cost: </label> <input type="text" id="cost" name="cost" required></td>
</tr>
<tr>
<td><label>Description: </label> <textarea type="text" id="description" name="description" cols="40" rows="5" maxlength="250" placeholder="Enter Description Here (Max 250 Characters...)" required></textarea></td>
<td></td>
</tr>
<tr>
<td><input id="productSubmit" type="submit" value="Add Product" onclick="submitProduct()"></td>
</tr>
</table>
</form>
</div>
</div>
addProduct.php
<?PHP
$db_host = "localhost";
$db_name = "web_scripting";
$db_root = "root";
$db_pass = "";
$odb = new PDO("mysql:host=$db_host;dbname=$db_name", $db_root, $db_pass);
if(isset($_POST['productName'])) {
$productName = $_POST['productName'];
$categoryName = $_POST['category'];
$stock = $_POST['stock'];
$cost = $_POST['cost'];
$description = $_POST['description'];
$q = "INSERT INTO Product_table(name, category, stock, cost, description) VALUES(:name, :category, :stock, :cost, :description);";
$query = $odb->prepare($q);
$results = $query->execute(array(
":name" => $productName,
":category" => $categoryName,
":stock" => $stock,
":cost" => $cost,
":description" => $description
));
}
?>
==>我知道有幾個拼寫錯誤的代碼(通常是category/catagory),但它始終如一,
任何幫助/建議將不勝感激
它插入'id',就像在設置選項值到'id' - >'echo'