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);
}
}
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)
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 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
I'm having hard time to figure it out how to insert the checked box-es or unchecked into the database with php.
I tried many many different ways but none is working, I think I'm very close but can't figure it out the problem.
Btw I work with Javascript and never worked with PHP except this time.
index.html
<html>
<head>
<style type="text/css">
#import "demo_page.css";
#import "header.ccss";
#import "demo_table.css";
#import "select.dataTables.min.css";
#import "jquery.dataTables.min.css";
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="RowGroupingWithFixedColumn.js"></script>
<script>$(document).ready(function(){load_(); console.log('load running')});</script>
</head>
<body id="dt_example">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="endpoints">
<thead>
<tr>
<th></th>
<th>Nr.</th>
<th>Java Class Name</th>
<th>http Method</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,DB_NAME);
$sql='SELECT * FROM url';
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
print $row['method'];
switch ($row['http_method']) {
case "GET":
echo "<tr class='gradeA'>";
break;
case "PUT":
echo "<tr class='gradeC'>";
break;
case "POST":
echo "<tr class='gradeU'>";
break;
case "DELETE":
echo "<tr class='gradeX'>";
break;
default:
echo "<tr>";
}
if($row['checked']){
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "' checked> </td>";
} else {
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "'> </td>";
}
echo "<td align=center >" . $row['number'] . "</td>";
echo "<td align=center >" . $row['class_name'] . "</td>";
echo "<td>" . $row['http_method'] . "</td>";
echo "<td style='font-weight:bold'>" . $row['endpoint'] . "</td>";
echo "</tr>";
}
if(isset($_POST['save'])){
echo "<script>console.log(" . $checkboxes.length . ");</script>";
$rows = $_POST['case'];
foreach($rows as $row){
$sql = "UPDATE url SET checked = 1 WHERE number = " . $case;
$result = mysqli_query($con,$sql);
}
}
mysqli_close($con);
echo "</tbody></table>";
echo "<input type='submit' name='save' id='save' value='Save' />";
?>
</body>
</html>
Any help would be appreciated.
image:
EDIT:
Solved - here is the code:
<?php
if(isset($_POST['save'])){
echo "<script>console.log(" . $checkboxes.length . ");</script>";
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,DB_NAME);
$rows = $_POST['case'];
foreach($rows as $row){
$sql = "UPDATE url SET checked = 1 WHERE number = " . $row;
$result = mysqli_query($con,$sql);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#import "demo_page.css";
#import "header.ccss";
#import "demo_table.css";
#import "select.dataTables.min.css";
#import "jquery.dataTables.min.css";
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="RowGroupingWithFixedColumn.js"></script>
<script>$(document).ready(function(){load_(); console.log('load running')});</script>
</head>
<body id="dt_example">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="endpoints">
<thead>
<tr>
<th></th>
<th>Nr.</th>
<th>Java Class Name</th>
<th>http Method</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect('sql7.freemysqlhosting.net','sql7117068','GZqaZj69G9','sql7117068');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,'sql7117068');
$sql='SELECT * FROM url';
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
print $row['method'];
switch ($row['http_method']) {
case "GET":
echo "<tr class='gradeA'>";
break;
case "PUT":
echo "<tr class='gradeC'>";
break;
case "POST":
echo "<tr class='gradeU'>";
break;
case "DELETE":
echo "<tr class='gradeX'>";
break;
default:
echo "<tr>";
}
if($row['checked']){
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "' checked> </td>";
} else {
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "'> </td>";
}
echo "<td align=center >" . $row['number'] . "</td>";
echo "<td align=center >" . $row['class_name'] . "</td>";
echo "<td>" . $row['http_method'] . "</td>";
echo "<td style='font-weight:bold'>" . $row['endpoint'] . "</td>";
echo "</tr>";
}
mysqli_close($con);
echo "</tbody></table>";
echo "<input type='submit' name='save' id='save' value='Save' />";
echo "</form>";
?>
</body>
</html>
PS: There may be many leaks and bad programming style in the code, but know that I'm not a PHP developer and it doesn't matter if there can be different attacks on my server. I only wanted to solve the problem and move on. Don't have time to stop at every point.
Use code below to check if a check box is checked or not. Then set some flags on some variables to insert data to to db accordingly.
//html
<input type='checkbox' name='boxname' value='1' />
//php
<?php
if(isset($_POST['boxname'])
{
echo "check box is checked";
}
?>
Order.php:
<?php
require_once('conn.php');
session_start();
$itemId=$_SESSION['itemId'];
$tnumber=$_SESSION['tnumber'];
$custno=$_SESSION['custno'];
$sql="select itemId,subtitle,price,quantity from cart where tnumber='$tnumber'" ;
$sql2="select subtitle from cart where itemId='$itemId'";
$res2=mysqli_query($dbhandle,$sql2);
$row1= mysqli_num_rows($res2);
$res=mysqli_query($dbhandle,$sql);
$total=0;
?>
<html>
<head>
<title>Home</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type='text/css' href='cartbox.css'/>
</head>
<body>
<h1></h1>
<script src="home.js"></script>
<div id="shopping-cart"> </div>
<?php
echo "<table id='t1' border='1'>
<th>Subtitle</th>
<th>Quantity</th>
<th>Price</th>
<th>Amount</th>
</tr>";
if (row1 > 0) {
while ($row=mysqli_fetch_array($res)) {
$amount=$row['price']*$row['quantity'];
echo "<form id='addform{$row['itemId']}' method='post' action='cartdelete.php'> ";
echo "<tr>";
echo "<td>" .$row['subtitle'] ."</td>";
echo "<td>" .$row['quantity'] ."</td>";
echo "<td>" .$row['price'] ."</td>";
echo "<td>" . $amount . "</td>";
echo "<td><input type='submit' value='x' name='submit'></td>";
echo "<td><input type='text' name='itemId' value= '{$row['itemId']}'></td>";
echo"</form>";
echo "</tr>";
$total = $total+ $amount;
}
}
echo "</table>";
?>
<?php echo $total ?>
<input type="button" name="continue" value="Continue Order" style="position: absolute;top:200px;" onclick="location.href='customerdicecream.php'">
</body>
</html>
I'm trying to display the data in an HTML table (with primary key). I need the subtitle to be entered into the HTML table, only once, but with the if condition I'm not able to achieve it. How can I do that?
You may use DISTINCT / GROUP BY in your query for prevention of the redundant value.
ex.
$sql="select itemId,subtitle,price,quantity from cart where tnumber='$tnumber'" ;
$sql2="select DISTINCT subtitle from cart where itemId='$itemId'";
Reference Site
Define a variable $prevSubTitle before the while ($row=mysqli_fetch_array($res)) { condition.
And when you read value for it from $row['subtitle'], check if it matches with previously read value. If 'false' display new value, else fill td with a space as <td> </td>
#prevSubTitle = '';
while ($row=mysqli_fetch_array($res)) {
$amount=$row['price']*$row['quantity'];
echo "<form id='addform{$row['itemId']}' method='post' action='cartdelete.php'> ";
echo "<tr>";
$subtitle = $row['subtitle'];
if( ! ( $subtitle == $prevSubTitle ) ) {
$prevSubTitle = $subtitle;
}
else {
$subtitle = ' ';
}
echo "<td>" . $subtitle ."</td>"; // empty when matched with previous row title
echo "<td>" .$row['quantity'] ."</td>";
echo "<td>" .$row['price'] ."</td>";