0

hi i am new to php. I am having some problem.
i had created a dropdown list which contains project names. and below that a table with checkbox userid,username. till tat it is working properly.
after selecting the project name and one cheque box and press submit button.It should display in the database table, as userid, project id and assigned date. but i am not getting that code so please can anyone.
thank u.

<?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='$_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='$_SESSION[username]' and role='2'";
    mysql_error();
    $result2=mysql_query($sql2);
    $num=mysql_num_rows($result2);
    mysql_close($con);
    ?>    
<head>
<body>
<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="id">
        <option value=0 >---select----<?=$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>
     <input type="submit" name="Submit" id="Submit" value="Submit">
    <?php
    if($_POST['Submit']=='Submit')
    {
        
        for($i=0;$i<count($checkbox);$i++)
        {
            $userid = $checkbox[$i];
            $sql3 = "Insert into projectassign(projectid,userid,assigneddate)values('$_POST[projectid]','$userid','')";
            mysql_error();
            $result = mysql_query($sql);
        }
        
    }      
    ?>
   
    </body>
    </head>
3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by JRM
0

I just skimmed your code but a couple of thoughts come to mind.

1. PHP files are read from top to bottom. Any action you put at the bottom of the file will NOT be called until the bottom of the page is reached. Once the client's browser finishes loading the page the PHP file is done and over with. In your example you seem to expect the PHP to pause and wait for the user to click submit or something.

2. I suggest you use a form tag.

3. I guess the level of security you have in your script is okay for private playing around with but if you ever think of putting it on the big bad net... google 'mysql PHP security' sometime.

0

keep in mind that that entire page of code re-runs from the top down when you you hit submit...or at least it should. As mentioned by sunsetrainbow, you should use the the form tag to tell PHP what action to take , ie; what form handler to use upon a submit .

Also, you might be able to better control what is happening by separating the code into functions that get called if POST is true or not.
You might have a problem here:

// I'm not sure if this will work if Submit is capitalized...
if($_POST['Submit']=='Submit')
// keep it simple with something like this:
if isset ($_POST) // only true after a submit
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.