how to insert value selected in the dropdown to backend?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niths
    New Member
    • Mar 2010
    • 18

    #1

    how to insert value selected in the dropdown to backend?

    hello everybody,
    i had created a dropdown in which project names will be appeared. and i am getting those project name from backend. Below that dropdown i am having a table of users with a check box.so whenever i select a projectname and click on the checkbox of one user, that projectname and username should store in project assign table. The problem is username is storing but the projectname is not storing in the database. it is displaying that column empty.
    so please do the needful.
    Thank u.

    Code:
    <?php
    ob_start();
    @session_start();
    require_once ("check.php");
    createsessions($username,$password,$userid,$projectid);
        $con=mysql_connect("10.70.1.50","invensis","invensis");
        if(!$con)
            {
                die('Could not connect: ' . mysql_error());
            }
        mysql_select_db("database_myproject",$con);
        $sql="SELECT projectname from projects where createdby='".mysql_real_escape_string($_SESSION['username'])."'";
        mysql_error();
        $result=mysql_query($sql);
        $options=""; 
        while ($row=mysql_fetch_array($result)) 
        {
            $id=$row["projectid"];
            $projectname=$row["projectname"];
            $options.="<option value=\"$id\">".$projectname.'</option>';
        }
        $sql2="SELECT * FROM users where reportingto='".mysql_real_escape_string($_SESSION['username'])."' and role='2'";
        mysql_error();
        $result2=mysql_query($sql2);
        $num=mysql_num_rows($result2);
        
        ?> 
    <head>
    <body>
    <script language="javascript" type="text/javascript">
    		function check()
    		{
    			if(document.getElementById('checkbox[]').value == "")
    			{
    				alert('Please select checkbox');
    				return false;
    				
    			}
    		}
    		</script>
    	
    
    <form action="projectassign.php" method="post">
    <table width="100%">
    <tr><td> <img src="Logofinalcopy.gif">
    </td></tr>
        <tr>
            <td bgcolor="aqua"><h2>Project assign</h2></td>
        </tr></table>
        <br>
        <table align="center">
        <tr>
            <td>projectassign</td>
            <td><select name="projectassign">
            <option value="<?php echo '$projectassign';?>" >----select project----</option><?php echo $options ?>
            </select>
            </td>
        </tr>
        </table>  
        <br><br><br>  
        <table border="1" cellspacing="0" cellpadding="0" align="center" style="width: 500px">
        <tr>
            <th></th>
            <th>userid</th>
            <th>username</th>
               
        </tr>
        <?php
        $sno=1;
        while($row=mysql_fetch_array($result2, MYSQL_ASSOC)){
        ?>
         <tr>
    <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['userid']; ?>"></td>
    <td><? echo $row['userid']; ?></td>
    <td><? echo $row['username'];?></td>
    </tr>
    <?php
        $sno=$sno+1;
    }
    ?>
        </table>
        
        <?php
    if(isset($_POST['Submit']))
        
        {
            $checkbox=$_POST['checkbox'];
            for($i=0;$i<count($checkbox);$i++)
            {
            $del_id = mysql_real_escape_string($checkbox[$i]);        
            $sql = "INSERT INTO projectassign(projectassign,userid)values('".mysql_real_escape_string($_POST['projectassign'])."','$del_id')";
             echo $sql;
             mysql_error();
            $result = mysql_query($sql);
           
            }
        }    
        
        ?>
        <input type="submit" name="Submit"  value="Submit">
        </form>
        </body>
        </head>
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    When you select the projects for you <option> boxes, you specify that the value of each should be a "projectid", but you fail to fetch such a row from your database. Thus, the value attribute of all the projectassign options is empty, which is the value put into the projectassign table where the project id should go.

    If you turn the error messages on, PHP should give you a warning that the "projectid" index doesn't exist.

    P.S.
    Putting mysql_error() by itself without printing it is pointless. It returns an error, it does not print it. You need to do that yourself with an echo or print. It's also quite popular to put it in a "or die()" clause following the mysql_query() call.

    Comment

    • niths
      New Member
      • Mar 2010
      • 18

      #3
      i didnt understand wat u said. can u please do that in my code..so that i can come to know. thank u.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        On line #18 you try to use a "projectid" from the query on line #12. But that column isn't in your query. - If you want to use a column from your table in your code, it needs to be included in the query.

        Consider this:
        [code=php]<?php
        $sql = "SELECT `one`, `two`
        FROM `my_table`";
        $result = mysql_query($sq l);
        $row = mysql_fetch_ass oc($result);

        // These will print the values, as expected.
        echo $row['one'];
        echo $row['two'];

        // But this will print nothing, because
        // this field was not retrieved in the query.
        echo $row['three'];
        ?>[/code]

        Comment

        Working...