How to request user ID or delete a row in PHP - php

I need to delete a specific row in php.. so how could I get the ID or other way to delete a specific record
dbconnect.php just a simple database connection
<?php
$con = mysqli_connect("localhost","root","","phpractice");
if(mysqli_connect_errno()){
echo "Database connection failed";
}
?>
index.php the page where the user can see
<html>
<head>
<link rel="stylesheet" type="text/css" href="../styles/index.css">
</head>
<title>Home Page</title>
<?php include'../script/dbconnect.php';?>
<body>
<div id="container">
<div id="table">
<?php
$result = mysqli_query($con,"SELECT * FROM users");
echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Password</th>
<th colspan=2>Controls</th>
</tr>
";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>"."<a href='#'>EDIT</a>"."</td>";
echo "<td><a href='../script/delete.php?id=".$row['user_id']."'>DELETE</a></td>";
echo "</tr>";
}
echo "</table>";
?>
Add User
</div><!--table-->
</div><!--container-->
</body>
</html>
delete.php the delete script
<?php
include '../script/dbconnect.php';
$id = $_GET['user_id'];
$query = "DELETE FROM users WHERE user_id = $id";
mysqli_query($con, $query) or die (mysqli_error($con));
echo "DELETE USER SUCCESSFUL!";
echo "</br>";
echo "<a href='../main/index.php'>RETURN TO DISPLAY</a>";
?>
thanks in advance

in index.php use:
echo "<td><a href='../script/delete.php?user_id=".$row['user_id']."'>DELETE</a></td>";
then use $_GET['user_id']; in delete.php

Related

Need Assistance with following ```HTML``` Error?

I am getting the following validation error, it claims I have "misplaced non-space characters inside the table" I have tried everything and for the life of me cannot fix this error. The error is the following
Here is the PHP/HTML being used for
<?php
$title = "Alter Records";
include('includes/head.php');
include('includes/nav.php');
ini_set('display_errors', 0);
require_once('config.php');
require_once('db_class.php');
$connection = new dbController(HOST,USER,PASS,DB);
$sql = "select id,name, image,location from location";
$results = $connection->getAllRecords($sql);
//var_dump($results);
?>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Location</th>
<th>Image</th>
<th colspan="2">Make Changes</th>
</tr>;
<?php
foreach ($results as $row){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['location']}</td>";
echo "<td><img class='thumb' src='{$row['image']}' alt='{$row['name']}'> </td>";
echo "<td><a href=''>Update</a></td>";
echo "<td><a href='delete_record.php?id={$row['id']}'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
include('includes/footer.php')
?>
Here is the code inside validator.w3.org
↩
<!DOCTYPE html>↩
<html lang="en">↩
<head>↩
<meta charset="utf-8">↩
<title>Alter Records</title>↩
<link href="https://fonts.googleapis.com/css2?family=Quicksand&display=swap" rel="stylesheet">↩
<link href="https://fonts.googleapis.com/css2?family=Bellota+Text:ital,wght#1,300;1,400&display=swap" rel="stylesheet">↩
<link rel="stylesheet" href="css/style.css">↩
</head>↩
<body>↩
<header><h1> TOURISM VICTORIA</h1></header>↩
<nav>↩
<ul class="flex-nav">↩
<li>Home</li>↩
<li>Add</li>↩
<li>Alter</li>↩
<li>↩
<form class="search-form" action="search.php" method="get">↩
<input type="text" name="search">↩
<button type="submit">Search</button>↩
</form>↩
</li>↩
</ul>↩
</nav>↩
<table>↩
<tr>↩
<th>Id</th>↩
<th>Name</th>↩
<th>Location</th>↩
<th>Image</th>↩
<th colspan="2">Make Changes</th>↩
</tr>;↩
<tr><td>1</td><td>Melbourne ShowGrounds</td><td>Royal Melbourne Showgrounds. Ascot Vale, Victoria, Austra
Any assistance with this would be greatly appreciated. If I am still unclear, please ask for further clarification.
just seeing your last include will also result in an error, you will need to end with ;

How to give a hyper link for user name [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In my admin page, admin can view the user id, user name and email id. Now, the admin can click user name, then it displays the details of that particular user name. I don't know how to do. Please help. My code is given below.
<html>
<head>
<title>Admin Page</title>
</head>
<body>
Welcome
<?php
include ('connect.php');
session_start();
$admin = $_SESSION['adminname'];
echo "$admin";?>
<b> SIGN OUT</b>
<table border="2" align="center">
<thead>
<tr>
<th>User_id</th>
<th>User Name</th>
<th>Email ID</th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($con, "SELECT * FROM sample");
while($row = mysqli_fetch_array($query))
{
$id=$row[0];
$name=$row[1];
$mail=$row[2];
echo
"<tr>
<td>{$row[0]}</td>
<td>{$row[1]}</td>
<td>{$row[2]}</td>";
}?>
</tbody>
</table>
</body>
</html>
First you query your database to get all username,ID and email.
Then you do a loop to display it.
Key is in your loop you can do like this.
<?php
$query = mysqli_query($con, "SELECT * FROM sample");
while($row = mysqli_fetch_array($query)){
$id=$row[0];
$name=$row[1];
$mail=$row[2];
echo "<tr>";
echo "<td>";
echo "<a href='?userid=$id'>$id</a>";
echo "</td>"
echo "<td>";
echo "<a href='?userid=$id'>$name</a>";
echo "</td>"
echo "<td>";
echo "<a href='?userid=$id'>$email</a>";
echo "</td>"
echo "</tr>";
}
?>
This is result from loop above.
<tr>
<td>
<a href='detail.php?userid=1'>Fullname1 : Email1#email.com</a>
<a href='detail.php?userid=2'>Fullname2 : Email2#email.com</a>
...
</td>
</tr>
Now you will see this in your URL ?userid=1
You can do this on top of detail.php file $user_id = $_GET['userid'];
Then you get your user_id // 1 2 3 4 5 ... whatever
Now you do a query again with $user_id
like this :
$query = "SELECT * FROM sample WHERE user_id = $user_id";
while($row = mysqli_fetch_array($query)){
//do what you want with this user data
}
I here modified your code, Please try this,
<html>
<head>
<title>Admin Page</title>
</head>
<body>
Welcome
<?php
include ('connect.php');
session_start();
$admin = $_SESSION['adminname'];
echo "$admin";
?>
<b> SIGN OUT</b>
<table border="2" align="center">
<thead>
<tr>
<th>User_id</th>
<th>User Name</th>
<th>Email ID</th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($con, "SELECT * FROM sample");
while($row = mysqli_fetch_array($query))
{
$id=$row[0];
$name=$row[1];
$mail=$row[2];
echo '<tr>';
echo '<td>{$row[0]}</td>';
echo '<td>{$row[1]}</td>';
echo '<td>{$row[2]}</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</body>
</html>
Hope it will solve your issue.

PHP dropdown database code not working?

I have an issue with the below code, it is showing the html part but not the data from my database. I am just trying to have the data from the database in the drop down list and when I click find to display it on the table below. Can you please help?
<?php
include 'config.php';
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<select name="manufacturer">
<option value="">----ALL----</option>
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($result))
{
echo "<option value='$r[id]'> $r[manufacturer]</option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
<br><br>
<table border="1">
<tr align="center">
<th>ID</th> <th>Manufacturer</th>
</tr>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$des=$_POST["manufacturer"];
if($des=="")
{
$result= mysqli_query("SELECT id, manufacturer FROM coop");
}
else
{
$result= mysqli_query($sql);
}
echo "<tr><td colspan='5'></td></tr>";
while($r= mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td align='center'>$r[0]</td>";
echo "<td width='200'>$r[1]" . " $r[2]</td>";
echo "<td alig='center' width='40'> $r[3]</td>";
echo "<td align='center' width='200'>$r[4]</td>";
echo "<td width='100' align='center'>$r[5]</td>";
echo "</tr>";
}
}
?>
</table>
</body>
</html>
your queries are wrong here
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($sql))
{
echo "<option value='$r[0]'> $r[0]</option>";
}
?>
this while ($r = mysqli_fetch_assoc($sql)) should be while ($r = mysqli_fetch_assoc($result ))
you should fetch your assoc with $result not with$sql
still you have some error bellow part

add/remove data from table from previous page

I have an HTML table being displayed from php data and I'm trying to move data from one table to another based on what is clicked. I cannot get it to move from table to another but I"m not getting any code error.
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests
</center>
</div>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
// get results from database
$result = mysql_query("SELECT * FROM temp")
or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['User_id'] . '</td>';
echo '<td>' . $row['First_name'] . '</td>';
echo '<td>' . $row['Last_name'] . '</td>';
echo '<td>' . $row['Username'] . '</td>';
echo '<td>Approve</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
</body>
</html>
PHP EDIT SCRIPT:
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>
I needed to use
$id = $_get['id];
and in the insert statement i had to replace id with User_id
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>

Delete operation in php using prototype js

I am using php / mysql and protype.js to delete record from a table. problem is that after deleting record from grid, grid is not updating automatically in template.
This is my code:
Step 1: index.php
<?php
require("Connection.php");
$sql = "SELECT * from tbluser";
$result=mysql_query($sql);
?>
<html>
<head>
<script src="prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js/src/scriptaculous.js" type="text/javascript"></script>
<script language="javascript">
function fnDeleteRecord(id)
{
new Ajax.Request('delete.php?action=Delete&id='+id,{method:'get'});
}
</script>
</head>
<body>
<table id="userrecords" border="1" cellpadding="0" cellspacing="0" width="700">
<tr>
<th> User Name </th>
<th> Password </th>
<th> Address </th>
<th> Phone Number </th>
<th> Action </th>
<th> Status </th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr id='".$row[id]."'>";
echo "<td>".$row['user_name']."</td>";
echo "<td>".$row['pass']."</td>";
echo "<td>".$row['address']."</td>";
echo "<td>".$row['phone_no']."</td>";
echo "<td><img src='images/spinner.gif' id='a[$row[id]]' alt=icon border=0 /><a href='javascript:void(0);' onclick=fnDeleteRecord('".$row['id']."'); >Delete</a></td>";
if($row['status'] == 'enabled')
echo "<td><img src='images/spinner.gif' id='a[$row[id]]' alt=icon border=0 style='display:none;' /><img src='images/enable.png' alt=icon border=0 onClick=fnUpdateStatus('".$row['id']."','disabled') /></td>";
else if ($row['status'] == 'disabled')
echo "<td><img src='images/spinner.gif' id='a[$row[id]]' alt=icon border=0 style='display:none;' /><img src='images/disable.png' alt=icon border=0 onClick=fnUpdateStatus('".$row['id']."','enabled') /></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
Step 2: delete.php
<?php
require_once('connection.php');
if ($_GET['action'])
{
switch($_GET['action'])
{
case 'Delete':
$sql = "Delete from tbluser where id ='".$_GET['id']."'";
$result = mysql_query($sql);
if(!$result)
echo "some problem occured during delete operation";
break;
}
}
?>
You can remove that table row with prototype:
<script language="javascript">
function fnDeleteRecord(id)
{
new Ajax.Request('delete.php?action=Delete&id='+id,{method:'get'});
$(id).remove(); // because <tr id='".$row[id]."'> :)
}
</script>

Categories