Going to someones profile - php

I have this userlist page and i want for users to be able to click the users username and it will send them to there profile page how would i go on doing this ?
This line is where the user clicks the username of a user
echo "<td class='info'><a href=''>". $people_list['username']."</a></td>";
Also in my .htaccess i have a code that makes it so i go to users profile all i do is http://www.example.com/username
<?php
include 'core/int.php';
include 'includes/head.php';
include 'head.php';
include 'includes/body.php';
include 'body.php';
$people_list="SELECT * FROM users";
$people=mysql_query($people_list);
?>
<html>
<head>
<style>
.owner {
color: orange;
}
</style>
</head>
<body>
<pre>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
</tr>
<?php
while($people_list=mysql_fetch_assoc($people)){
echo "<tr>";
if ($people_list['username'] == KillerDucky1){
echo "<td>". $people_list['user_id']."</td>";
echo "<td class='warning'><a class='owner' href=''>". $people_list['username']."</a></td>";
echo "<td>". $people_list['email']."</td>";
} else {
echo "<td>". $people_list['user_id']."</td>";
echo "<td class='info'><a href=''>". $people_list['username']."</a></td>";
echo "<td>". $people_list['email']."</td>";
echo "</tr>";
}
}
?>
</thead>
</pre>
</body>
</html>

You simply need to put the username in the <a> link tag :)
<a href='/".$people_list['username']."'>...</a>

You could try just adding the username to the href of the anchor.
echo '<td class="info">'. $people_list['username'].'</td>';
should give you something like:
<td class="info">username</td>

Related

How to use an HTML button to trigger a function?

I am populating an HTML table with information from a database and want to trigger it with a button.
Can someone help me with this, and perhaps add some links to relevant website with examples?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div method="GET">
<table>
<thead>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Usuario</th>
</tr>
<?php
include "php/populate.php";
?>
</thead>
</table>
<input type="button" value="button" id="button">
</div>
</body>
</html>
<?php
$result = mysqli_query($enlace,"SELECT * FROM tb_personas");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['txt_nombre'] . "</td>";
echo "<td>" . $row['txt_usuario'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($enlace);
?>
You need to use jQuery (the easiest way to use ajax)
take a look at this question on stackoverflow how to call a php script on a html button click
additionally you are including your data at table's header, instead they should be included in table's body.
<table>
<thead>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Usuario</th>
</tr>
</thead>
<tbody>
<?php include "php/populate.php"; ?>
</tbody>
</table>

Show specific content based on current user session PHP

My question is how to display specific content based on user session php.
I have a file called profile.php. When a user click on another user the first user gets redirected to the profile.php file. In this file I want the users to be able to see all the posts that user has made.
Image illustration:
Something like this:
<?php
if ($_SESSION['username'] == ($_GET[‘id’])) {
//DISPLAY rows with info from Database just like the attached code.
//DISPLAY edit button ONLY if the current user session is the same as the current id of the profile page.
}
?>
profile.php code below:
<?php
session_start();
require('connect.php');
if (#$_SESSION["username"]) {
?>
<!DOCTYPE html>
<html>
<head>
<title>Profile page</title>
</head>
<body>
<?php include('header.php'); ?>
<center>
<?php echo '<table border="1px;">'; ?>
<tr>
<td>
<span>ID</span>
</td>
<td width="400px" style="text-align: center;">
Name
</td>
<td width="80px" style="text-align: center;">
Creator
</td>
<td width="80px" style="text-align: center;">
Date
</td>
<td width="80px" style="text-align: center;">
Edit
</td>
</tr>
</center>
</body>
</html>
<?php
if (#$_GET['id']) {
$check_d = mysql_query("SELECT * FROM users WHERE id ='".$_GET['id']."'");
while ($row_d = mysql_fetch_assoc($check_d)) {
echo "<h1>Post made by: ".$row_d['username']."</h1>";
$check_u = mysql_query("SELECT * FROM topics WHERE topic_creator='".$row_d['username']."'");
while ($row_u = mysql_fetch_assoc($check_u)) {
$id = $row_u['topic_id'];
echo "<tr>";
echo "<td>".$row_u['topic_id']."</td>";
echo "<td><a href='topic.php?id=$id'>".$row_u['topic_name']."<br /></a></td>";
echo "<td>".$row_u['topic_creator']."<br /></td>";
echo "<td>".$row_u['date']."<br /></td>";
echo "<td><a href='edit.php?edit=$id'>Edit</a><br /></td>";
echo "</tr>";
}
}
}
echo "</table>";
if (#$_GET['action'] == "logout") {
session_destroy();
header("Location: login.php");
}
}else {
echo "You must be logged in.";
}
?>
If anyone knows how to solve this I would be most grateful!
Most of the answers I could find online involves user level distribution where the admin and user levels are predetermined. This is not what I would prefer. I simply would like the current user that is logged in to be able to edit their own posts, but not the other user posts.
I hope that this made sense, but if not, just ask!
Thanks beforehand!
// E.
If logged in user shouldn't edit other user's posts, then don't show the edit column, then you can do simple if check for the column Edit like below
while ($row_d = mysql_fetch_assoc($check_d)) {
echo "<h1>Post made by: ".$row_d['username']."</h1>";
$check_u = mysql_query("SELECT * FROM topics WHERE topic_creator='".$row_d['username']."'");
while ($row_u = mysql_fetch_assoc($check_u)) {
$id = $row_u['topic_id'];
echo "<tr>";
echo "<td>".$row_u['topic_id']."</td>";
echo "<td><a href='topic.php?id=$id'>".$row_u['topic_name']."<br /></a></td>";
echo "<td>".$row_u['topic_creator']."<br /></td>";
echo "<td>".$row_u['date']."<br /></td>";
// Add if condition here
if($_SESSION['current_logged_in_user_id'] === $row_u['topic_creator_id']) {
echo "<td><a href='edit.php?edit=$id'>Edit</a><br /></td>";
}
echo "</tr>";
}
}
but don't use mysql_* functions. use mysqli or PDOs for security reasons like protecting yourself from sql injection attacks.

Add selected data from one table to another table using 'onclick button HTML

My project is about Allergies and I am trying to add the allergies given in one table "List of Allergies" to another table "MyAllergies" with an onclick button "add" for each allergies given. What I want is when the user clicks on add button for one allergy, that specific allergy should automatically be added to the "MyAllergy.php". Please help me. I have been trying for 2 days and I got no where. However, I tried the query and it was almost correct. I would be so grateful. Thank you. Let me know if you want me to attach the other code or any more questions. :)
AddAllergy.php
<?php
include('Session.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Allergen Details</title>
<div align="center">
<h1>Allergen Information</h1>
</div>
</head>
<body>
<?php
$allergysql = 'SELECT Allergy,Description,Symptoms FROM FoodAllergy';
$retrieve = mysqli_query($connection, $allergysql);
if(!$retrieve)
{
die("Could not get data!".mysqli_error($connection));
}
print "
<table border=\"5\" cellpadding=\"7\" style=\"border-collapse: collapse\" bordercolor=\"#85144b\" width=\"100%\" id=\"AutoNumber2\" bgcolor=\"#7FDBFF\"><tr>
<th width=100>Allergy:</th>
<th width=100>Description:</th>
<th width=100>Symptoms:</th>
<th width=100>Status:</th>
</tr>";
while($rowA = mysqli_fetch_assoc($retrieve))
{
print "<tr>";
print "<td>" . $rowA['Allergy'] . "</td>";
print "<td>" . $rowA['Description'] . "</td>";
print "<td>" . $rowA['Symptoms'] . "</td>"; ?>
<td> <div align ="center"> <button type ="button" onclick="addallergy()">Add</button></div></td>
<script>
function addallergy() {
// WHAT SHOULD I ADD HERE?? //
alert ('Added to your profile!');
}
</script>
<?php
print "</tr>";
}
print "</table>";
?>
</body>
</html>

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

Retrieving row from gridview data or mySQL DB and display in PopUp

Current Grid View structure with View Anchor - I want to view a specific row after clicking on View anchor, data should be displayed in popup - javascript
Below is my code. I have already implemented functionality for PHP Grid View, Delete option is implemented at the top with querystring
Now what i want is, After clicking view, it should display javascript popup with all the details of that specific row, and close option
The part which am not getting is
how to transfer data from php/mysql to javaScript and display it in popup
`
if(isset($_GET['id'])){
$id = $_GET['id'];
//$x = 'confirm("Are you sure you want to delete this product")';
//echo $x;
mysql_query("DELETE FROM users WHERE id = '$id'");
//echo "alert('Row Deletion Successful')";
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Table Display</title>
<style>
table, td, th
{
border:1px solid green;
}
th
{
background-color:green;
color:white;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<?php $result = mysql_query("SELECT id, CONCAT(title, ' ', name) as FullName, email, mobile FROM users") or die(mysql_error());
$row_count = 1;
$row = mysql_fetch_assoc($result);
echo '<td><input type="checkbox" /></td>';
echo "<th> Sr. No </th>";
foreach($row as $col=>$value)
{
echo "<th>";
echo $col;
echo "</th>";
}
?>
<th>EDIT</th>
</tr>
</thead>
<tbody>
<?php
mysql_data_seek($result, 0);
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo '<td><input type="checkbox" /></td>';
echo "<td>" . $row_count ."</td>";
foreach($row as $key=>$value)
{
echo "<td>";
echo $row[$key];
echo "</td>";
}
$row_count++;
?>
<td>
VIEW |
DELETE |
EDIT
</td>
<?php
echo "</tr>";
}
echo "</table>";
?>
</tbody>
</table>
</body>
</html>`
you can use this code to display in your records in popup window
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>your code to display in table format</p>");
myWindow.focus();
}
<a onclick="openWin();">Edit/Delete/View(any one)</a>
try this out in document.write() method enter your code inside a table tag
render specific view in a page (e.g. viewpageaddress.php?id=7)
and then get it's content in js by:
$.get('viewpageaddress.php?id=7', function(cnt){/* show cnt in your popup */})

Categories