【问题标题】:Save data displayed in a dropdown from one table to another将下拉列表中显示的数据从一个表保存到另一个表
【发布时间】:2013-12-12 12:20:52
【问题描述】:

我是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.

拥有以下orders.php 我想使用dropdown 代替customer_id客户表 中显示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('','','','','');
    }
?> 

【问题讨论】:

  • 你遇到了什么错误?
  • 你混淆了 UPDATE 和 INSERT

标签: php mysql


【解决方案1】:

你的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' 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2021-10-16
    • 2015-10-08
    • 2014-06-22
    • 2011-10-23
    • 2015-11-19
    相关资源
    最近更新 更多