douxi7219 2015-07-18 14:29
浏览 45

PDO表单添加到数据库:添加了数组编号而不是它所代表的文本

I'm new to PHP and PDO. I've managed to get the PDO to add the form data to a mysql database when the submit button is clicked. The problem I am having is drop down box which selects and displays the data from another database table. When this is added to the databases rather than displaying the selected option 'Top, Hat or Coat' it instead displays '0, 1, 2'.

HTML Code (with some PHP):

<!DOCTYPE html>
<html>
<head>
</head>

<body>
    <div>
        <?PHP include_once("addProduct.php");?>
        <form method="post" action="">
            Product Name: <input type="text" id="productName" name="productName" /><br />

            Catagory: 
            <?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";
                $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><br />

            Stock: <input type="number" id="stock" name="stock" /><br />

            Cost: <input type="number" id="cost" name="cost" /><br />

            <input type="submit" value="add"> <br />

            <?PHP   
                $query = "SELECT * FROM product_table";
                $result = $odb->query($query);
                if($result->rowCount() > 0) {
                    foreach($result as $item) {
                    echo($item['name'] . " , " . $item['category'] . " , " . $item['stock'] . " , " . $item['cost'] . "<br />");
                    }
                }
            ?>
        </form>
    </div>

</body>

PHP Code:

$host = "localhost";
$user = "root";
$db = "web_scripting";
$pass = "";

$odb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);

if(isset($_POST['productName'])) {
    $productName = $_POST['productName'];
    $category = $_POST['category'];
    $stock = $_POST['stock'];
    $cost = $_POST['cost'];
    $q = "INSERT INTO product_table(name, category, stock, cost) VALUES(:name, :category, :stock, :cost);";
    $query = $odb->prepare($q);
    $results = $query->execute(array(
        ":name" => $productName,
        ":category" => $category,
        ":stock" => $stock,
        ":cost" => $cost
    ));
}
?>
  • 写回答

1条回答 默认 最新

  • douyong4842 2015-07-18 14:46
    关注

    I would say this is correct, your database is saving the id of the category which is what you want. The name of the category can be found by querying the category table for that id. This is relational database design. Consider if you did store the name of the category on the product table. Later down the line you then decided to change the name of the category, you would need to update all products records rather than just one category record.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序