Deleting a Table Entry with PHP and SQL - php

So I have created a form that takes in entries and stores them in a SQL database called Warehouse_Maitenence (yes I know it is mispelled and I did that on purpose). The goal for this set of code is to connect to the database and display the table accordingly
<?php include("nav.php") ?>
<body style='background-color:#C0C0C0'>
<style>
.top{
text-align: Left;
}
.Headers_Inputs{
padding-top: 20px;
padding-bottom: 20px;
border-style: solid;
border-width: 1px;
}
input{
width: 100%;
}
.PRIMARY_TABLE{
padding-left: 20px;
padding-right: 20px;
width: 100%;
}
body{
padding-left: 20px;
padding-right: 20px;
padding-top: 20px;
padding-bottom: 60px;
}
.PRIMARY_TRAY{
border-style: solid;
border-width: 2px;
}
</style>
<body>
<table width="100%" border="1">
<tr>
<th style="text-align: center; font-size: 16px;"> ID </th>
<th style="text-align: center; font-size: 16px;"> MACHINE </th>
<th style="text-align: center; font-size: 16px;"> LABEL </th>
<th style="text-align: center; font-size: 16px;"> CONDITION </th>
<th style="text-align: center; font-size: 16px;"> ACTION </th>
<th style="text-align: center; font-size: 16px;"> ADDRESS </th>
<th style="text-align: center; font-size: 16px;"> DATE </th>
<th style="text-align: center; font-size: 16px;"> COMMENT </th>
<th style="text-algin: center; font-size: 16px;"> DELETE </th>
</tr>
<?php
$username="";
$password="";
$database="";
$servername = "";
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'SELECT ID, MACHINE, LABEL, COND, ACTION, ADDRESS, DATE, COMMENT FROM Warehouse_Maitenence ORDER BY `Warehouse_Maitenence`.`MACHINE` ASC, `Warehouse_Maitenence`.`LABEL` ASC, `Warehouse_Maitenence`.`ID` ASC';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td style='text-align: center; font-size: 16px;' id=".$row["ID"].">".$row["ID"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["MACHINE"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["LABEL"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["COND"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["ACTION"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["ADDRESS"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["DATE"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["COMMENT"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'><input type='button' onclick=delete_entry(".$row["ID"].")</td>";
echo "</tr>";
}
} else { echo "0 results"; }
echo "</table>";
mysqli_close($conn);
?>
So This level of code does work as intended up to the deletion point. I want to be able to load buttons on the end of each row of the table and onclick delete that row from the table and SQL Database.
For the table I have a JS already which works in another set of code I have been working on.
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("dataTable").deleteRow(i);
}
I know that onclick I will need to reconnect to the database (no issues there since it is already in this code) and then run
DELETE FROM `Warehouse_Maitenence` WHERE `Warehouse_Maitenence`.`ID` = whatever row was selected
but I am not sure how to put it all together, since I am not doing a form submission.

Am only good with php and i will advice you to do this..
add this to the list of the table
<td>
<form action="delete.php" method="post" enctype="multipart/form-data">
<input type="text" name="id" value="<?php echo $id; ?>" style="display:none;" />
<input type="submit" name="delete" value="Delete" class="btn btn-danger" />
</form>
</td>
then create a page call delete.php
<?php
if(!isset($_SERVER['HTTP_REFERER'])){ //This is to stop direct access to this delete.php page
header('location: /404.php'); //where to be redirected to
exit;
}
session_start();
include("connect.php"); //your database connection declaration page
if(isset($_POST['delete'])){ //name from the form from table
$id = mysqli_real_escape_string($con, $_POST['id']); //id of the row
$variable = $con->query("DELETE FROM tablename WHERE id = '$id'") or die(mysqli_error($con)); //connecting to the db to the table to delete
if($variable == TRUE){
echo"<script>alert('Deletion Completed!');</script>";
echo"<meta http-equiv='refresh' content='0 url=page.php' />"; //where to be redirected to after deleting.
}else{
echo"<script>alert('Error');</script>";
print_r("Error, Lets go back and Try again");
echo"<meta http-equiv='refresh' content='0 url=page.php' />";
}
exit();
}
?>
I hope am able to solve your issues.!

Related

CSS Changing The Look of A Table

I have a table on my website which contains the columns: User, Title, Description, Join, Update, Delete.
The "User" column's width is way too big as well as the "Title" column. I need help with CSS to set them to something smaller without affecting the width of the other columns as they are perfect as is.
My Code:
<?php
echo "<table>
<tr>
<th>User</th>
<th>Title</th>
<th>Description</th>
<th></th>
<th>Join</th>
<th>Update</th>
<th>Delete</th>
</tr>";
while ($record = mysql_fetch_array($myData))
{
echo "<form action=findGroup.php method=post>";
echo "<div class=joinLink>";
echo "<tr>";
echo "<td>" . "<input type=text name=name value='" . $record['form_user'] . "'/> </td>";
echo "<td>" . "<input type=text name=name value='" . $record['form_name'] . "'/> </td>";
echo "<td>" . "<input type=text name=description value='" . $record['form_description'] . "'/> </td>";
echo "<td>" . "<input type=hidden name=hidden value='" . $record['form_id'] . "'/></td>";
echo "<td><a class=joinLink type=text name=join href='http://localhost:3000'>Join</a></td>";
echo "<td>" . "<input type=submit name=update value='Update" . "'/> </td>";
echo "<td>" . "<input type=submit name=delete value='Delete" . "'/> </td>";
echo "</tr>";
echo "</div>";
echo "</form>";
}
echo "</table>";
CSS:
table {
width: 100%;
font: 17px/1.5 Arial, Helvetica,sans-serif;
text-align: centre;
align: centre;
}
input {
width: 100%;
font: 13px/1.5 Arial, Helvetica,sans-serif;
text-align: centre;
align: centre;
height: auto;
overflow: hidden;
}
th {
align: centre;
text-align: centre;
background-color: #4D5960;
color: white;
}
tr {
background-color: #f2f2f2
}
input[type="text"]{
width:100% !important;
line-height:30px;
box-sizing:border-box;
}
a[type="text"]{
width:100% !important;
line-height:30px;
box-sizing:border-box;
}
You can use width property to fix the width of your columns. You can apply width in html directly or using css
HTML
<td width="20%">content</td>
<th width="20%">content</th>
CSS
.custom-class{
width: 20%;
}
<th class="custom-class"></th>
<td class="custom-class"></td>
If you don't mind I would suggest some changes, see below. Check if the form action is either join, update or delete to perform the action. About the data structure you receive when the form is submitted check out the var_dump($_POST); part
PHP:
<?php
var_dump($_POST);
?>
CSS:
table {
width: 100%;
font: 17px/1.5 Arial, Helvetica,sans-serif;
}
input[type="text"] {
width: 100%;
font: 13px/1.5 Arial, Helvetica,sans-serif;
text-align: center;
box-sizing: border-box;
padding: 6px 13px;
}
button {
width: 100%;
font: 13px/1.5 Arial, Helvetica,sans-serif;
text-align: center;
box-sizing: border-box;
padding: 6px 13px;
}
thead th {
align: center;
text-align: center;
background-color: #4D5960;
color: #ffffff;
}
tbody td {
align: center;
text-align: center;
background-color: #f2f2f2;
}
HTML/PHP:
<form action="index.php" method="POST" enctype="application/x-www-form-urlencoded">
<table>
<thead>
<tr>
<th class="col-name">User</th>
<th class="col-title">Title</th>
<th class="col-description">Description</th>
<th class="col-join">Join</th>
<th class="col-update">Update</th>
<th class="col-delete">Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ([0, 1, 2, 3] as $row) : ?>
<tr>
<td class="col-name">
<input type="hidden" name="user[id][]" value="">
<input type="text" name="user[name][]" value="" size="10">
</td>
<td class="col-title">
<input type="text" name="user[title][]" value="" size="10">
</td>
<td class="col-description">
<input type="text" name="user[description][]" value="" size="10">
</td>
<td class="col-join">
<button type="submit" name="action" value="join">Join</button>
</td>
<td class="col-update">
<button type="submit" name="action" value="update">Update</button>
</td>
<td class="col-delete">
<button type="submit" name="action" value="delete">Delete</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
it seems that there is no problem, everything is the same
table {
width: 100%;
font: 17px/1.5 Arial, Helvetica,sans-serif;
text-align: center;
align: center;
}
input {
width: 100%;
font: 13px/1.5 Arial, Helvetica,sans-serif;
text-align: center;
align: center;
height: auto;
overflow: hidden;
}
th {
align:center;
text-align: center;
background-color: #4D5960;
color: white;
}
tr {background-color: #f2f2f2}
input[type="text"]{width:100% !important; line-height:30px; box-sizing:border-box;}
a[type="text"]{width:100% !important; line-height:30px; box-sizing:border-box;}
https://jsfiddle.net/1qy5v05p/6/
you can also use Bootstrap 4, maybe it will be better https://www.w3schools.com/bootstrap4/bootstrap_forms.asp
There are bunch of jQuery table plugins. Check this out.
My favourites are DataTables, x-Editable and Bootgrid. There are also open sources, you can see them with their Github repo.

getting my db table into html table gets no results

this is my first post and im already totally confused about my project right now :x
im able to make a successful connection to the sql db and also receive my data which displays on the top left, BUT its supposed to display in a html table, not somewhere on the screen :O
Here is the website to see the code in action, though there is none yet!
http://blackskill.square7.ch/
Here is the code which is running fine, but maybe im just too stupid and new for coding yet xd
<!DOCTYPE html>
<?php
$connection=mysqli_connect("localhost","username","password","db_name");
if ($connection) {
echo "database online <br>";
} else {
die("Connection failed. Reason: ".mysqli_connection_error());
}
$sql="SELECT * FROM blackskill_playerdb";
$results=mysqli_query($connection,$sql);
if (mysqli_num_rows($results)>0) {
while($row=mysqli_fetch_array($results)) {
echo '<tr>
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>
<td>'.$row[3].'</td>
</tr>';
echo "<br>";
}
}
mysqli_close($connection);
?>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 20%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
}
#myTable th, #myTable td {
text-align: left;
padding: 12px;
}
#myTable tr {
border-bottom: 1px solid #ddd;
}
#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
</style>
</head>
<body>
<h2><center>Inofficial S.K.I.L.L. - Special Force 2 - Dishonor List</center></h2>
<input type="text" id="myInput" onkeyup="aa" placeholder="Search for player..." title="Type in a name">
<table id="myTable">
<tr class="header">
<th style="width:40%;">Player</th>
<th style="width:20%;">Clan</th>
<th style="width:20%;">Evidence</th>
<th style="width:20%;">Added to list</th>
<?php while($row=mysqli_fetch_array($results)) : ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['clan']; ?></td>
<td><?php echo $row['rulebreak']; ?></td>
<td><?php echo $row['addlist']; ?></td>
</tr>
<?php endwhile ?>
</tr>
</table>
<tbody>
</body>
</html>
i hope we can find an answer together with friendship, emotions and friendship!
regards

Passing php variables

I am creating a user interface for managing a database. Each entry has two radio buttons, one for validation (and subsequent emailing to concerned authorities) and one for deleting the entry. The code is given below, and the bold parts are the ones which concern this functionality.
A couple of questions:
1) Is it possible to do so without the form attribute? If so, how would I do it?
2) With the form attribute, I use 'handle.php' to delete each entry. The syntax in the 'handle.php' includes this line
$sql="DELETE FROM rti WHERE ID=x"; //x here is the ID number to delete.
Now, how do I pass the value of the ID as well from my interface, so that the above line of code deletes the entry corresponding to the button that was pressed?
<html>
<head>
<style>
* {
margin: 0px;
padding: 0px;
}
div.topbar {
position: relative;
background-color: black;
height: 45px;
text-align: center;
font-family: Calibri;
font-size: 30px;
padding-top: 10px;
}
div.topbar img {
position: absolute;
left: 10px;
top: 0px;
}
div.topbar span {
color: white;
}
div.container {
background-color: #a5a5a5;
width: 100%;
height: 100%;
color: white;
}
div.data {
padding: 20px;
}
table {
background-color: #b1b1b1;
border-collapse: collapse;
}
th, td {
text-align: center;
border: solid 1px black;
margin: 0px;
}
th.one {
width: 50px;
}
th.two{
width: 150px;
}
th.three {
width: 75px;
}
th.four {
width: 200px;
}
div.validation {
background-color: #b5b5b5;
width: 200px;
height: 100%;
position: relative;
left: 1100px;
bottom: 300%;
text-align: center;
border-left: solid 1px black;
}
</style>
</head>
<body>
<div class = "topbar"><img src="logo.png"><span>RTI DATABASE</span></div>
<div class = "container">
<div class ="data">
<?php
$servername="localhost";
$username="root";
$password='';
$conn=mysql_connect($servername, $username, $password) or die ("Error connecting to mysql server: ".mysql_error());
$dbname = 'bsp';
mysql_select_db($dbname, $conn) or die ("Error selecting specified database on mysql server: ".mysql_error());
$sql="SELECT * FROM rti";
$result=mysql_query($sql) or die ("Query to get data from firsttable failed: ".mysql_error());
echo "<table>";
echo "<tr>";
echo '<th class="one">ID</th>
<th class="two">Name</th>
<th class="three">Board</th>
<th class="four">Query</th>
<th class="five">Validate</th>
<th class="six">Delete</th>
<th class="seven">Submit</th>';
echo "</tr>";
while ($row=mysql_fetch_array($result)) {
$id=$row["ID"];
$name=$row["name"];
$board=$row["board"];
$query=$row["query"];
echo "<tr>";
echo "<td>$id</td>
<td>$name</td>
<td>$board</td>
<td>$query</td>
**<form action='handle.php' method='POST'>
<td><input type='radio' name='option' value='validate'></td>
<td><input type='radio' name='option' value='delete'></td>
<td><input type='submit' value='Submit' name='submit'>
</form></td>";
echo "</tr>";**
}
echo "</table><br>";
?>
</div>
</div>
</body>
</html>
**<form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td>
<td><input type='radio' name='option' value='delete'>
<input type='hidden' name="id" value='".$id."'>
</td>
<td><input type='submit' value='Submit' name='submit'></form></td>";
In handle.php:
$id = $_POST['id'];
inside your form add an hidden type input with the id value for each item , so each time user click the button the id sent. just like this :
<form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td>
<input type = 'hidden' name="id" value='".$row['id']."'><td>
<input type='radio' name='option' value='delete'></td>
<td><input type='submit' value='Submit' name='submit'></form></td>";
use something like <input type='radio' name='option[your_id]' value='validate'> to identify the item.
Like: <input type='radio' name='option[$id]' value='validate'>

Different sized rows in tables populated by php

So I made this div that is populated by php calls to my server to populate it with a table per entry in the Server table. Aka, a php script connects to my server, checks how many entries there are, and creates a table for each one to populate the div. So in the end there are several table entries displayed corresponding to each entry in the database.
I don't know of a jsfiddle/codepin type place that allows php since it's server-side so I took a picture to show what it looks like. The arrows are pointing to the two entries where the box they are in is wider than the others. This happens any time I do not include a link (an optional entry in the database). I assume this is because it is trying to fill the space left by having no link in the last table row, but the last part of my php script should have removed that last row if it was empty (assuming I wrote it correctly) and it shouldn't need to stretch to fill something no longer there.
So, what I'm asking here is how do I make each of the orange rows the same size?
Picture of the box: http://i67.tinypic.com/5k3nzs.png
HTML (Relevant portion)
<div id="announcements_box">
<h2>Announcements</h2>
<?php include "scripts/php/getAnnouncement.php";?>
<div class="bottom_color"></div>
</div> <!--end announcement_box-->
CSS (Relevant portion)
#announcements_box{
table-layout: fixed;
display: inline-block;
background-color: #FFFFFF;
width: 47%;
color: black;
margin-left: 1rem;
border-top-left-radius: 30px;
border-top-right-radius: 30px;
border-bottom-left-radius: 30px;
border-bottom-right-radius: 30px;
}
#announcements_box h2{
font-weight: normal;
font-size: 2rem;
background-color: navy;
color: white;
border-top-left-radius: 25px;
border-top-right-radius: 25px;
}
.announcement_blocks{
width: 100%;
margin-bottom: .5rem; /* space after each block */
background-color: #C0C0C0;
}
.announcement_blocks img{
/* height: 75px; */
width: 75px;
margin: .5rem;
padding-top: 1rem;
}
.announcement_blocks td{
/* word-wrap: break-word;
word-break: break-all;
*/
}
.announcement_blocks td.title{
text-align: center;
background-color: #E18A07;
font-weight: bold;
}
.announcement_blocks td.descr{
padding-left: 1rem;
padding-right: 1rem;
}
.announcement_blocks td.link{
text-align: center;
/*
padding-left: 1rem;
padding-right: 1rem;
*/
}
.announcement_blocks td.img_s{
width: 1rem;
background-color: #330000;
}
.bottom_color{
height: 2.5rem;
background-color: navy;
border-bottom-left-radius: 25px;
border-bottom-right-radius: 25px;
margin-top: -.5rem;
}
PHP (Relevant portion)
<?php require "scripts/php/database_connect.php"?>
<?php
$query = mysqli_query($con, "SELECT * FROM Announcements") or die("Error in query. Details: ".mysqli_error());
while($row = mysqli_fetch_assoc($query))
{
?>
<table class="announcement_blocks">
<tr>
<td class="img_s" rowspan="3"><?php echo "<img src='/img/announcement_icon/announcement_imp.png" . "' alt='announcement'>";?></td>
<td class="title"><?php echo $row['title']; ?></td>
</tr>
<tr>
<td class="descr"><?php echo $row['description']; ?></td>
</tr>
<?php if($row['link'] != "")
{
?>
<td class="link"><?php echo $row['link']; ?></td>
<?php
}
?>
</table>
<?php
}
?>
I know this is long and probably a bit confusing since I can't just hand you a fiddle or codepin to toy with but any ideas?
Thanks
In bottom if links are there its working, if not there its height increased.
to avoid this you can give
<?php if($row['link'] != ""){?>
<?php echo $row['link']; ?>
<?}else { echo "<br>";}?>
Try to use this php code:
<?php
$query = mysqli_query($con, "SELECT * FROM Announcements") or die("Error in query. Details: ".mysqli_error());
while($row = mysqli_fetch_assoc($query)){?>
<table class="announcement_blocks">
<tr>
<td class="img_s" rowspan="3"><?php echo "<img src='/img/announcement_icon/announcement_imp.png" . "' alt='announcement'>";?></td>
<td class="title"><?php echo $row['title']; ?></td>
</tr>
<tr>
<td class="descr"><?php echo $row['description']; ?></td>
</tr>
<tr>
<td class="link">
<?php if($row['link'] != ""){?>
<?php echo $row['link']; ?>
<?}?>
</td>
</tr>
</table>
<?}?>

I am trying to display information in a table format from sql database what am I doing wrong?

Ok I can display data on to the page from the database but I am having a problem displaying it nicely in a table format so its under headers and gos down the page. This is what has happened at the moment:
Here is my page code:
<link rel="stylesheet" href="css/productiontable.css">
<?php
error_reporting(0);
require './db/connect.php';
include './includes/header.php';
?>
<h2>Productions</h2>
<div class="productiontable">
<table>
<tr>
<th>Production Name</th>
<th>Production Description</th>
<th>Production Type</th>
</tr>
<tr>
<td class="productname">
<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
echo $row->ProductionName;
?>
</td>
<td class="productinfo">
<?php
echo $row->ProductionInformation;
?>
</td>
<td class="producttype">
<?php
echo $row->ProductionType;
}
?>
</td><bR>
</tr>
</table>
<?php
$result->free();
}
}
mysqli_close($connection);
include './includes/footer.php';
Here is the css for the table:
#import url(http://fonts.googleapis.com/css?family=Dosis);
h2{
text-align: left;
color: white;
font-family: 'Dosis';
margin: 10px;
font-size: 32px;
}
h3{
font-size: 18px;
color: white;
margin: 20px;
}
.productiontable{
width: 900px;
border: 1px black solid;
margin: 10px;
}
.productname{
width: 200px;
float: left;
font-weight: bold;
margin-left: 10px;
}
.productinfo{
width: 500px;
float: left;
margin-left: 10px;
}
.producttype{
width: 200px;
float: left;
font-style: oblique;
margin-left: 10px;
}
Please help as much as possible many thanks :)
There are a couple of things wrong:
1. The br tag is not needed.
2. The loop should be outside of the tr. This way a new row will be made for every row.

Categories