我有一张表,其中包括id&类别, 然后我有一个带有下拉框的表单,它使用类别列填充它。 下一步是当点击提交按钮时,它将所有数据发送到另一个表[product_table], 所有数据都提交到产品表,但不是插入类别名称而是插入id。 我尝试过多次修复无济于事,我认为最简单的方法是采取输出类别' 1'' 2'' 3& #39; ...并使用查询将其转换为' hat,' shirt' shoes' ...使用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
));
}
?>
==&GT;我知道代码中有一些拼写错误(通常是类别/类别),但它始终保持一致,
非常感谢任何帮助/建议
答案 0 :(得分:0)
select
元素用于创建下拉列表,但元素中的option value=""
标记定义列表中包含值的可用选项。
在您的情况下,错误就在这里,因为您将$row['id']
置于值
echo "<option value='".$row['id']."'>'".$row['category']."'</option>";
因此,如果您想要name
而不是id
,则必须更改option value
echo "<option value='".$row['category']."'>'".$row['category']."'</option>";