I have a page to manage the users and it retrieves users fine but when I try to remove a user nothing happens.
I'm running on php 7.2 with apache2. I've checked logs and nothing breaks or gives a warning/info level. I've tried switching the query and variables and the location however it doesn't seem to take effect.
<!DOCTYPE html>
<html lang="en">
<?php
$con=mysqli_connect("localhost:3306","user","pass","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT `id`,`username`,`email`,`created_at`,`is_admin` FROM users ";
$result = mysqli_query($con,$query);
//if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['submit'])){
if (empty($_POST["id"])) {
$error = "Make a Selection";
} else {
$delete_id = test_input($_POST["id"]);
$sql = mysqli_query($con,"DELETE FROM users WHERE id='$delete_id'");
echo "<p>" . var_dump($sql) . "</p>";
if($sql){
echo "Deleted";
}
}
}
?>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body{
font: 14px sans-serif;
}
.wrapper{
width: 350px;
vertical-align: middle;
padding: 20px;
}
</style>
</head>
<body>
<form method="post">
<table border='1'><tr><th></th><th>username</th><th>email</th><th>date joined</th><th>Permissions</th></tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
$id = $row['id'];
if ($row['is_admin'] == 1) {
$permissions ="Admin";
}
else {
$permissions = "User";
}
echo "<tr>";
echo '<td><input type="checkbox" name="ID" value="' . $id . '"></td>';
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "<td>" . $permissions . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<BR>";
echo "<BR>";
echo "<BR>";
echo '<input type="submit" name="submit" class="btn btn-primary" value="Delete">        ';
echo '<input type="submit" class="btn btn-primary" value="Reset PW"><BR/><BR />';
echo "</form>";
mysqli_close($con);
?>
</body>
</html>
I expect the user selected to be deleted but the deletion doesn't happen
Try the following:
replace:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
with:
if(isset($_POST['submit'])){
and add name="submit" to your submit button
Related
So I have a website that runs a cycle on a database as long as there is a record in it.
It also puts two buttons in each row within the table.
Each button has the same name.
If I press one of the buttons, how can I get the ID of that button (which points to a Youtube link from the database) to be included in a SQL query.
Here is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ruben MediaShare</title>
</head>
<body>
<form method='POST'>
<?php
session_start();
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
echo "Welcome to the member's area, " . $_SESSION['modname'] . "!";
} else {
echo "Please log in first to see this page.";
}
$link = mysqli_connect("localhost", "asd", "asd", "mediareq");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM requests";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>link</th>";
echo "<th>Twitch Név</th>";
echo "<th>Üzenet</th>";
echo "<th>Engedélyezés</th>";
echo "<th>Elutasítás</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
$link = $row['link'];
echo "<td>" . "<iframe width='560' height='315' src='$link' frameborder='0' allowfullscreen></iframe>" . "</td>";
echo "<td>" . $row['ttvname'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "<td>" . "<input type='submit' id='$link' value='' name='enable'></input>" . "</td>";
echo "<td>" . "<input type='submit' id='$link' value='' name='eject'></input>" . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
if (isset($_POST['enable'])) {
$ID = $_POST[$link];
echo $ID;
}
} else{
echo "Még nem kaptál videót, várjál már egy kicsit vagy csak tolj egy F5öt Pog Pog Pog";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</form>
</body>
</html>
The website looks like this: https://imgur.com/a/MWVj7L5
If it can't be done that way, what can I do in this case to keep the loop but since it has to be dynamic, and somehow get it to pick the link from each button that it stores in the ID (or some other way)
When I click the delete button it should delete the user from that row from the Database. I also want help for my modify button in this table if I click the modify button it should change the user type (Admin, Chief, user). I already tried everything but I don't know how a can solve it that's why I'm asking your help.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pannel</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body {
font: 14px sans-serif;
text-align: center;
}
</style>
</head>
<body>
<div class="page-header">
<h1> Admin Pannel</h1>
</div>
<div>
</div>
</body>
</html>
<?php
session_start();
require 'config.php';
$strSQL = "SELECT username, email, type FROM benutzer";
$rs = mysqli_query($link, $strSQL);
echo "<table border='1' style='margin: 0 auto'>
<tr>
<th class='text-center'>Name / Vorname</th>
<th class='text-center'>Email</th>
<th class='text-center'>Type</th>
<th class='text-center'>Modify</th>
<th class='text-center'>Delete</th>
</tr>";
while ($row = mysqli_fetch_array($rs)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td><input type='submit' value='Modify' class='btn' name='modify'></td>";
echo "<td><input type='submit' value='Delete' class='btn' name='delete'></td>";
echo "</tr>";
}
echo "</table>";
if (isset($_POST['modify'])) {
$username = $row['username'];
$modify_query = mysqli_query($link, "UPDATE benutzer SET type='Mitarbeiter, Chef' WHERE username=$username");
if ($modify_query) {
mysqli_close($link);
header("location:welcome.php");
exit;
} else {
echo mysqli_close($link);
}
}
if (isset($_POST['delete'])) {
$username = $row['username'];
$delete_query = mysqli_query($link, "DELETE FROM benutzer WHERE id=$username");
if ($delete_query) {
mysqli_close($link);
echo "Record deleted successfully";
exit;
} else {
echo mysqli_close($link);
}
}
?>
Try this you were missing hidden input types so the php can't see your code you submitted.
Since you are showing alot of username's you will need to create a bunch of input hidden types with name=username1, name=username2 etc.. or it won't work well I can show you a easier way to do it (look below this code)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pannel</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body {
font: 14px sans-serif;
text-align: center;
}
</style>
</head>
<body>
<div class="page-header">
<h1> Admin Pannel</h1>
</div>
<div>
</div>
</body>
</html>
<?php
session_start();
require('config.php');
$strSQL = "SELECT username, email, type FROM benutzer";
$rs = mysqli_query($link, $strSQL);
echo "<table border='1' style='margin: 0 auto'>
<tr>
<th class='text-center'>Name / Vorname</th>
<th class='text-center'>Email</th>
<th class='text-center'>Type</th>
<th class='text-center'>Modify</th>
<th class='text-center'>Delete</th>
</tr>";
while ($row = mysqli_fetch_array($rs)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<form action='pannel.php' method='post'>";
echo "<input type='hidden' id=username' name='username' value='".$row['username']."'>";
echo "<input type='hidden' id=email' name='email' value='".$row['email']."'>";
echo "<input type='hidden' id=type' name='type' value='".$row['type']."'>";
echo "<td><input type='submit' value='Modify' class='btn' name='modify'></td>";
echo "<td><input type='submit' value='Delete' class='btn' name='delete'></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
if (isset($_POST['modify'])) {
$username = $_POST['username'];
$modify_query = mysqli_query($link, "UPDATE benutzer SET type='Mitarbeiter, Chef' WHERE username=$username");
if ($modify_query) {
mysqli_close($link);
header("location:welcome.php");
exit;
} else {
echo mysqli_close($link);
}
} else if (isset($_POST['delete'])) {
$username = $_POST['username'];
$delete_query = mysqli_query($link, "DELETE FROM benutzer WHERE username=$username");
if ($delete_query) {
mysqli_close($link);
echo "Record deleted successfully";
exit;
} else {
echo mysqli_close($link);
}
}
EDIT:
Easier way to do it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pannel</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body {
font: 14px sans-serif;
text-align: center;
}
</style>
</head>
<body>
<div class="page-header">
<h1> Admin Pannel</h1>
</div>
<div>
</div>
</body>
</html>
<?php
session_start();
require('config.php');
$strSQL = "SELECT username, email, type FROM benutzer";
$rs = mysqli_query($link, $strSQL);
echo "<table border='1' style='margin: 0 auto'>
<tr>
<th class='text-center'>Name / Vorname</th>
<th class='text-center'>Email</th>
<th class='text-center'>Type</th>
<th class='text-center'>Modify</th>
<th class='text-center'>Delete</th>
</tr>";
while ($row = mysqli_fetch_array($rs)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td><a href='pannel.php?delete=".$row['username']."'>Delete ".$row['username']."</a></td>";
echo "<td><a href='pannel.php?modify=".$row['username']."'>Modify ".$row['username']."</a></td>";
echo "</tr>";
}
echo "</table>";
if (isset($_GET['modify'])) {
$username = $_GET['modify'];
$modify_query = mysqli_query($link, "UPDATE benutzer SET type='Mitarbeiter, Chef' WHERE username=$username");
if ($modify_query) {
mysqli_close($link);
header("location:welcome.php");
exit;
} else {
echo mysqli_close($link);
}
} else if (isset($_GET['delete'])) {
$username = $_GET['delete'];
$delete_query = mysqli_query($link, "DELETE FROM benutzer WHERE username=$username");
if ($delete_query) {
mysqli_close($link);
echo "Record deleted successfully";
exit;
} else {
echo mysqli_close($link);
}
}
I have a data table to show on the table one row is called "address". There are only two types of address can be output "dhaka" and "yaka" . I want to show the font colour as red when the output is "dhaka" and green when the output is "yaka".
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<style type="text/css">
.wrapper{
width: 650px;
margin: 0 auto;
}
.page-header h2{
margin-top: 0;
}
table tr td:last-child a{
margin-right: 15px;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
</script>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<?php
require_once "config.php";
$sql = "SELECT * FROM student_record";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered table-striped table-hover '>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>Name</th>";
echo "<th>Address</th>";
echo "<th>Marks</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['marks'] . "</td>";
echo "<td>";
echo "<a href='read.php?id=". $row['id'] ."' title='View
Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='update.php?id=". $row['id'] ."' title='Update
Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". $row['id'] ."' title='Delete
Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
mysqli_close($conn);
?>
<form method="post" action="search1.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
</div>
</div>
</div>
</div>
</body>
</html>
You need to actually use if elseif statements:-
<?php
if ($row['address'] == 'dhaka') {
$color = 'RED';
} elseif ($row['address'] == 'yaka') {
$color = 'GREEN';
}
echo "<td><span style=\"color: $color\">" . $row['address'] . "</span></td>";
Please try to include this code inside while loop
if($row['address'] =='dhaka'){$color='red';}
else{$color='green';}
And update the address line with
echo "<td style='color:'.$color.'>" . $row['address'] . "</td>";
I outputed the data from my database in a HTML Table and styled it with CSS. Everything is fine, but there is an awkward White space above the outputed Table. (see picture). What's wrong with my code?
Result:
<html>
<head>
<title>Create new user</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<div>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from employee";
$result = $conn->query($sql);
echo "<table>";
echo "<th>Identifier</th><th>Name</th><th>Lastname</th>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
echo "</br>";
}
}
else {
echo "0 results";
}
echo "</table>";
$conn->close();
?>
</div>
</body>
</html>
Expected result : no white space above the Table
Many Thanks
Why are you adding a <br>
echo "</br>";
in your while loop.
Try this -
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
If this does not fix, try inspecting and see what is adding space. Also check for the padding and margins in the computed view in chrome inspect view.
If nothing works, try setting top:0 for the div and may be for table :)
i want to display users data in a table with the dropdown list , the first option shows all the users but when i click a name it still shows all users, i think i have some trouble in my ajax script because it deosn't seem to work
this is the page where i display data :
<?php
//load_data_select.php
$connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence");
function fill_emp($connect)
{
$output = '';
$sql = "SELECT * FROM employés";
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_array($result))
{
$output = $output."'<option value="'.$row["IdEmp"].'">'.$row["NomEmp"].'</option>'";
}
return $output;
}
function fill_Abs($connect)
{
$output = '';
$sql = "SELECT NomEmp,PrénomEmp,DateD,DateF,CauseAbs,justifié
FROM absence.absences
WHERE
MONTH(DateF) = MONTH(NOW())
AND
YEAR(DateF) = YEAR(NOW());";
$result = mysqli_query($connect, $sql);
echo "<table>
<tr>
<th>NomEmp</th>
<th>PrénomEmp</th>
<th>DateD</th>
<th>DateF</th>
<th>CauseAbs</th>
<th>justifié<th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NomEmp'] . "</td>";
echo "<td>" . $row['PrénomEmp'] . "</td>";
echo "<td>" . $row['DateD'] . "</td>";
echo "<td>" . $row['DateF'] . "</td>";
echo "<td>" . $row['CauseAbs'] . "</td>";
echo "<td>" . $row['justifié'] . "</td>";
echo "</tr>";
}
$output="</table>";
return $output;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial | Multiple Image Upload</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
/>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
</script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js">
</script>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
<script>
$(document).ready(function(){
$('#nom').change(function(){
var NomEmp = $(this).val();
$.ajax({
url:"usr.php",
method:"POST",
data:{NomEmp:NomEmp},
success:function(data){
$('#show_Abs').html(data);
}
});
});
});
</script>
</head>
<body>
<br /><br />
<div class="container">
<h3>
<label for="nom">Selectionnez le nom de l'employé </label>
</br></br>
<select name="nom" id="nom">
<option value="">Tout les employés</option>
<?php echo fill_emp($connect); ?>
</select>
<br /><br /> <br />
<div class="row" id="show_Abs">
<?php echo fill_Abs($connect);?>
</div>
</h3>
</div>
</body>
</html>
and this is the usr.php :
<?php
//load_data.php
$connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence");
$output = '';
if(isset($_POST["NomEmp"]))
{
if($_POST["NomEmp"] != '')
{
$sql = "SELECT NomEmp,PrénomEmp,DateD,DateF,CauseAbs,justifié
FROM absence.absences
WHERE
MONTH(DateF) = MONTH(NOW())
AND
YEAR(DateF) = YEAR(NOW()) WHERE NomEmp = '".$_POST["NomEmp"]."'";
}
else
{
$sql = "SELECT NomEmp,PrénomEmp,DateD,DateF,CauseAbs,justifié
FROM absence.absences
WHERE
MONTH(DateF) = MONTH(NOW())
AND
YEAR(DateF) = YEAR(NOW());";
}
$result = mysqli_query($connect, $sql);
$output="<table>
<tr>
<th>NomEmp</th>
<th>PrénomEmp</th>
<th>DateD</th>
<th>DateF</th>
<th>CauseAbs</th>
<th>justifié<th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NomEmp'] . "</td>";
echo "<td>" . $row['PrénomEmp'] . "</td>";
echo "<td>" . $row['DateD'] . "</td>";
echo "<td>" . $row['DateF'] . "</td>";
echo "<td>" . $row['CauseAbs'] . "</td>";
echo "<td>" . $row['justifié'] . "</td>";
echo "</tr>";
}
echo $output;
}
?>
can anyone help me to find the problem in this ? any help would be appreciated.
Thanks.
We could use a little more info to help with this, but I'll try to get the ball rolling.
I would guess that $_POST["NomEmp"] in usr.php is === "" so the else is running, which shows the entire table again.
I can't tell what you are setting the "value" attribute to in the fill_emp function. What is the value of $row[1] in that function?
Just change the ajax call like mentioned below :
$(document).ready(function(){
NomEmp = "";
$.ajax({
url:"usr.php",
method:"POST",
data:{NomEmp:NomEmp},
success:function(data){
$('#show_Abs').html(data);
}
});
$('#nom').change(function(){
NomEmp = $(this).val();
$.ajax({
url:"usr.php",
method:"POST",
data:{NomEmp:NomEmp},
success:function(data){
$('#show_Abs').html(data);
}
});
});
});
Also in your html cannot able to find a tag having id show_Abs. So just modify the div into
<div class="row" id="show_Abs">
</div>
Now I think the php function <?php echo fill_Abs($connect);?> is not necessary.
When page loads the first time, ajax call will be generated and on changing the select tag call will be generated with passing the selected parameters getting appropriate results.