将下拉列表中显示的数据从一个表保存到另一个表

时间:2013-12-12 12:20:52

标签: php mysql

当我尝试在另一个表中的PHP-MySQL字段值中显示并将其保存到当前表中时,我是dropdown新手并遇到问题。我在网上找到了一个完整的CRUD教程,但它只使用输入字段,没有选择字段和其他复杂的东西,只有简单的创建,更新,显示功能。

目前我有2张桌子:

  1. 客户,包含以下字段:

    customer_id, customer_name, customer_address, customer_country

  2. 订单,包含字段:

    order_id, customer_id, product_name, product_price, product_qty

  3. 拥有以下orders.php我想使用dropdown customer_id来显示来自 customers表customer_name并选择客户名称customer_id保存在当前表(订单)customer_id字段中。

    <?php
        // creates the new record form
        // since this form is used multiple times in this file, I have made it a function  that is easily reusable
        function renderForm($customerid, $productname, $productprice, $productqty, $error)
        {
    ?>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
       <meta charset="utf-8">
       <title>ORDERS</title>
    </head>
    
    <body>
        <form action="" method="post">
            <label for="customer_id">Customer ID:* </label>
            <select name="customer_id">
            <?php
                $sql = "SELECT * FROM customers ORDER BY customer_name ASC;";
                $res = mysql_query($sql);
                while($row= mysql_fetch_assoc($res)) {
                    echo "<option value='" . $row['customer_id']
                            . "'>" . $row['customer_name'] . "</option>";
                }
            ?>
            </select>
        <label for="product_name">Product name:*</label>
        <input type="text" name="product_name" value="<?php echo $productname; ?>" /><br/>
        <label for="product_price">Price:* </label>
        <input type="text" name="product_price" value="<?php echo $productprice; ?>" /><br/>
        <label for="product_qty">Qty:* </label>
        <input type="text" name="product_qty" value="<?php echo $productqty; ?>" /><br/>
        <input type="submit" name="submit" class="btn-global" value="Save">
        </form>
    </body>
    </html>
    <?php 
        }
    
        // connect to the database
        include('connect.php');
    
        // check if the form has been submitted. If it has, start to process the form and save it to the database
        if (isset($_POST['submit']))
        { 
            // get form data, making sure it is valid
            $id = mysql_real_escape_string(htmlspecialchars($_POST['customer_id']));
            $product = mysql_real_escape_string(htmlspecialchars($_POST['product_name']));
            $price = mysql_real_escape_string(htmlspecialchars($_POST['product_price']));
            $qty = mysql_real_escape_string(htmlspecialchars($_POST['product_qty']));
    
            // check to make sure both fields are entered
            if ($id == '' || $product == '' || $price == '' || $qty == '')
            {
                // generate error message
                $error = 'ERROR: Please fill in all required fields!';
    
               // if either field is blank, display the form again
               renderForm($customerid, $productname, $productprice, $productqty, $error);
    
            }
            else
            {
                // save the data to the database
                mysql_query("INSERT orders SET customer_id='$id', product_name='$product',  product_price='$price', product_qty='$qty'")
                or die(mysql_error()); 
    
                // once saved, redirect back to the view page
                header("Location: some.php"); 
             }
        }
        else    // if the form hasn't been submitted, display the form
        {
            renderForm('','','','','');
        }
    ?> 
    

1 个答案:

答案 0 :(得分:0)

你的sql错了,INSERT应该是这样的:

INSERT INTO orders (customer_id, product_name, product_price, product_qty) VALUES ('$id', '$product', '$price','$qty')

更新现有记录时,可以使用以下内容:

UPDATE orders SET product_name='$someValue', product_price='$someValue2' WHERE customer_id='$id'