Include different mysql data on a page based on the users choice - php

I have a problem here I can't solve. I have a database of houses with country, location, price etc entities. I print only the countries in a table using:
while ($data = mysql_fetch_array($select))
where $select is a query that selects specific data from the db. I want to display the full information of the selected db data on a different page i have created. The problem is that i don't know how to take the price of the data the user selected. When i get the price of the $_SESSION['counter'], its the number of the last entity of the db. . I don't want to use javascript etc. Here's the code of the page:
<?php
require_once 'php/core.php'; // opens the database
require_once 'php/openDB.php'; // starts the new session
$select = mysql_query("SELECT ID, Country, City FROM Houses");
$counter = 1;
echo "<form action='house_profile.php' method='get'>
<table width='400'>
<tr>
<td>No.</td>
<td>Country</td>
<td>City</td>";
echo "<tr>";
while ($data = mysql_fetch_array($select))
{
echo "<tr>";
echo "<td>" . $counter . "</td>";
echo "<td>" . $data['Country'] . "</td>";
// echo "<td>" . "<a href='house_profile.php' type='submit' value='$counter'>" . $data['City'] . "</a>" . "</td>";
echo "<td>" . $data['City'] . "</td>";
echo "<td><input type='submit' value='info' name='" . $counter . "'></td>";
echo "</tr>";
$_SESSION['counter'] = $counter;
$counter++;
}
echo "</table>
</form>";
?>`

Clarification:
You can pass any value to house_profile.php by adding a ? to start the querystring, then the name of the variable and the value. If you want to pass the id to house_profile then the link should look like this.
"<a href='house_profile.php?ID=" . $data['ID'] . "'>"
then in house_profile.php update your query like this
$select = mysql_query("SELECT * FROM Houses where ID = " . $_GET['ID']);
Now only the entity they clicked will be available.
FYI $counter is just the count, it's not necessarily the ID of the entity. e.g. if you delete ID 1 your records will start from ID 2 but counter will always start with 1. So to reference your entities correctly use the database ID.

Your query retrieves each house's ID from the database but doesn't display this (unless I don't understand your code). When the user chooses a house, you have to retrieve the ID and then use this in a separate query, such as
select price
from houses
where id = :ID
Currently you're displaying a line counter on your form as opposed to the house's ID - which explains why you write...
When i get the price of the $_SESSION['counter'], its the number of the last entity of the db.
You're passing the value of the row counter to the second query instead of the house's ID.

Related

How to pull data from MySQL database with PHP based on DB entry ID

I am complete newbie to PHP/SQL and all this stuff, also not really skilled with any kind of programming. My problem is that I am currently trying to pull out data from MySQL table on to website, but after finishing the code it pulls out all of the data from my table.
I would love to somehow get to pull out only data from specific table row, based on it's primary key. My current code looks like this.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rainbow";
$link = mysqli_connect($servername, $username, $password, $dbname);
if($link === false){
die("ERROR: COuld not connect." . mysqli_connect_error());
}
$sql = "
SELECT name
, nick
, surname
, team
, country
, birthdate
, mouse
, dpi
, keyboard
, headset
FROM players
";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>name</th>";
echo "<th>nick</th>";
echo "<th>surname</th>";
echo "<th>team</th>";
echo "<th>country</th>";
echo "<th>birthdate</th>";
echo "<th>mouse</th>";
echo "<th>dpi</th>";
echo "<th>keyboard</th>";
echo "<th>headset</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['nick'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['team'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['birthdate'] . "</td>";
echo "<td>" . $row['mouse'] . "</td>";
echo "<td>" . $row['dpi'] . "</td>";
echo "<td>" . $row['keyboard'] . "</td>";
echo "<td>" . $row['headset'] . "</td>";
echo "</tr>";
}
echo "</table";
mysqli_free_result($result);
}
else {
echo "Ziadny vysledok a nic nefunguje";
}
}
mysqli_close($link);
?>
To be honest, I am not even sure if this is the right way to do it, but it works and it pulls the data into a HTML table which is not necessary for me, I just wanted to try it. Thanks for answers!
Let's suppose you have a page with this code:
<form action='page2.php' method='post'>
Inform a number:
<input type='text' name='number'>
<input type='submit' value='Send'>
</form>
Save the form above as page1.php
The file page2.php will contain the code that select one register from your table and show the result.
<?php
$id = $_POST["number"];
//The sql command will look like this:
$sql = "SELECT name, nick, surname, team, country, birthdate, mouse, dpi, keyboard, headset FROM players WHERE id = $id";
?>
A couple of things.
Firstly, You would typically keep some of your confidential stuff (database name, userid, password and server name in a separate file (eg. config.php) and you would "include" that file in this file... include ("config.php");
Secondly, in your $sql line you are selecting all of your columns individually and later in the 10 lines after your "while" statement you are selecting them again (for display). I would be calling the whole table in your $sql line with SELECT * from players and I would follow this with a WHERE. As a newbie, your basic retrive from batabase has 3 main words, SELECT (means go and get what you want and in most cases, grab it all with a *) FROM (the table you want to get it from, in your case the table is players) and WHERE (this is your selection criteria... id > 50...colour = "blue"... whatever you want). When pushing data to the DB you would use SET and UPDATE but when retrieving... SELECT, FROM, WHERE.
Your "while" statement will simply do your $sql statement (select, from, where) and return results until it runs out of records
Good luck Newbie
gri2a

Using anchor tag send id to next page in php

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']";

How to select a specific row from a table to edit or delete

I have implemented a booking system, part of the system involves allowing users to modify or delete a booking. I am able to retrieve bookings from the database based on user's session and id which is displayed in a html table. I have been having trouble selecting a specific row from the html table to edit or delete a booking. I want to be able to select a specific row from the html table which i will then be able to edit or delete that record.
The code below retrieves the bookings and displays it in a table
bookings.php
$sqlquery = "SELECT * FROM bookings1,users WHERE bookings1.userid =
users.userid AND users.username = '".$_SESSION['loggedInUser']."' ";
$results = $mysqli->query($sqlquery);
if ($results->num_rows > 0) {
while ($row = $results->fetch_assoc()){
echo'<form method = "POST" action = "editmot.php" >';
echo'<tr>';
echo "<td>". $row["booking_id"]. "</td>";
echo "<td>". $row["Type"]. "</td>";
echo "<td>". $row["BookingDate"]. "</td>";
echo "<td>". $row["Timeslot"]. "</td>";
echo "<td>". $row["Manufacture"]. "</td>";
echo "<td>". $row["Model"]. "</td>";
echo "<td>". $row["RegistrationNo"]. "</td>";
echo "<td><a href = 'editmot.php' id ='".$row['booking_id']."'> Edit </td>";
echo "<td><a href = 'delete.php' id = '".$row['booking_id']."'> Remove </td>";
echo '</tr>';
echo '</form>';
In the href attribute of the Edit Link, include the Booking_Id as a URL parameter. When the Edit page loads, pick up the booking_id from the URL parameter and fetch the other fields for edit. Like :
echo " <a href = 'editmot.php' . '?EditBookingId=' . $row['booking_id'] "
and then so on and so forth concatenate other things.
Your final href URL should look like :
'http://editmot.php?EditBookingId=24798' or some such.
use this code to edit or delete
for edit
for delete
if(isset($_REQUEST['action']))
if($_REQUEST['action']=="update")
update query
else if($_REQUEST['action']=="delete")
delete query
Instead of using the anchor tag, you could use a submit button and a hidden input field to hold the booking ID, since each booking detail (or row) is in a separate form. You could use something like this in each booking row:
echo "<td><input name='edit' type='submit' value='Edit'>"
. "<input type='hidden' name='booking_id' value='".$row['booking_id']."'></td>";
echo "<td><input name='delete' type='submit' value='Remove'>"
. "<input type='hidden' name='booking_id' value='".$row['booking_id']."'></td>";
And then in editmot.php, process the delete or edit form action:
if (isset($_POST['edit'])) {
// edit video here
}
if (isset($_POST['delete'])) {
// delete video here
}
You need to insert the id in the url so you can get it with the $_GET method in the edit or delete file.
So your anker needs to look something like:
<a href = 'delete.php?id=".$row['booking_id']."'>Delete</a>;
Now when a user go's to that url the url wil look something like:
localhost/delete.php?id=20
Now in your delete.php you can get the id with the $_GET method:
$id = $_GET['id'];
$qry = "DELETE FROM booking1 WHERE id=$id"; //this will delete the row where id is 20

Select, Insert MySQL and PHP

I have created a search function and was able to display the rows.
But I have no idea what to do about this thing:
For example, I searched name starting with J, and lists all the names starting with J with login time and has not been logout. Meaning, the timeOut has been set to null from the first place. It displays like:
1 | Johny | 7:58 | (timeOut)
4 | Jess | 8:05 | (timeOut)`
The (timeOut) is a input type="button". And I what I want is whenever I click the logout, for example, I click timeOut for Johny, it will insert values now() for the timeout, not affecting the row for Jess.
I have this code:
<?php
$result=mysqli_query($con,"SELECT * FROM records WHERE PlateNumber LIKE '%$name%'");
while($row = mysqli_fetch_array($result)) {
if($row['TimeOut'] == null){
echo "<table border='1'>" . "<tr>" ."<td style='width:100px;'>" . $row['Number']. "</td>". "<td style='width:100px;'>" . $row['PlateNumber']. "</td>". "<td style='width:100px;'>" . $row['TimeIn'] . "</td>" ."<td style='width:100px;'>" . "<form>" . "<input type='submit' name='logout' value='Log Out'/>" . "</form>". "</td>" . "</table>" ;
if(isset($_POST['logout'])){
//HELP ME IN THIS PART
}
}
}
?>
I'm not really good at php so please bear me with this. Thanks for your help.
I think you meant to update not to insert, and you have to feed the userid to update that particular row.
Also you do not need to spawn every form in the loop. Wrap the table with the form instead.
if(isset($_POST['logout'])) { // if logout button is submitted
// do not insert but update
$id = $con->real_escape_string($_POST['logout']);
$query = $con->query("UPDATE records SET TimeOut = NOW() WHERE id = '$id'");
}
$name = $con->real_escape_string($name);
$result = mysqli_query($con, "SELECT * FROM records WHERE PlateNumber LIKE '%$name%'");
echo '<form method="POST">';
echo '<table boder="1" cellpadding="10">';
while($row = mysqli_fetch_assoc($result)) {
if(empty($row['TimeOut'])){
$id = $row['id'];
echo
"<tr>" .
"<td style='width:100px;'>" . $row['Number']. "</td>".
"<td style='width:100px;'>" . $row['PlateNumber']. "</td>".
"<td style='width:100px;'>" . $row['TimeIn'] . "</td>" .
"<td style='width:100px;'>" .
"<button type='submit' name='logout' value='$id'>Logout</button>" . "</td>" .
"</tr>";
}
}
echo '</table>';
You need to put a record ID of some sort in the 'value' portion of your input. Not just "log out". You need to post "log out jess" (or better yet, just "Jess" so you don't have to edit it when you retrieve it to remove "Log Out" from the string). Usually this is done with a primary key field. But it can also be done with any unique field. So instead of value='Log Out' try value='Jess'.
Then $_POST['logout'] will actually contain Jess and you can build your update sql query from it.
Another method would be to skip the form entirely and just build a web link on the "Log Out" next to "Jess" which would contain "GET" parameters instead of POST. May be easier on the eye.
Instead of a select statement you need to use an update statement and use a where clause to scope the record to the current user based off user id.
So for you it would be like this:
UPDATE records
SET logouttime=now()
WHERE userid='4'

PHP Add button to view all the contents of entire record MySQL

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);

Categories