Home » Php » php – Updating department and comments

php – Updating department and comments

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am very new to php and have started a project where user creates a proposal and then user1 selects a department with comment and submit. On submit the proposal created by user1 with department and comments goes to user2 for approval and reject. Now user2 will check the proposal and comment if needed for approval and reject. Here the code i am trying to do is update the department and comment section at the user1 level and then after submitting with department and comment by user1 then only user2 can approve/reject with comment?

  1. Create proposal.php
<?php


$host="localhost";
$username="root";
$pass="";
$db="update";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
    die("Database connection error");
}

// insert query for register page
if(isset($_REQUEST['proposal']))
{

$details=$_POST['details'];
$location=$_POST['location'];
$date=$_POST['date'];
$time=$_POST['time'];
$checkbox=$_POST['checkbox'];
$status="Pending";
$comment=$_POST['comment'];
$department="MMD";


     $query="INSERT INTO `ronel` (`details`,`location`,`date`,`time`,`checkbox`,`status`,`comment`,`department`) VALUES ('$details','$location','$date','$time','$checkbox','$status','$comment','$department' )";

    $res=mysqli_query($conn,$query);
    if($res){
        $_SESSION['success']="Not Inserted successfully!";
        header('Location:');
    }else{
        echo "Leave not Applied, please try again!";
    }
    }
?>

<div class="col-xs-6 col-xs-push-3 well">
<form class="form-horizontal" method="post" action="" >
<input type="hidden" name="proposal" value="">
  <fieldset>
    <legend>New Proposals  </legend>
            <!----left box----------->
        <!----right box----------->
    <div class="col-xs-9">
<div class="form-group">
      <label for="inputEmail" class="col-lg-3"><b>Details:</b></label>
      <div class="col-lg-9">
        <textarea rows="8" cols="128" name="details" class="form-control"></textarea>
      </div>
    </div>
   <div class="form-group">
      <label for="inputEmail" class="col-lg-3"><b>Location:</b></label>
      <div class="col-lg-9">
        <input type="text" name="location" class="form-control" >
      </div>
    </div>
    <div class="form-group">
      <label for="inputEmail" class="col-lg-3"><b>Date:</b></label>
      <div class="col-lg-9">
        <input type="date" name="date"  class="form-control">
      </div>
    </div>
    <div class="form-group">
      <label for="inputEmail" class="col-lg-3"><b>Time:</b></label>
      <div class="col-lg-9">
        <input type="time" name="time"  class="form-control" >
      </div>
    </div>
    <br> <br>    
    <div class="form-group">
      <label style="position:relative; bottom: 23px;left: 100px; font-size:19px;color:orangered;">Classification(Tick the appropriate one,leave blank if you find it difficult) :</label>
      <div class="col-lg-9">
       <input type="checkbox" name="checkbox" value="Nearmiss case" class="form-control" style="position:relative; left:50%; ">1. Nearmisscase<br>
                        <input type="checkbox" value="First Aid Case" name="checkbox" style="position:relative; left:50%;  ">2. First Aid Case<br>
                        <input type="checkbox" value="Lost Time Injury" name="checkbox" style="position:relative; left:50%; ">3. Lost Time Injury<br>
                        <input type="checkbox" value="Fatal" name="checkbox" style="position:relative; left:50%;  ">4. Fatal<br>
                        <input type="checkbox" value="Fire" name="checkbox" style="position:relative; left:50%;  ">5. Fire<br>
                        <input type="checkbox" value="Emission/Discharge/Spill/Leak(Abnormal)" name="checkbox" style="position:relative; left:50%;  ">6. Emission/Discharge/Spill/Leak(Abnormal)<br>
                        <input type="checkbox" value="Property Damage" name="checkbox" style="position:relative; left:50%;  ">7. Property Damage<br>
                        <input type="checkbox" value="HIPO" name="checkbox" style="position:relative; left:50%;  ">8. HIPO

      </div>
    </div>
      <div class="col-lg-9">
          <input type="hidden" name="status" class="form-control" >
      </div>

    <div class="form-group">
      <label for="inputEmail" class="col-lg-3"><b></b></label>
      <div class="col-lg-9">
          <input type="hidden" name="comment"  class="form-control">
      </div>
        <div class="col-lg-9">
          <input type="hidden" name="department" class="form-control" >

      </div>

    </div>
    </div>


    <div class="form-group">
      <div class="col-lg-12">
        <button type="reset" class="btn btn-default">Cancel</button>
        <button type="submit" class="btn btn-primary">Submit</button>
      </div>
    </div>
  </fieldset>
</form>
</div>
</div>  
  1. Dashboard.php
<?php
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root", "","update");

    $res = $conn->query("SELECT id
                              , location
                              , status
                              , CASE WHEN LENGTH(comment) > 45
                                     THEN CONCAT(LEFT(comment,45),'&hellip;') 
                                     ELSE comment
                                END as comment
                          FROM ronel
                          ORDER BY id    
                        ");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-language" content="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Dashboard</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style type='text/css'>
    body  { font-family: calibri, sans; font-size: 12pt; }
</style>
</head>
<body>
<header class="w3-container w3-orange w3-padding w3-center">
    <h2>Dashboard</h2>
</header>
<div class="w3-content w3-padding">
    <table class="w3-table w3-bordered">
        <tr style="background-color:#E4EBC5; color:orangered;">
            <th>ID</th>

            <th>Location</th>

            <th>Status</th>
            <th>Comment</th>

        </tr>


    <?php
        if ($row = $res->fetch_assoc()) {
            do {
                switch ($row['status'])  {
                    case 'pending':
                        $cls1 = 'class="w3-light-gray"';
                        $cls2 = 'class="w3-dark-gray"';
                        break;
                    case 'approved':
                        $cls1 = '';
                        $cls2 = 'class="w3-pale-green"';
                        break;
                    case 'rejected':
                        $cls1 = '';
                        $cls2 = 'class="w3-pale-red"';
                }
                echo "<tr $cls1>
                         <td>{$row['id']}</td>

                         <td>{$row['location']}</td>

                         <td $cls2>{$row['status']}</td>
                         <td>{$row['comment']}</td>
                         <td><a href='ronel_detail.php?id={$row['id']}' class='w3-button w3-tiny w3-khaki'>View Details</a></td>
                      </tr>
                     ";
            } while ($row = $res->fetch_assoc());
        }
        else {
            echo "<tr><td colspan='7'>No records</td></tr>" ;
        } 

    ?>

    </table>
</div>
</body>
</html>
  1. Proposaldetail.php
<?php
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root", "","update");

//
//   PROCESS POSTED DATA
//
        if ($_SERVER['REQUEST_METHOD']=='POST') {
            $stmt = $conn->prepare("UPDATE ronel
                                    SET
                                        comment = ?,
                                        department = ?
                                    WHERE id = ?
                                    ");
            $stmt->bind_param('ssi', $_POST['comment'], $_POST['department'], $_POST['id']);
            $stmt->execute();

            header("Location: dashboard.php");
            exit;
        }


if (!isset($_GET['id']) || trim($_GET['id'])=='') {
    header("Location: ");
    exit;
}

$res = $conn->prepare("SELECT id

                            , location

                            , status
                            , comment
                            , department
                       FROM ronel
                       WHERE id = ?
                       ");
$res->bind_param('i', $_GET['id']);
$res->execute();
$res->bind_result($id, $location, $status, $comment, $department);
$res->fetch();
$res->close();

//
//  status-dependent processing
//
$buttons = "<div class='data'>
            <button name='status','department' class='w3-button w3-khaki' value='$status','$department'>Update</button> 
            </div>
            ";

?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-language" content="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Details</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style type='text/css'>
    body  { font-family: calibri, sans; font-size: 12pt; }
    label { display: inline-block; width: 150px; background-color:#E4EBC5; color:orangered; padding: 8px; font-weight: 600; margin-right:30px; vertical-align: top;}
    .data { display: inline-block; width: 500px; padding: 8px; vertical-align: top;}
    /*.comment { display: inline-block; width: 450px; vertical-align: top;}  */
</style>
</head>
<body>
<header class="w3-display-container w3-orange w3-padding" style='height:90px;'>
    <div class="w3-left">
        <h2>Details</h2>
    </div>
    <div class="w3-right w3-margin">
        <a href='dashboard.php' class='w3-khaki w3-button'>Dashboard</a>
    </div>
</header>
<div class='w3-container'> 
    <form method='POST'>

    <input type='hidden' name='id' value='<?=$id?>'>
    <div class="w3-card w3-padding" style='max-width: 750px; margin: 30px auto;'>
        <div>
            <label>ID</label>
            <div class="data"><?=$_GET['id']?></div>
        </div>

        <div>
            <label>Location</label>
            <div class="data"><?=$location?></div>
        </div>

        <div>
            <label>Status</label>
            <div><?=$status?></div>
        </div>
        <div>
            <label>Comments</label>
            <div class="data"><textarea name='comment' class='w3-input w3-border' rows='5'><?=$comment?></textarea></div>
        </div>
        <div><label>Select Department:</label>
<div class="data" name='department'>
<select id='department' >
  <option value="MMD">MMD</option>
  <option value="O&M">O&M</option>
  <option value="Civil">Civil</option>
  <option value="C&M">C&M</option>
</select>
</div>  
</div>
    </div>
    <div class='w3-content w3-center w3-padding'>
        <?=$buttons?>
    </div>
    </form>
</div>
</body>
</html>

How to&Answers: