1

I have done this code in viewstudents.php the deletion process worked with the image but I'm not able to do the javascript part inside php inside the echo i mean can someone help me out. here is my code:

 <?php
session_start();
include("connection.php");
if(!isset($_SESSION['user']))
    header("location:login.php");
$qry="SELECT * FROM studentinformation";

$result= mysqli_query($con,$qry) or die (mysqli_error($con));
echo "Welcome ".$_SESSION['user'];
echo "<table border =1 width=80%><tr><td>FullName</td><td>Email</td><td>Password</td></tr>";

while($i = mysqli_fetch_array($result)){
    echo "<tr>";
    echo "<td>".$i['FullName']."</td>";
    echo "<td>".$i['email']."</td>";
    echo "<td>".$i['Password']."</td> ";
    echo "<td><a href=DeleteStudentAction.php?Key=".$i['ID']."onclick="return confirm('Are you sure?')" ><img src = delete-1432400-1211078.png style = width:35px;hight:20px;> </img> </a></td>";

    echo "</tr>";
}

echo "</table>";




?>

it worked fine with the image but I want to have a confirmation prompt that's not working can someone help me out please. Here is my php action for deletion from the database in DeleteStudentAction.php

I really appreciate if someone would help me out because I'm new to php. Thanks!

  • Check your rendered output your html is going to be malformed, eg your href attribute is combining with your onclick attribute thus the parser isnt going to see that as a onclick attribute but rather a long url instead – Patrick Evans Nov 08 '19 at 16:27
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Nov 11 '19 at 18:49
  • Always `exit()` after `header('Location: ...');` Your code is going to be execute even if the user is not logged in! – Dharman Nov 11 '19 at 18:49

1 Answers1

1

You haven't got quotes around your href, try this:

while($i = mysqli_fetch_array($result)) {
 echo '<tr>
        <td>' . $i['FullName'] . '</td>
        <td>' . $i['email'] . '</td>
        <td>' . $i['Password'] . '</td>
        <td>
            <a href="DeleteStudentAction.php?Key=' . $i['ID'] . '" onclick="return confirm(\'Are you sure?\')">
                <img src="delete-1432400-1211078.png" style="width:35px; height:20px;" />
            </a>
        </td>
    </tr>';

}
Mark
  • 1,852
  • 3
  • 18
  • 31