<?php
    include("connect.php");

    if(isset($_POST['add'])){
        header("location:show_data.php");
        $first=$_POST['fname'];
        $last=$_POST['lname'];
        $fulname=$first." ".$last;
        $gen=$_POST['gender'];
        $hob_array=$_POST['hob'];
        $status=$_POST['status'];

        foreach($hob_array as $one_hob){
            $source .= $one_hob.", ";
        }
        $hobb = substr($source, 0, -2);
//insert data
        $query_insert="INSERT INTO employee(name,gender,hobbies,status) VALUES('$fulname','$gen','$hobb','$status')";
        if(mysqli_query($con,$query_insert)){
            echo "inserted";
        }else{
            echo "no";
        }
    }

    if(isset($_POST['show'])){
        header("location:show_data.php");
    }

    if(isset($_POST['insert'])){
        header("location:insert.php");
    }

    if(isset($_POST['update'])){
        $fname=$_POST['fname'];
        $lname=$_POST['lname'];
        $fulname=$fname." ".$lname;
        $gen=$_POST['gender'];
        $hob_array=$_POST['hob'];
        $status=$_POST['status'];
        $id=$_REQUEST['emp_id'];

        foreach($hob_array as $one_hob){
            $source .= $one_hob.", ";
        }
        $hobb = substr($source, 0, -2);

        $query_edit="UPDATE employee SET name='$fulname',gender='$gen',hobbies='$hobb',status='$status' WHERE id='$id'";
        if(mysqli_query($con,$query_edit)){
            header("location:show_data.php");
        }else{
            echo "Error Updating Data";
        }
    }

    $num=$_GET['emp_id'];
    $query_select_data="SELECT * FROM employee where id=$num";
    $result2=mysqli_query($con,$query_select_data);
    if($result2){
        while($row=mysqli_fetch_array($result2)){
            list($fname,$lname)=explode(" ",$row['name']);
            $gen=$row['gender'];
            $focus=explode(",",$row['hobbies']);
            print_r($focus);
            $status = $row['status'];
        }
    }
?>

<html>
<head>
    <style>
        table{
            margin: 50 auto;
            width: 600px;
            border-collapse: collapse;
        }
    </style>
</head>
<body>
    <?php
        if($_GET['emp_id'] != ''){
    ?>
    <h3>Update Data</h3>
    <?php }else{?>
    <h3>Insert Data</h3>
    <?php }?>
    <form name="frm" method="POST">
        <table border="1" cellpadding="5">
            <tr>
                <td>First Name: </td>
                <td><input type="text" name="fname" value="<?php echo $fname; ?>" /></td>
            </tr>
            <tr>
                <td>Last Name: </td>
                <td><input type="text" name="lname" value="<?php echo $lname; ?>" ></td>
            </tr>
            <tr>
                <td>Gender: </td>
                <td>
                    <input type="radio" name="gender" value="Male" <?php echo ($gen=='Male')?"checked":"" ;?>>Male
                    <input type="radio" name="gender" value="Female" <?php echo ($gen=='Female')?"checked":"" ;?>>Female
                </td>
            </tr>
            <tr>
                <td>Hobbies: </td>
                <td>
                    <?php
                        if($_GET['emp_id'] == ''){
                            foreach ($hobby_name as $key) {
                    ?>
                    <input type="checkbox" name="hob[]" value="<?php echo $key;?>"><?php echo $key;?>
                    <?php
                            }
                        }else{
                            foreach ($hobby_name as $key) {
                    ?>
                    <input type="checkbox" name="hob[]" value="<?php echo $key;?>" <?php foreach($focus as $key1){if(in_array($key1,$hobby_name)) {?> checked="checked" <?php } }?>><?php echo $key;?>
                    <?php
                            }
                        }
                    ?>

                </td>
            </tr>
            <tr>
                <td>Status: </td>
                <td>
                    <select name="status">
                        <option value="0" <?php if (!empty($status) && $status == '0')  echo 'selected = "selected"'; ?>>Select</option>
                        <option value="Active" <?php if (!empty($status) && $status == 'Active')  echo 'selected = "selected"'; ?>>Active</option>
                        <option value="Inactive" <?php if (!empty($status) && $status == 'Inactive')  echo 'selected = "selected"'; ?>>Inactive</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <?php
                        if($_GET['emp_id'] != '')
                        {
                    ?>
                            <input type="submit" name="update" value="Update">  
                            <input type='submit' name='insert' value='Insert New Record'>  
                    <?php
                        }
                        else{
                    ?>
                            <input type="submit" name="add" value="Submit">  
                    <?php
                        }
                    ?>
                        <input type="submit" name="show" value="Show Table Data">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

this is my code and i want only selected values to be checked but it shows all values to be checked...how to do this????

Member Avatar for iamthwee

All your conditions result in selected = selected!

Member Avatar for iamthwee

Also escape your queries!

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.