Hi,

I have created dynamic dropdown list called technologies from table "technology". I have used it on another form called 'add_project'. In this form, there are 2 fields project name and technology which is dynamic dropdown list. I am getting all the values in dropdown.Now I want to save those value in another table called "project" under the field name called "technology". But I am unable to insert it into the database. Pls help me i am stuck here for a day now and not getting anything on google. Thanks in advance.

My code is:

<html>
<body>

<?php 

// Query to make connection with database
$con = mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('practice') or die(mysql_error());


if(isset($_POST['submit']) && $_POST['submit'] == 'Add')
{

         $prjName = $_POST['prjName'];
         $techOption = $_POST['select1'];

          if($prjName != "" && $techOption != "")
         {
            // Query to insert data into project
            echo $sql = "insert into project(prj_name,technology) values ('$prjName','$techOption')";
            if(mysql_query($sql))
            {
                echo "Success";
            }

            else
            {
                mysql_error();
            }
        }
        else
        {
            echo "Can't insert data " . mysql_error();
        }
        // }
// $sql = mysql_query($sql);


}

?>
<form name = "addPrj_form" method = "POST">
    <table id = "addPrj_form">
        <tr>
            <td></td>
            <td> Add New Projects </td>
        </tr>
        <tr>
            <td> Project Name </td>
            <td><input type = "text" name = "prjName" ></td>
        </tr>
        <tr>
            <td>Technologies</td>
            <!--  <td><select name = "technologies" id = "technologies">
                <option> Php </option>
                <option>HTML5</option>
                <option>CSS3</option>
                <option>Drupal</option>
                <option>iOS</option>
                <option>Android</option>
                <option>SAp</option>
                <option>.Net</option>
                <option>Yii</option>
            </select></td> -->
            <td><select name="select1" id = "select2">

                <?php
                // Query to get values of technologies in dropdown list of technology
                    $query = "select tech_name from technology";
                    $result = mysql_query($query);

                 while ($line = mysql_fetch_assoc($result))
                { ?>
                    <option name  = "techOption" value = "<?php $line['tech_name'];?>"><?php echo $line['tech_name']; ?> </option>
                <?php
                }
                ?>
            </select></td> 
        </tr> 
        <tr>
            <td></td>
            <td> <input type = "submit" name = "submit" value = "Add"></td>
        </tr>
    </table>
</form>
</body>
</html>

Other than the fact that mysql is depreciated (use mysqli or pdo), it looks to me like your php code should run, so I think it's a sql or database problem. Try changing line 25 to echo mysql_error(); instead of just mysql_error(); and you might see an error message that isn't being displayed right now.

<option name  = "techOption" value = "<?php $line['tech_name'];?>"><?php echo $line['tech_name']; ?> </option>

remove the name="techOption"

<option value = "<?php $line['tech_name'];?>"><?php echo $line['tech_name']; ?> </option>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.