0

I have a dropdown list which is populated by php.I need to enter new values to the db using the data entered.The page is posted to itself.But the list does not show the new values inserted.

SOURCE CODE:

  <!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
    {

    }
    ?>
8
  • You are selecting the values before you insert, thats the problem i think Commented Feb 25, 2015 at 12:09
  • Theres no insert code and after that it seems you will show your database records first and only then insert them, that won't show you the last one inserted. Commented Feb 25, 2015 at 12:10
  • @Bulk deleted code accidentally while posting,edited Commented Feb 25, 2015 at 12:10
  • @techno no problem I deleted my comment after I saw that Commented Feb 25, 2015 at 12:11
  • @techno my comment remain valid! You are retrive values from DB before you save new one on DB (if exists new one to be inserted). Try to pass your last php code to the beggining of your file. Commented Feb 25, 2015 at 12:13

1 Answer 1

3

@techno, you are retrive values from DB before you save new one on DB (if exists new one to be inserted). Try to pass your last php code to the beggining of your file. Code you should pass to beggining is:

    <?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
        {

        }
    ?>

And give a space in this line next to VALUES

$result = $conn->query("INSERT INTO products(pname) VALUES ('$productname')");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.