0

Hi Guys,
Help me solve this...
First I fetch values from DB and shows it in a table with check boxes. Once clicking OK button, it shows selected check box values. User modify any values and click edit, it should update the DB. I don't know where to fix the 'update query'. I tried in foreach loop. Can anyone help me to solve this. Or any other easy method to do this. Because I feel that 2 times fetching from DB is unnecessary. Here is my coke. Table contains 2 fields, name & age.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
    $dbcon = mysql_connect("localhost", "root", "");
    if(!$dbcon)
        {
            die('Couldnot connect:'. mysql_error());
        }
    else
        { echo "Connected successfully"; }

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Foreach Loop</title>
</head>

<body>
    <form name="form1" action="#" method="post">
        <table border="0" cellpadding="0" cellspacing="0"  >
            <?php
                mysql_select_db("rakesh", $dbcon);  
                $qry = mysql_query("select * from student");
                while($res = mysql_fetch_array($qry))
                    {?> 
                        <tr>
                            <td>
                                <input type="checkbox" name="chkbx[]" value="<?php echo $res[0]; ?>"  />                          
                                <input style="color:#FF0000" type="text" name="stu_name" value="<?php echo $res[1]; ?>"  />
                                <input style="color:#FF0000" type="text" name="stu_age" value="<?php echo $res[2]; ?>"  />

                            </td>
                        </tr>                    
                    <?php
                    }
            ?>          
            <tr> <td> <input type="submit" name="ok" value="ok" /> </td> </tr>
        </table>
<?php
    $chkbx=@$_POST['chkbx'];
    //print_r($chkbx);
    $ok=@$_POST['ok'];
    $stu_name=@$_POST['stu_name'];
    //print_r($stu_name);
    $stu_age=@$_POST['stu_age'];
    $stu_nm=@$_POST['stu_nm'];
    $stu_ag=@$_POST['stu_ag'];
    $edit=@$_POST['edit'];

    if(isset($ok))
        {
            foreach($chkbx as $i)
            {

                //echo $i;
                $qry1 = mysql_query("select * from student where id=$i");
                $res1=mysql_fetch_row($qry1);
                ?>
                    <tr>
                        <td>
                            <input type="text" name="stu_nm" value="<?php echo $res1[1]; ?>"  />
                            <input type="text" name = "stu_ag" value="<?php echo $res1[2]; ?>"  />
                        </td>
                    </tr>
                    <br/>
                <?php 
            }?>

         <tr> <td> <input type="submit" name="edit" value="edit"  /> </td> </tr>
         <?php
        }
?>
</form>
</body>
</html>

Edited by Reverend Jim: Fixed formatting

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by diafol
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.