Can I have one page for all profiles? [duplicate] - php

This question already has answers here:
How do I pass parameters into a PHP script through a webpage?
(2 answers)
Closed 1 year ago.
The idea is to click on the name of an employee and go to a page called profile.php. This page will always be profile.php but will output different information depending on which employees name is selected. This is the code I have so far.
<?php
$sql = "SELECT id, firstname, lastname, role, branch FROM users WHERE role='maintenence' ORDER BY firstname, lastname ASC;";
$result = mysqli_query($db, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<a href='profile.php'><tr><td class='col-s-4'>" . "<a href='profile.php'>" . $row['firstname'] . " " . $row['lastname'] . "</td><td class='col-s-4'>" . $row['role'] . "</td><td class='col-s-4'>" . $row['branch'] . "</td></tr></a>";
}
}
?>
It is doing exactly what I want it to do at the moment. But if the above is possible, how can I store a variable of the employee chosen (employee id) so I can use that variable to output information on profile.php. Thank you.

Generally, you use a parameter from a request parameter ($_GET or $_POST or $_REQUEST) or something from the session to seed your query.
"<a href='profile.php?id=" . $row['employee_id'] . "'>" . $row['firstname'] . "</a>"
In profile.php, you can access the request parameter as $_GET['id'].

According to my understanding of your question, You can use AJAX here to dynamically change content without refreshing the page...If you're not familiar with AJAX requests, check out some tutorials, it is easy to implement and perfect if you need to load content dynamically. i.e. Loading details like employee salary etc based on whichever employee is selected...This can be implemented mainly using javascript/jquery and AJAX.

Related

PHP delete functionality probably logical error

In the top of my admin PHP (admin.php), I have loaded data from "Sport" table in "DogSport" MySQL database. After clicking "Edit" link of a particular record, I can
edit, delete that record on "editform2.php". Even on that page, I can also insert a new record.
Problem: Deleting first and second records do not work! Deleting all following records work fine. This problem only with deleting functionality.
Code segment - admin.php
<?php
include("dbconnect.inc.php");
//Retrieving Data
$query1="SELECT * FROM Sport ORDER BY SportId ASC";
$result2=mysqli_query($con, $query1);
$rows=mysqli_num_rows($result2);
$i=0;
if($rows==0)
echo "<br/>There are no records";
else{
echo "<table id='sport'>";
echo "<tr><th>Sport Id</th><th>Sport Name</th><th>Description</th></tr>";
while ($row = mysqli_fetch_array($result2))
{
echo "<tr><td>" . $row["SportId"] . "</td><td>" . $row["SportName"] . "</td><td>" . $row["Description"] .
"</td><td> <a href='editform2.php?id=" . $row["SportId"] . "' target='_blank'>Edit</a></td></tr>";
$i++;
}
echo "</table>";
}
?>
Code segment - editform2.php
if(isset($_POST["delete"])){
include("dbconnect.inc.php");
$query3="DELETE FROM Sport WHERE SportId=$id";
mysqli_query($con, $query3);
echo "<h1>Deleted Successfully</h1>";
mysqli_close($con);
}
I have uploaded necessary files, you can also use to check it including database .sql script. Your help is appreciated.
https://www.dropbox.com/s/wn40u93oa1sxcph/SO.rar?dl=0
$query3="DELETE FROM Sport WHERE SportId=$id";
Where did you set value of $id? It should be $_POST["id"] right?
As a side note - SQL injection risk ...

How to get user information from a MYSQL local database

Hey i need to get information from a database and display it on my index.php.
<html>
<body>
<?php
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('users');
$query = "SELECT * FROM employee"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['age'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
?>
</body>
</html>
however this will show the information for all of the users in the database.
I have a primary key in the database, which is the account number, could we use this to only display information about that account on the page only?
I will also need to save the details into variables on the php page so some guidance doing that would be greatfull.
Thanks Oliver
In order to retrieve data according to a specific user you must explicitly tell mysql. So, add WHERE id={the I'd u want the info for}.
For your second question. "How do u get the id when the user logs in".
The answer to this is you can (a) how a session initialized at login or (b) how another file or even a loop in the script you need the Id for to run a query for the users name and then you get the Id.
The draw backs of option b is that you are slowing this done and are doing alot of call which can crash your overall site.
The choice is your this is how you can get a particular I'd for a user.
<?php
include('session.php');
?>
<html>
<body>
<?php
$connection = mysql_connect('localhost', 'root', 'Oliver'); //The Blank string is the password
mysql_select_db('users');
$query = "SELECT * FROM username WHERE username='$login_session'"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['username'] . "</td></tr>"; //$row['index'] the index here is a field name
echo "<tr><td>" . $row['sex'] . "</td><td>" . $row['phone'] . "</td></tr>"; //$row['index'] the index here is a field name
echo "<tr><td>" . $row['email'] . "</td><td>" . $row['password'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
?>
</body>
</html>
I got the WHERE username="$login_session"
and the login_session value is a unique value for each of the users in the database so it can never be the same, so i can use this to pull up information for that user only
You can use session variables to store the information in the case you are working with sessions, if not, you can use a regular variable to store the information or maybe use post or get variables

How to pass variables from one PHP file to another using HTML links?

//DB CONNECTION
$sql = "SELECT `city`,`country` from infotab";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row["city"].$row["country"]"<a href='order.php'>order</a>"; }
Table output:
This code will select data. Additionally, there is reference to order.php on every line. When the user clicks on reference( <a href> clause), it opens order.php and there I need to know which row the user selected to work with these data.
Change the code to:
while ($row = $result->fetch_assoc()) {
echo $row["city"] . $row["country"] . "<a href='order.php?city=" . $row["city"] . "&country=" . $row["country"] . "'>order</a>";
}
In order.php you can then access these values by using the $_GET["city"] and $_GET["country"] variables which contain the values from your <a href> link on the previous page. For example, running echo $_GET["city"]; will output the city name.
Edit: As #Rizier123 pointed out, using a unique ID might be more prone to errors in case your database contains more than one entry for the same city or country. You should consider introducing an ID in your table structure and then using that in the link to order.php.

how to add a view button in table ?

In my script i want to add a view link that will redirect to me next page and show the values of that perticuler field ...
out put of my script is like this-:
username1 1086769 view complete details
******** ******* view complete details
so if i click on view complete details of row 1 .. then i want full details of username1 in next page like d_mail , d_phone etc.
include 'config.php';
$list="select d_name,d_amount from donated where d_apprv= 1 order by d_id desc limit 10;";
$data=mysqli_query($con,$list);
echo "<table border='1'>";
echo "<tr><td>" ."<strong>NAME OF DONER(s)</strong>" . "</td><td>" . "<strong>AMOUNT DONATED</strong>" . "</td></tr>";
while($info = mysqli_fetch_array($data))
{
echo "<tr><td>" . $info['d_name'] . "</td><td>" . $info['d_amount'] . "</td><td>"."<a href='view.php'>view complete details</a>" ."</td></tr>";
}
echo "</table>";
As I commented, it seems like you are already using a link like <a href='view.php'>view complete details</a> but you aren't passing any values in that, so you should do it like
<a href='view.php?user_id='<?php echo $info['user_id_column_name']; ?>>view complete details</a>
And then on another page, use the value of user_id to pull the relevant data from the database using $_GET['user_id'].
Don't forget to sanitize value retrieved using $_GET before pulling the details from DB

Combine $_GET and $_POST in PHP?

for some friends and family (different sites), I created a script that allows them to input data into the database. With
echo ("<a href=\"./pagina.php?ID=" . $row['ID'] . "\">" . $row['ID'] . "<br>");
, I 'send' the ID of the requested table to the URL.
In pagina.php, I have this code:
ID: <?php echo $_GET["ID"]; ?>
That works, of course, but now I want to use that ID to also display the data from the database, so not from the URL. These values are " . $row['onderwerp'] . " and " . $row['tekst'] . "
(There may be more values to come, but I'm just a beginner, trying to get something to work).
I know this is possible, but I just can't get anything to work, as I have just started learning PHP.
I hope you can help me.
If you don't care whether data came from a $_COOKIE, $_GET, or $_POST, you can use $_REQUEST.
$id = (int)$_GET['id'];
$sql = "SELECT onderwerp, tekst FROM yourtable WHERE id=$id";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
echo "{$row['onderwerp']} - {$row['tekst']}<br />";
}

Categories