Hi im making Crud app with php mysqli procedural language.i was able to retrieve and insert data but unable to delete. when i clicked on delete link it goes to delete.php page but nothing happens.
db.php
<?php
$conn = mysqli_connect('localhost', 'root','123','app');
?>
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CRUD</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</head>
<body>
<table class="table table-bordered ">
<tr>
<th>id</th>
<th>Email</th>
<th>Fisrtname</th>
<th>Lastname</th>
<th>Delete</th>
<th>Edit</th>
</tr>
<?php
include 'db.php';
$query ="select *from people";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><a href='delete.php?id=<?php echo $row['id']; ?>'>Delete</a></td>
<td><a href='edit.php?id=<?php echo $row['id']; ?>'>Edit</a></td>
</tr>
<?php
}
?>
<div class="container">
<i class="glyphicon glyphicon-plus"></i> Add Records
</div>
</body>
</html>
delete.php
<?php
include 'db.php';
if (isset($_GET('id'))) {
$delete_id = $_GET['id'];
$query = "delete from people where id = '$delete_id'";
$run = mysqli_query($conn,$query);
if ($run) {
header("Location:index.php");
}
}
?>
You have a typo in delete.php on line 4.
In GET array you have to use [] brackets around key name instead of ()
if (isset($_GET['id'])) {...}
Related
I had this project wherein, I am supposed to insert and select pictures, as in upload and view them. Initially, when I tried a code, it worked but the image didn't display. After many trials, I just copied a code from a mate which worked for him but when I tried, it didn't. Why? I have posted everything below-
Image upload code
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap");
$query = "SELECT * FROM `blobclob`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Image View Code
<html>
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap_exp");
$query = "SELECT * FROM `images`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Output
Database
it should be something like this
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';
it should be **src="data:image/jpeg;base64 ... ** instead of src="data:image..
I am trying to fetch data from an sql database into a php table. Yet the problem is that the first data from my database doesn't appear into the table. Did i commit a mistake ? The following is my code....
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
$data = mysqli_fetch_array($sql);
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data)
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>
You are doing mysqli_fetch_array() twice. Fixed code:
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>
I have been trying to display the data from a table onto a page. The table is called events and the database is school.
As far as I know, I'm doing everything right, I've stored the query in a string, run it using mysqli_query() and later put the echo statements in a while loop while placing $row=mysqli_fetch_assoc($result) inside the parenthesis. Yet when I run it not even a single entry is displayed on the page.
Here's my entire code:
<?php
require("includes/common.php");
$query = "SELECT name,place,date FROM school.events";
$result = mysqli_query($con, $query)or die(mysqli_error($con));
?>
<!DOCTYPE html>
<!--
Calendar Page for Trinity High School
-->
<html>
<head>
<title>Events Calendar</title>
<?php
require 'includes/external.php';
?>
</head>
<body>
<div class="content">
<?php
include 'includes/header.php';
?>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Event</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['place']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
PS: Using some trial and error I found out that the program simply doesn't enter the while loop. Perhaps there's something wrong with the condition?
I have made a script that is giving me the top 5 players according to most credits trough a desc 0,5 sql query
This is in a table, but it ain't really doing as i what it to go, what it does is every row it gives the table header, and its not making it as 1 table (see this screenshot)
this is my php and css:
<?php
include('connect.php');
$result = $db->prepare("SELECT * FROM `tbl_users` WHERE `credits` >'0' ORDER BY `credits` DESC LIMIT 0,5");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style2.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Great+Vibes' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="floated_elements">
<table>
<thead>
<tr>
<th> ID </th>
<th> Wie </th>
<th> Hoeveel </th>
<th> email </th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['userID']; ?></td>
<td><?php echo $row['userName']; ?></td>
<td><?php echo $row['credits']; ?></td>
<td><?php echo $row['userEmail']; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
</div> <!-- Closing floated elements -->
<script>
$('table tr').each(function(){
$(this).find('th').first().addClass('first');
$(this).find('th').last().addClass('last');
$(this).find('td').first().addClass('first');
$(this).find('td').last().addClass('last');
});
$('table tr').first().addClass('row-first');
$('table tr').last().addClass('row-last');
</script>
</body>
</html>
i found that for rule and tried to apply it but not really giving the correct results
Edited to give additional code cause asked for
You have started for loop and put everything into that. Its not a right way. Try as below :
<?php
include('connect.php');
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style2.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Great+Vibes'
rel='stylesheet' type='text/css'>
</head>
<body>
<div class="floated_elements">
<table>
<thead>
<tr>
<th>ID</th>
<th>Wie</th>
<th>Hoeveel</th>
<th>email</th>
</tr>
</thead>
<tbody>
<?php
$result = $db->prepare("SELECT * FROM `tbl_users` WHERE `credits` >'0' ORDER BY `credits` DESC LIMIT 0,5");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){ ?>
<tr>
<td><?php echo $row['userID']; ?></td>
<td><?php echo $row['userName']; ?></td>
<td><?php echo $row['credits']; ?></td>
<td><?php echo $row['userEmail']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<script>
$('table tr').each(function(){
$(this).find('th').first().addClass('first');
$(this).find('th').last().addClass('last');
$(this).find('td').first().addClass('first');
$(this).find('td').last().addClass('last');
});
$('table tr').first().addClass('row-first');
$('table tr').last().addClass('row-last');
</script>
</body>
</html>
I'm asking a user to fill out a form and I want to display that information in table format. Then, I want the user to be able to sort the table from the header row on each column. I am trying to use the jquery tablesorter plug in but cannot seem to get it to work. Does the plug in not work with PHP generated tables?
<!DOCTYPE HTML>
<html>
<head>
<title>Dashboard</title>
<link href ="table.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-latest.js"></script>
<script type="text/javascript" src="jquery.tablesorter.js"></script>
<script type = "text/javascript">
$(document).ready(function() {
$("#sortedtable").tablesorter();
});
</script>
</head>
<body>
<?php
unset($_SESSION['errors_record']);
define("WEB_DB", "server_db");
define("DB_USER", "root");
define("DB_PASS", "prog");
$db_host = "localhost";
MYSQL_CONNECT($db_host,DB_USER,DB_PASS);
mysql_select_db(WEB_DB);
?>
<p><h1>SRG TDE Technical Review Dashboard</h1></p>
<p>
<button>Create a New Review Record</button>
Logout
</p>
<div class="CSSTableGenerator">
<table id = "sortedtable" class = "tablesorter">
<thead>
<tr>
<th>Review Record ID</th>
<th>Project</th>
<th>Date</th>
<th>Author</th>
<th>Moderator</th>
<th>Portfolio Lead</th>
<th>Review Artifact Type</th>
<th>Review Artifact Name</th>
<th>Version</th>
</tr>
$sql = "select record_id,project,date,author,moderator,portlead,rtype,rname,version from dashboard_table ORDER BY date DESC";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
if ($num)
{
while(list($record_id,$project,$date,$author,$moderator,$portlead,$rtype,$rname,$version) = mysql_fetch_row($result))
{
?>
<?php $url="http://localhost/main_tab.php?record=" . $record_id ?>
<tbody>
<tr>
<td><?php echo "<a href = '$url'>$record_id</a>";?></td>
<td><?php echo $project?></td>
<td><?php echo $date?></td>
<td><?php echo $author?></td>
<td><?php echo $moderator?></td>
<td><?php echo $portlead?></td>
<td><?php echo $rtype?></td>
<td><?php echo $rname?></td>
<td><?php echo $version?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
I'm not that good with php, but it appears that the table being built will have every row wrapped in a new tbody. Move the initial <tbody> outside of the while loop:
if ($num)
{
echo "<tbody>";
while(list($record_id,$project,$date,$author,$moderator,$portlead,$rtype,$rname,$version) = mysql_fetch_row($result))
{
?>
<?php $url="http://localhost/main_tab.php?record=" . $record_id ?>
<tr>
<td><?php echo "<a href = '$url'>$record_id</a>";?></td>
<td><?php echo $project?></td>
<td><?php echo $date?></td>
<td><?php echo $author?></td>
<td><?php echo $moderator?></td>
<td><?php echo $portlead?></td>
<td><?php echo $rtype?></td>
<td><?php echo $rname?></td>
<td><?php echo $version?></td>
</tr>
<?php
}
}
?>
</tbody>