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
Related
I am showing a html table which is retrieving the data from mysql using php,the table has a column feedback, the elements in this column are hyperlinked, they direct you to program called feedback.php where a feedback form is there with candidate name and id retrieved from database and shown there , below is the code showing the data displayed on table page
echo " <header class='w3-container w3-black'>
<h1>TABLE Of Faculty Position Applicants :-</h1>
</header>
<form action = '' method = 'post'>
<table class ='w3-table w3-striped w3-border'>
<tr class ='w3-grey'>
<th>Candidate No </th><th>Candidate Name</th><th>Google Scholar</th><th>DBLP</th><th>CV</th><th>Feedback</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['candidate_no'] . "</td>";
echo "<td>" . $row['candidate_name'] . "</td>";
echo "<td><a href=" . $row['gs_link'] . " target='_blank'>Google Scholar</a></td>";
echo "<td><a href=" . $row['dblp_link'] . " target='_blank'>DBLP link</a></td>";
echo "<td><a href=" . $row['cv_link'] . " target='_blank'>CV</a></td>";
echo "<td><a href=" . $row['feedback_link'] ." id = ".$row[id]. " target='_blank'>Feedback</a></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
now you can see this line :-
echo "<td><a href=" . $row['feedback_link'] ." id = ".$row[id]. " target='_blank'>Feedback</a></td>";
which I am using to pass the id to feedback.php
on feedback.php this query is written which requires id and retrieved id of the specific candidate using id which is passed to this page
$query="SELECT candidate_name FROM faculty WHERE id=$_POST[id]";
$query2="SELECT candidate_no FROM faculty WHERE id=$_POST[id]";
$result = mysqli_query($conn,$query);
$result1 = mysqli_query($conn,$query2);
$row= mysqli_fetch_assoc($result);
$row2= mysqli_fetch_assoc($result1);
$conn->close();
I want to know the way to pass id to feedback.php so that I can retrieve candidate name and id on that program and show it in form which I will display because in above program I am doing some mistakes or is using wrong way to pass the id to feedback.php , please help me if there is problem I will clarify , help me.
You can pass the id as a GET variable, making a link something like this:
feedback.php?id=123
Then on feedback.php you can catch the ID by using $_GET['id'] where id is the name of the variable and the return will be whatever is set in it, in our example it will be ID 123.
You can then run your query on these IDs.
Use hyperlink queries.
http://mywebsite?data=test
server. Queries
You can access them easily in php:
<?php
$data=$_GET["data"]
?>
use $_GET instead of $_POST
$query="SELECT candidate_name FROM faculty WHERE id=$_GET['id']";
$query2="SELECT candidate_no FROM faculty WHERE id=$_GET['id']";
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 ...
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
//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.
Currently I have a query running that brings up all the contents of the 'products' table and the 'user' associated with the products. It prints on a table. I want to create a button, that brings up the entire records.
Please note that I must only be able to view the selected record only.. How would I go about this?
echo "<table border='1'>
<tr>
<th>user_id</th>
<th>user_name</th>
<th>selected_product</th>
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['selected_product'] . "</td>";
So a button should appear for each record that is returned as a result. When I click this button another window should appear which shows the entire contents (for this record only) and the user associated.
How does the button know which record its associated with.
<a href="anotherpage.php?id=<?php echo $row['user_id']?>" ><button>View Details</button></a>
This will take you to another page.
Use get method tho get the id into php variable.
$userId = $_GET['id'];
And now use sql query to fetch all the data from the database for single user.
$userResult = mysql_query("SELECT * FROM userTable WHERE user_id = ".$userId);