简体   繁体   English

将下拉数据从一个表插入到另一个表

[英]Inserting drop down data from one table to another

I have a table which consists of just id & category, I then have a form with a drop down box which uses the category column to populate it. 我有一个仅由id和category组成的表,然后有一个带有下拉框的表单,该表单使用category列进行填充。 The next step is when the submit button is clicked it sends all the data into another table [product_table], All the data submits to the product table, but rather than the category name inserting it is inserting the id. 下一步是单击“提交”按钮时,它将所有数据发送到另一个表[product_table]中,所有数据都提交到产品表中,而不是插入类别名称而是插入ID。 I've tried multiple fixes to no avail, I'm thinking the easiest way would be to take outputted category '1','2','3'... and use a query to convert it to 'hat,'shirt',shoes'... using the category_table. 我已经尝试了多种修复措施,但都无济于事,我认为最简单的方法是采用输出的类别“ 1”,“ 2”,“ 3” ...,并使用查询将其转换为“帽子” ',shoes'...使用category_table。 I'm not 100% sure how to do this though, 我不确定100%如何做到这一点,

index.php 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&#10;(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 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
    ));
}

?>

==> I know there's a few spelling mistakes in the code (usually category/catagory) but it's consistent throughout, ==>我知道代码中存在一些拼写错误(通常是类别/类别),但在整个过程中都是一致的,

Any help/advice would be great appreciated 任何帮助/建议将不胜感激

The select element is used to create a drop-down list but The option value="" tags inside the element define the available options in the list with values. select元素用于创建下拉列表,但是元素内的option value=""标签使用值定义列表中的可用选项。

In your case, the mistake is here, as you are putting $row['id'] in value 在您的情况下,错误就在这里,因为您将$row['id']赋值

echo "<option value='".$row['id']."'>'".$row['category']."'</option>";

So if you want the name instead of the id , have to change the option value 因此,如果您想要name而不是id ,则必须更改option value

echo "<option value='".$row['category']."'>'".$row['category']."'</option>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从脚本创建的下拉框中重新加载数据,而无需删除元素并创建另一个元素 - Reload data from a drop down box created by script without having to delete the element and create another one 从另一个选择后设置一个下拉菜单 - Setting One Drop Down after Selecting from Another 根据数据库中另一个下拉菜单中选择的选项更新一个下拉菜单 - Updating one drop down menu according to option selected in another drop down menu from database 从一个表克隆的行插入到另一个表的特定位置 - Inserting a row cloned from one table into a specific location of another table 从下拉列表中获取经过过滤的数据,并显示在另一个下拉列表中 - Fetch filtered data from the drop down list and present in another drop down list 将多个行从一个表拖放到另一个表 - Drag and drop multiple rows from one table to another table 一个下拉列表内容取决于另一个 - One drop down list contents depending on another 从另一个下拉菜单中选择一个选项时,动态加载一个下拉菜单 - Dynamically load a drop down on selecting an option from another drop down jQuery Datatable将一列从一个表拖放到另一个表 - Jquery Datatable drag and drop a column from one table to another 如何从一个HTML表拖放到另一个? - how to drag and drop from one HTML table to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM