PHP在提交后未在下拉列表中显示更新的值

时间:2015-02-25 12:04:34

标签: php html mysql forms

我有一个由php填充的下拉列表。我需要使用输入的数据向数据库输入新值。页面将发布到自身。但列表未显示插入的新值。

消息来源

  <!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>Add Product Category</title>
    <link rel="stylesheet" type="text/css" href="view.css" media="all">
    <script type="text/javascript" src="view.js"></script>
    <script type="text/javascript">
    function validateForm() {
        var x = document.forms["form_974780"]["productname"].value;
        if (x == null || x == "") {
            alert("Please Enter New Product Name");
            return false;
        }
    }
    </script>
    </head>
    <body id="main_body" >

        <img id="top" src="top.png" alt="">
        <div id="form_container">

            <h1><a>Add Product Category</a></h1>
            <form id="form_974780" class="appnitro"  method="post" action="?" onsubmit="return validateForm()">
                        <div class="form_description">
                <h2>Add Product Category</h2>
                <p></p>
            </div>                      
                <ul >

                        <li id="li_12" >
            <label class="description" for="element_12">Existing Product Categories  </label>
            <div>
            <select class="element select medium" id="element_12" name="element_12"> 
            <?php 

   // $conn = goes here 
    $result=$conn->query("SELECT pname FROM products");
    while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
    echo "<option value='".$row['pname']."'>".$row['pname']."</option>";


    }
     mysqli_close($conn);
    ?> 

            </select>
            </div> 
            </li>       <li id="li_13" >
            <label class="description" for="element_13">Add New Category </label>
            <div>
                <input id="element_13" name="productname" class="element text medium" type="text" maxlength="255" value=""/> 
            </div><p class="guidelines" id="guide_13"><small>Enter the new Product Category to add and Click the Add Button. </small></p> 
            </li>

                        <li class="buttons">
                    <input type="hidden" name="form_id" value="974780" />

                    <input id="saveForm" class="button_text" type="submit" name="submit" value="submit"/>
            </li>
                </ul>
            </form> 

        </div>
        <img id="bottom" src="bottom.png" alt="">
        </body>
    </html>
    <?php
    if (isset($_POST['submit']))
    {
        $productname=$_POST['productname'];
        //$conn string will go here 
       $result=$conn->query("INSERT INTO products(pname)VALUES('$productname')");
        if($result)
        {

                    echo "<font color=\"white\">";
            echo("Successfully Inserted new Products");

            echo"</font>!";
        }
        else
        {
            echo "<font color=\"red\">";
            echo("Error when inserting");
            echo"</font>!";
        }
    } 
    else
    {

    }
    ?>

1 个答案:

答案 0 :(得分:3)

@techno, 在DB上保存新的值之前,您是DB的反向值(如果存在要插入的新值)。尝试将您的最后一个PHP代码传递给您的文件的开始。 您应该传递给开始的代码是:

    <?php
        if (isset($_POST['submit']))
        {
            $productname=$_POST['productname'];
            //$conn string will go here 
           $result=$conn->query("INSERT INTO products(pname)VALUES('$productname')");
            if($result)
            {

                        echo "<font color=\"white\">";
                echo("Successfully Inserted new Products");

                echo"</font>!";
            }
            else
            {
                echo "<font color=\"red\">";
                echo("Error when inserting");
                echo"</font>!";
            }
        } 
        else
        {

        }
    ?>

并在VALUES

旁边的这一行中留出空格
$result = $conn->query("INSERT INTO products(pname) VALUES ('$productname')");