php - 如何在数据库中存储下拉列表中的值

标签 php html mysql drop-down-menu mysql-insert-id

我创建了一个动态下拉列表,它正在从数据库填充数据。到目前为止它工作正常。现在我想将下拉列表中选定的数据添加到我的数据库中。我尝试了各种可能的方法,但无法在我的数据库中插入值,以下程序没有给出任何错误消息,但它也没有在我的数据库中插入数据。请检查一下。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

<?php 

session_start();
if(isset($_SESSION['username']))
{


    include 'connect.php';

            $select_query=          'Select * from category';
            $select_query_run =     mysql_query($select_query);

    echo "  
        <form action='insert_product.php' method='POST' ></br>

        Product Name:   <input type='text' name='product_name'  /></br>

        Price       :   <input type= 'text' name= 'price'  /></br>

        Description :   <input type='text' name='description'  />*Seperate by Comma</br>

                        ";



    //  Drop Down Display   




            echo "<select name='category'>";


            while   ($select_query_array=   mysql_fetch_array($select_query_run) )
            {

                    echo "<option value='".$select_query_array['category_id']."' >".
                    htmlspecialchars($select_query_array["name"])."</option>";


                }

         $selectTag= "<input type='submit' value='Insert'  /></select></form>";

         echo $selectTag;
    //Drop Down End!




    if(isset($_POST['product_name']) && isset($_POST['price']) && isset($_POST['description'])  )
    {
        echo $product_name  =       $_POST['product_name'];
        echo $price         =       $_POST['price'];
        echo $description   =       $_POST['description'];
        echo $category      =       $_POST['category'];


//Problem Area 

    $query= "insert into products (name, price, description,  category_id ) 
                VALUES( '$product_name', $price, '$description', $category )";


    if($query_run=      mysql_query($query))
    {

        echo 'Data Inserted';



        }   
        else
        {
            'Error In SQL'.mysql_error();
            }
    }

    else
    {
        echo 'empty Field';
        }

}

else
{
    echo 'You Must Log in To View this Page!';
    }
?>

最佳答案

更改:

$select_query=          'Select * from category';
$select_query_run =     mysql_query($select_query);
echo "  
    <form action='insert_product.php' method='POST' ></br>

    Product Name:   <input type='text' name='product_name'  /></br>

    Price       :   <input type= 'text' name= 'price'  /></br>

    Description :   <input type='text' name='description'  />*Seperate by Comma</br>

                    <input type='submit' value='Insert'  />";
echo "<select name='category'>";
while   ($select_query_array=   mysql_fetch_array($select_query_run) )
        {

                echo "<option value='".$select_query_array['category_id']."' >".
                htmlspecialchars($select_query_array["name"])."</option>";


            }

echo "</select></form>";

查询:

$query= "insert into products (name, price, description, category_id, name) VALUES('$product_name', $price, '$description', '".$_POST['category']."' )";

关于php - 如何在数据库中存储下拉列表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17694376/

相关文章:

php - xt:Commerce 嵌套集 categories_left 和 categories_right

html - 如何在 <area coords> 周围设置边界?

mysql - 将mysql中的时间更新为datetime

mysql - 数据库设计: User can be part of many lists,并且列表可以与多个用户关联

php - 在 PHP 中使用 AJAX 进行图像预览

php - 使用三个表和一些空值的 SQL 连接查询

php - 两个其他日期之间的日期,忽略年份

HTML A 标签标题格式

html - 如何将多个div相邻对齐?

mysql - 将prepare() 与DQL 查询一起使用是否有意义?