I am very new to PHP/HTML/CSS programming and in the code I have attached bellow is my attempt to create the basic functionality of an administration panel for a website.
What I want to do is to print out the table with all of its rows and columns and to have an additional column with controls which would allow me to delete said row from a database. Eventually I would like to make it possible to change the name, password and administrator privileges for each user.
Additionally, I really have no idea how to make each button to hold a value connecting it to its respective row.
Perhaps due to me being an astoundingly inexperienced programmer all of my attempts have either failed or deleted the last row (perhaps as it was the last value under the $email
variable name). A friend suggested to use either JavaScript or to move to a different platform (Angular JS was his suggestion) in order to achieve my goals but for now I would really like to keep it simple (if that is really the word for it) with PHP and CSS.
Here is an image of what the administration panel looks like:
Here is my table generator (or as good as I managed to get it):
<?php
include "connection.php";
$sql = "SELECT * FROM users;";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table class='sqltable'>
<tr class='sqltable'>
<th class='sqltable'>ID</th>
<th class='sqltable'>EMAIL</th>
<th class='sqltable'>NAME</th>
<th class='sqltable'>IS ADMIN</th>
<th class='sqltable'>PASSWORD</th>
<th class='sqltable'>CONTROLS</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr class='sqltable'>
<td class='sqlcell'>".$row["ID"]."</td>
<td class='sqlcell'>".$row["EMAIL"]."</td>
<td class='sqlcell'>".$row["FIRST_NAME"]." ".$row["MID_NAME"]." ".$row["LAST_NAME"]."</td>
<td class='sqlcell'>".$row["IS_ADMIN"]."</td>
<td class='sqlcell'>".$row["PASSWORD"]."</td>
<td class='sqlcell'>
<center>
<div style='border: 1px solid lightgray;' method='POST'>
<input type='hidden' name='ID' value='".$row['ID']." '/>
<input type='button' name='delete' value='DEL ".$row['ID']." '/>
</div>
</center>
</td>
</tr>";
}
echo "</table>";
}
else
{
echo "DATABASE IS EMPTY!";
}
$conn->close();
if (isset($_POST['delete']))
{ //if a delete request received
$id = $_POST['id']; //primary key of this row
/////// Connectivity /////////
$servername = "127.0.0.1";
$username = "root";
$password = "";
$db = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
//check connection
if ($conn)
{
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
//compose sql statement
$stmt = mysqli_prepare($conn, "DELETE FROM users WHERE ID=?");
mysqli_stmt_bind_param($stmt,'i',$id); //now add the $id to the statement 'i' stands for integer
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($conn); //connection closed
}
?>
This is what I started doing and I am already pretty sure that I have taken the wrong route to do this.
function delete()
{
$del = "DELETE FROM '".$table."' WHERE EMAIL='".$email."';";
$conn->query($del);
}