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

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.

Related

search multiple query in database check my code if there something wrong [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 4 days ago.
Improve this question
the problem is when i search for a name the other with same name was not displaying in landing page.
i got only 1 result, i search the name with 5 same name and different surname.
this was code in index.php
<?php
if(isset($_POST['submit']))
{
$cName = $_POST['search'];
$sql = "SELECT * FROM tbl_client WHERE cName LIKE '%$cName%'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>5)
{
$row = mysqli_fetch_assoc($result);
$cID = $row['cID'];
$cName = $row['cName'];
$cContact = $row['cContact'];
$cAddress = $row['cAddress'];
$cAirconType = $row['cAirconType'];
$cAirconQuantity = $row['cAirconQuantity'];
$cACInsClean = $row['cACInsClean'];
$cCCTVInsRepair = $row['cCCTVInsRepair'];
$cCCTVQuantity = $row['cCCTVQuantity'];
$cSubCon = $row['cSubCon'];
$cPrice = $row['cPrice'];
$cDate = $row['cDate'];
header("Location: search.php?cID=$cID&cName=$cName&cContact=$cContact&cAddress=$cAddress&cAirconType=$cAirconType&cAirconQuantity=$cAirconQuantity&cACInsClean=$cACInsClean&cCCTVInsRepair=$cCCTVInsRepair&cCCTVQuantity=$cCCTVQuantity&cSubCon=$cSubCon&cPrice=$cPrice&cDate=$cDate");
exit();
}
?>
and this one is for search.php
<?php session_start();
include 'connectDB.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Search Record List</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./clientrecords.css"/>
</head>
<body>
<div>
<table class="table">
<thead>
<tr>
<th colspan="2" scope="colgroup">
<img src="images/logosmall.png"/>
<th colspan="8" scope="colgroup">
<th colspan="2" scope="colgroup">
<span>HOME</span><i></i>
</tr>
<tbody>
<tr>
<th scope="col">ID</th>
<th scope="col">NAME</th>
<th scope="col">CONTACT #</th>
<th scope="col">ADDRESS</th>
<th scope="col">TYPE OF AIRCON</th>
<th scope="col">AIRCON QUANTITY</th>
<th scope="col">AIRCON I-C</th>
<th scope="col">CCTV I-R</th>
<th scope="col">CCTV QUANTITY</th>
<th scope="col">SUB-CON</th>
<th scope="col">PRICE</th>
<th scope="col">DATE</th>
</tr>
</th>
</thead>
<tr>
<?php
$cID = $_GET['cID'];
$cName = $_GET['cName'];
$cContact = $_GET['cContact'];
$cAddress = $_GET['cAddress'];
$cAirconType = $_GET['cAirconType'];
$cAirconQuantity = $_GET['cAirconQuantity'];
$cACInsClean = $_GET['cACInsClean'];
$cCCTVInsRepair = $_GET['cCCTVInsRepair'];
$cCCTVQuantity = $_GET['cCCTVQuantity'];
$cSubCon = $_GET['cSubCon'];
$cPrice = $_GET['cPrice'];
$cDate = $_GET['cDate'];
$sql = "SELECT * FROM tbl_client WHERE cName LIKE '%$cName%'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>5) {
echo " <td>".$cID."</td>";
echo "<td>".$cName."</td>";
echo "<td>".$cAddress."</td>";
echo "<td>".$cContact."</td>";
echo "<td>".$cAirconType."</td>";
echo "<td>".$cAirconQuantity."</td>";
echo "<td>".$cACInsClean."</td>";
echo "<td>".$cCCTVInsRepair."</td>";
echo "<td>".$cCCTVQuantity."</td>";
echo "<td>".$cSubCon."</td>";
echo "<td>".$cPrice."</td>";
echo "<td>".$cDate."</td>";
}
?>
</tr>
</tbody>
</table>
</div>
</body>
</html>
im expecting that the result will display all the name that im searching for it in my database
Do you loop over your result? In your code I don't see a loop.
Try adding this in your if statement:
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)) {
echo " <td>".$row['cID']."</td>";
echo "<td>".$row['cName']."</td>";
echo "<td>".$row['cAddress']."</td>";
echo "<td>".$row['cContact']."</td>";
echo "<td>".$row['cAirconType']."</td>";
echo "<td>".$row['cAirconQuantity']."</td>";
echo "<td>".$row['cACInsClean']."</td>";
echo "<td>".$row['cCCTVInsRepair']."</td>";
echo "<td>".$row['cCCTVQuantity']."</td>";
echo "<td>".$row['cSubCon']."</td>";
echo "<td>".$row['cPrice']."</td>";
echo "<td>".$row['cDate']."</td>";
}
}

Group list of array in a table [duplicate]

This question already has answers here:
Creating dynamic tables in HTML using MySQL and PHP
(2 answers)
Closed 11 months ago.
I want to show the list of an array in a table, I want to insert each ID in the ID column, each name in the "name" column, etc...
But it's showing all the content in single column, How I can fix it?
It shows like that right now:
[![enter image description here][1]][1]
PHP:
<table class="blueTable">
<thead>
<tr>
<th>ID</th>
<th>USER</th>
<th>MAIL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">
<div class="links">« <a class="active" href="#">1</a> 2 3 4 »</div>
</td>
</tr>
</tfoot>
<tbody>
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
echo "<tr>";
while ($row = $query->fetch_array()) {
echo "<td>" . $row['id'] . "</td>";
echo "<tr><td>" . $row['username'] . "</td></tr>";
echo "<tr><td>" . $row['email'] . "</td></tr>";
}
?>
</tbody>
</table>
[1]: https://i.stack.imgur.com/jPTR8.png
change while() loop code like below:
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
echo "<tr>"; //put <tr> opening code inside, not outside
echo "<td>".$row['id']."</td>";
echo "<td>".$row['username']."</td>"; //remove <tr></tr>
echo "<td>".$row['email']."</td>"; //remove <tr></tr>
echo "</tr>"; //</tr> need to be added at last
}
Try this
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
?>
<tr>
<td> <?php echo $row['id'] ?></td>
<td> <?php echo $row['username'] ?> </td>
<td> <?php echo $row['email'] ?> </td>
</tr>
<?php
}
?>

i want to make delete button [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 5 years ago.
Improve this question
So i start the language in this summer,and i have problem,i don't know how to make delete button,i looked lot of pages but i can't find how to make with pdo.
countryandcity.php
<?php
require 'pdo.php';
$connect=connect();
?><!DOCTYPE html>
<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="wrapper">
<table class="table" >
<thead>
<tr>
<th>Country</th>
<th>City</th>
<th>Image</th>
</tr>
<form action="deleteall.php" method="POST" >
<?php
$sql = connect()->prepare("SELECT * FROM countries ORDER BY country");
$sql->execute();
while($result = $sql->fetch(PDO::FETCH_ASSOC)) {
echo"<tr>";
echo"<td>".$result['country']."</td>";
echo"<td>".$result['city']."</td>";
if(!empty($result['image'])){
echo '<td><img src="images/'.$result['image'].'"/></td>';
}
else {
echo"<td>-</td>";
}
echo "<td><a href='edit.php?uid=".$result['country']."'>Edit</a></td>";
echo "<td><a href='deleteall.php?uid=".$result['country']."'>Delete</a></td>";
echo"</tr>";
}
?>
</form>
</thead>
</table>
</div>
</body>
deleteall.php
<?php
require 'pdo.php';
$connect=connect();
if(isset($_POST['delete_btn']))
?><!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form>
<div class="form-group">
<label>Do u want to delete?</label>
</div>
<button>
<input type="submit" value="YES" name="delete_btn">
</button>
<button>
<input type="submit" name="no" value="NO">
</button>
</form>
</body>
</html>
Please help me with the sql query and php code after if(isset), i need help!
I just want to delete on row from the database.
How can I solve this?
Sorry for my bad english.
Thanks!
Here is correction of your firsts page (content in <tbody> instead of <thead>, no <form> needed, ...) countryandcity.php :
<table>
<thead>
<tr>
<th>column name...</th>
</tr>
</thead>
<tbody>
<?php
$sql = connect()->prepare("SELECT * FROM countries ORDER BY country");
$sql->execute();
if($result = $sql->fetch(PDO::FETCH_ASSOC)) {
foreach($result as $i => $item) {
extract($item);
echo "<tr><td>$country</td>";
echo "<td>$city</td>";
if(!empty($image)) {
echo "<td><img src=\"images/$image\"/></td>";
}
else {
echo "<td>-</td>";
}
echo "<td>Edit</td>";
echo "<td>Delete</td></tr>";
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
And here solution for deleting your record in deleteall.php :
<?php
if(isset($_GET['delete_btn'])) {
$country = addslashes($_GET['delete_btn']);
$q = "DELETE FROM countries WHERE country = '$country'";
require 'pdo.php';
$sql = connect()->prepare($q);
$sql->execute();
}
?>
hope it will help you, you can add delete link then go this page it will delete
Edit
Delete
<?php
require 'pdo.php';
$connect = connect();
?>
<!DOCTYPE html>
<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div class="wrapper">
<table class="table">
<thead>
<tr>
<th>Country</th>
<th>City</th>
<th>Image</th>
</tr>
<form action="deleteall.php" method="POST">
<?php
$sql = connect()->prepare("SELECT * FROM countries ORDER BY country");
$sql->execute();
while ($result = $sql->fetch(PDO::FETCH_ASSOC)) {
if (!empty($result['image'])) {
$content_image = '<td><img src="images/' . $result['image'] . '"/></td>';
} else {
$content_image = '<td>-</td>';
}
?>
<tr>
<td> <?= $result['country'] ?></td>
<td><?= $result['city'] ?></td>
<?= $content_image ?>
<td>Edit</td>
<td>Delete
</td>
</tr>
<?php }
?>
</form>
</thead>
</table>
</div>
</body>
</html>
then this is your deleteall.php
<?php
$uid = trim($_GET['uid']);
if(isset($uid)) {
$sql = "DELETE FROM countries WHERE country = ?";
$q = connect()->prepare($sql);
$response = $q->execute(array($uid));
}
?>
For the form, I'd recommend having a button that acts as the function, that way you don't accidentally lose all your data when the page loads, or anything like that. This is simply three lines of code.
<form action="deleteall.php" method="POST">
<input type="hidden" value="true" name="cameFromForm">
<button type="submit" name="confirmButton">Delete All</button>
</form>
When the button is clicked, then it will trigger the entry point of deleteall.php, which then takes the input and deletes ALL data from the table.
<?php
if(isset($_POST["cameFromForm"]) && $_POST["cameFromForm"] == "true") {
// This makes sure that the form actually sent it.
try {
$tableName = "Insert the name of your table here";
$mysqli = new mysqli(
'databaseHost',
'databaseUser',
'databasePassword',
'databaseName');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $mysqli->prepare("DELETE FROM ?");
$stmt->bind_param('s', $tableName);
$stmt->execute();
$stmt->close();
$mysqli->close();
header('Location: http://afterDeltePage.com');
}
catch (Exception $e) {
print($e);
die();
}
}
else {
header('Location: http://notAuthorisedPage.com');
}
?>
Once you've finished the process, you should send the user to a page with UI. Try to separate the things you need to show the user and your processes. Also, if this is going to be public, you need to make sure that deleteall is not accessible by anyone by URL.
To do that, you need to move your file out of the wwwroot (public_html usually). There are various topics on how to do that.
PHP Forms information and PHP MySQLi information.

Updating a row of SQL with html table

I have the following code:
$sql = "SELECT * FROM Tickets WHERE stat='Open'";
$result = mysql_query($sql);
mysql_close($con);
?>
<!DOCTYPE>
<html>
<body>
<table class="striped">
<tr class="header">
<td>Username</td>
<td>Title</td>
<td>Description</td>
<td>Admin Name</td>
<td>Category</td>
<td>Status</td>
<td>Urgency</td>
<td>Time In</td>
<td> </td>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[username]."</td>";
echo "<td>".$row[title]."</td>";
echo "<td>".$row[description]."</td>";?>
<td><select>
<?php
echo "<option value'".$row[admin_name]."'>".$row[admin_name]."</option>";
$sql = mysql_query("SELECT username FROM Users WHERE user_type='admin'");
while ($u = mysql_fetch_array($sql)){
echo "<option value='".$u['username']."'>".$u['username']."</option>";
}
?>
</select></td>
<?php
echo "<td>".$row[category]."</td>";
echo "<td>".$row[stat]."</td>";
echo "<td>".$row[urgency]."</td>";
echo "<td>".$row[time_in]."</td>";
echo "<td><a href='close.php'>Close Ticket</a></td>";
echo "</tr>";
}
?>
</table>
<a href='update.php'>Update</a>
</body>
</html>
I have two links on this page. Both of them need to update a SQL database. The Close ticket link needs to just update the single row, while the update link should update all of them. I am not sure how to get the info from one php to the next. It seems like you can put the individual row information into a Post array for the close ticket link, but I am not sure how. For the update link it needs to take the value of the dropdown in the table and change the admin_name field to that value.

How to request user ID or delete a row in 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

Categories