0

If you will just refer to the code below, I as trying to add a delete link inside a gridview. And at the same time if I click it, I want to delete that line from the database. Please help me out.

Here's my HTML php code:

<?php
session_start();
$loginuser = $_SESSION['result'];

if ($loginuser['position'] != "HR Assistant" ) {

}  
else{
header("location:HRpage.php");
 } 
?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-   ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
 $(function() {
   $( ".datepicker" ).datepicker({
       dateFormat: "yy-mm-dd",
    minDate: 0
});
  });
  </script>
 <html>
<head>
<link rel="stylesheet" href="master.css" type="text/css">
</head>
<body>
    <div class="divpage">
        <br>
        <div class="divfloatholder">
            <div style="height: 50px; width: 98%;margin-left: 5px;border:inset; background:#099DD9">
                <div>
                    <div class="textheader">
                         <p class="editedp"><label class="labelheader">Employee:</label>&nbsp&nbsp<?php echo $loginuser['fullname']?></p>
                    </div>
                    <div class="textheader">
                         <p class="editedp"><label class="labelheader">Email:</label>&nbsp&nbsp&nbsp<?php echo $loginuser['email']?></p>
                    </div>
                    <div style="width:30%; float: right; text-align: right;">
                         <p style="margin-top: 3px; margin-bottom: 1px"><a href="">Settings</a>|<a href="logout.php">Log Out</a></p>
                    </div>
                </div>
                    <div class="textheader">
                         <p class="editedp"><label class="labelheader">Position:</label>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<?php echo $loginuser['position']?></p>
                    </div>
                    <div class="textheader">
                         <p class="editedp"><label class="labelheader">Shift:</label>&nbsp&nbsp&nbsp&nbsp<?php echo $loginuser['startofshift']?>-<?php echo $loginuser['endofshift']?></p>
                    </div>
            </div>
            <div class="rightdivpage">
                <form action="insert.php" method="post">
                <div>
                    <div style="height: 90px; width: 47.5%; margin-left: 5px; float: left; text-align: left;">
                        <p class="editedp"><label>Request Date:</label>&nbsp&nbsp&nbsp&nbsp<?php
                            $currentDate = date("F j, Y");
                            echo $currentDate."<br/>";
                        ?></p>
                        <p class="editedp"><label>Start Date:</label>&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" class="datepicker" name="startdate"></p>
                        <p class="editedp"><label>End Date:</label>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" class="datepicker" name="enddate"></p>
                    </div>
                    <div style="height: 90px; width: 47.5%;margin-left: 5px; float: right; text-align: left;">
                        <p class="editedp"><label>Reason:</label>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="reason"></p>
                        <p class="editedp"><label>Type of Leave:</label>&nbsp&nbsp&nbsp&nbsp 
                            <select class="custom-select" name="typeofleave">
                                        <option>Select Type of Leave</option>
                                        <option>Sick Leave</option>
                                        <option>Vacation Leave</option>
                                        <option>Paid Leave</option>
                                        <option>UnPaid Leave</option>
                            </select></p>
                        <p class="editedp"> <button>Submit</button></p>
                    </div>
                </div>
                <div style="height:70%;">
                    <?php
                    $con=mysqli_connect("localhost","root","","leavecalendar");
                    // Check connection
                    if (mysqli_connect_errno())
                    {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                    }

                    $result = mysqli_query($con,"SELECT * FROM leavedetails where username = '$loginuser[username]'");

                    echo "<table border='1' class='form-table' width='10%'>
                    <tr>
                          <th>Leave Type</th>
                          <th>Start Date</th>
                          <th>End Date</th>
                          <th>Reason</th>
                          <th>Status</th>
                    </tr>";

                    while($row = mysqli_fetch_array($result))
                    {
                    echo "<tr>";
                    echo "<td>" . $row['typeofleave'] . "</td>";
                    echo "<td>" . $row['stardate'] . "</td>";
                    echo "<td>" . $row['endate'] . "</td>";
                    echo "<td>" . $row['reason'] . "</td>";
                    echo "<td>" . $row['status'] . "</td>";
                    echo "</tr>";
                    }
                    echo "</table>";

                    mysqli_close($con);
                ?>
                </div>
                <br>
                <input type="submit" name="Submit" value="Submit">
            </div>
        </form>
            <div style="float:left; width: 23%; height: 88%; border: solid; margin-left: 5px; margin-top:5px;">
                asdsadsad
            </div>
        </div>
    </div>  
</body>

Now here's my code for deletion:

<?php
session_start();
$loginuser = $_SESSION['result'];
$con=mysqli_connect("localhost","root","","leavecalendar");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//$myusername=$_POST['username']; 
//$mypassword=$_POST['startdate']; 

$sql="DELETE FROM `leavedetails` WHERE username = '$loginuser[username]' and reason =     '$_POST[reason]'"
echo ($sql);
 if (!mysqli_query($con,$sql))
   {
  die('Error: ' . mysqli_error($con));
  }
 //echo "1 record added";
 header("location:login_success.php");


mysqli_close($con);
?>
Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
user3465736
  • 51
  • 1
  • 1
  • 8

0 Answers0