Save id of link clicked as a session - php

Lets start from the beginning. I have a database which stores a links the name which is placed over the link and auto increment ids for each link. My code brings up all of the links in the database when the link is clicked it takes me to the default page for each link. but heres the problem. the content on the page has to depend on the id of the link which is clicked. is there a way to save the id of the link which is clicked as a session so i can view the content on the next page.
My code so far
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "score";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, description FROM all_scores ORDER BY id DESC LIMIT 5";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<p></p>";
echo "<a href=score2/view.php>". $row["name"]. "</a>";
echo "<p>". $row["description"]. "</p>";
}
} else {
echo "0 results";
}
$conn->close();
?>
how can i save the if of the link clicked as a session. the link is default so it never changes but the name description and id have an id and the id from them is what i need saved.
i have looked everywhere and i need an answer or something to help me.

You can append the id of the page to the link itself. So your while loop should be like this:
while($row = $result->fetch_assoc()) {
echo "". $row["name"]. "";
echo "<p>". $row["description"]. "</p>";
}
And on view.php page catch the pageid like this:
$pageid = $_GET['pageid'];
// Now display the page

Related

Show records based on a catergory value

I have a table with data in it ID, Name, Task, Business. I am trying to show all records based on the Business category.
I would like a separate page to show all the businesses and then when you click on the name it displays all the records associated with it.
I am unsure of how to display this.
I can almost see what I want in SQL workbench but I am unsure how to do this is php code..
SELECT * FROM job2 where business="MR Plumber"
I want the "Mr plumber" to be what ever business I click on the business page.
I would like to have a page to list the businesses and then when I click on the business name link and displays all records associated to it.
Try this.
<?php $servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
echo "error";
}
$sql = "your sql";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["column_name"];
}
} else {
echo "0 results";
}
$conn->close();
?>

How to pass a variable from one page to another with PHP?

I support a database how can I share a variable from one page to another?
My page choose.php when it is loaded generates buttons with a field value of a database table as value.
I have to make sure that at the click of the button:
- save me a table data ("id")
- I am redirected to another page
- on the page where I am redirected to get the variable and put it in a query
it's possible? If so how?
<!DOCTYPE html>
<?php
session_start();
if(!isset($_SESSION["username"])){
header('location: ../index.php');
}else
{
?>
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT idCantiere,nomeCantiere,codiceCommessa,indirizzoCantiere FROM Cantiere";
$result = $conn->query($sql);
echo'<h1> <font face="verdana" color="green">Quale Cantiere desideri Modificare?</font> </h1>';
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo'<br><br><br>';
echo'' . $row["nomeCantiere"] . '';
}
echo'<br><br><br>';
echo 'Nuovo Cantiere +';
} else {
echo "0 results";
}
$idCantierePerSelect = $_POST["idCantiere"];
global = $idCantierePerSelect;
echo $idCantierePerSelect;
$conn->close();
?>
For now I only managed to do the automatic loading of the buttons ...
and I thought of putting "idCantiere", which is the field that I have to go from table to table, global
One way of passing variables between pages is 'posting' it in the URL.
This question has been answered before, look here.
Passing multiple variables to another page in url
In short add:
Then at index.php do
$idCantiere = $_GET['idCantiere']

php html remove button to delete a MySQL row

I am trying to delete a MySQL row being displayed on a page. Yes, I want to delete the entry from the database.
I have tried several of the solutions noted throughout these forms and other google related searches for what I am trying to accomplish.
Here is my current code:
<?php
// Display information from table & add remove button
$servername = "";
$username = "website";
$password = "";
$dbname = "website";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT location, thelink, status FROM main_page";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table align=\"center\"><tr><th align=\"center\">Image</th><th align=\"center\">Information</th></tr><br />";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td><img src=".$row["location"]. "><br /></td><td align=\"center\"><br /> ".$row["status"]. "<br /><br />Delete</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
When I try to add the $row variable to the end of the delete.php line it either doesn't show up or the page won't load because of an error.
At the link do
Delete
Now on the delete.php do
<?php
if(isset($_GET["id"])){
$id=$_GET["id"];
// HERE YOU CAN MAKE QUERY TO DELETE THE RECORD
}
?>
You'll need to pass through the ID for each row into the link to delete itself:
if ($result->num_rows > 0) {
echo "<table align=\"center\"><tr><th align=\"center\">Image</th><th align=\"center\">Information</th></tr><br />";
while($row = $result->fetch_assoc()) {
echo "<tr><td><img src=".$row["location"]. "><br /></td><td align=\"center\"><br /> ".$row["status"]. "<br /><br />Delete</td></tr>";
}
echo "</table>";
}
Then on delete.php you would need to check the ID based on the id $_GET parameter:
// Connection needs to be defined again -- ideally from an include()
$conn = new mysqli($servername, $username, $password, $dbname);
if(isset($_GET["id"])){
$stmt = $conn->prepare("DELETE FROM table WHERE ID = ?");
$stmt->bind_param('i', $_GET["id"]));
$stmt->execute();
$stmt->close();
}
Keep in mind that this is incredibly dangerous to do a deletion through a $_GET request, as someone could simply visit delete.php?id=1 through their browser and delete a given record; you'll probably want to incorporate $_POST into your deletions.
Hope this helps! :)

Getting User Data Based on Their Information

This first field is where a web visitor will enter in the 'cardname' hit submit and be directed to another page (dashboard2.php) where only his or her content will appear.
Enter your cardname to access your content<br>
<form action='dashboard2.php'>
<input type='text' name='cardname'/><input type='submit' value='retrieve card'/>
</form>
</body>
The page below is the page that is directed after the user enters in the 'cardname' from the first input field. However, I only want this second page to show the information based on the cardname that was entered. Right now, it shows every single cardname, questionone, answerone from that table.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "flashcards";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT cardname, questionone, answerone FROM cards";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> ". $row["cardname"]. " ". $row["questionone"]. " " . $row["answerone"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
You have to modify the query to accept a WHERE clause. For instance, WHERE cardname = mysqli_real_escape_string($conn, $_GET['cardname']) (The default method for any form is GET unless you specify method="post".).
You should learn about prepared statements for MySQLi and perhaps consider using PDO, it's really not hard.
It seems that you want to perform a search and not a display all the records.
Usually a search returns records that match a certain field, unless a specific ID or unique value was entered in the search. I'm not sure this is the case.
I put this together a little quick but hopefully it helps...
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "flashcards";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// escape the string to avoid SQL injections
$searchEscaped = $conn->real_escape_string($_POST['cardname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT cardname, questionone, answerone FROM cards WHERE cardname = '$searchEscaped' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($result->num_rows == 1){
// only one result found, show just that
$row = $result->fetch_assoc()
echo "<br> ". $row["cardname"]. " ". $row["questionone"]. " " . $row["answerone"] . "<br>";
}else{
// multiple rows found, show them all
while($row = $result->fetch_assoc()) {
echo "<br> ". $row["cardname"]. " ". $row["questionone"]. " " . $row["answerone"] . "<br>";
}
}
} else {
echo "0 results";
}
$conn->close();
?>

mysqli create link based on row

I want to create an link based on row value, AND - MORE IMPORTANT, add an variable - if row empty, then this text... if row have some value, display this....
//I NEED TO GET THIS DISPLAY:
// if row season is empty, the resulting link must be
// web.com/MOVIES/".$row["title_id"]."
// if row season have some value, then the link must be: //web.com/SERIES/".$row["title_id"]."/SEASON/".$row["season"]."/EPISODE/".$row["episode"]."
This is the base code.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, type, label, title_id, season, episode, approved FROM links order by id desc LIMIT 30 OFFSET 50";
$result = $last_id = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Label</th><th>URL</th><th>Season</th><th>Episode</th><th>Approved</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["label"]."</td>";
// --------------------------------------------------------------
echo "<td><a href='http://web.com/(VARIABLES HERE)'>";
echo " ".$row["title_id"]."</a></td>";
// --------------------------------------------------------------
//I NEED TO GET THIS DISPLAY:
// if row season is empty, the resulting link must be
// web.com/MOVIES/".$row["title_id"]."
// if row season have some value, then the link must be:
//web.com/SERIES/".$row["title_id"]."/SEASON/".$row["season"]."/EPISODE/".$row["episode"]."
// -----------------------------------------
echo "<td>".$row["season"]."</td><td>".$row["episode"]."</td><td>".$row["approved"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
As is stated in the comments, you are using a reserved MySQL keyword: type. So your query is not working properly. You can't tell because you haven't set up any checks to make sure it is working. you can either put backticks around it like so:
SELECT id, `type`
or change the name of the key in your database (I'd recommend this approach, something like titleType). Until you fix this, nothing will work in your PHP.
Once you have fixed this, as for how to generate your results, you could do something like this inside your while loop (I'm assuming your mean the episode value):
if (empty($row['episode'])) {
echo 'web.com/MOVIES/'.$row["title_id"];
}
else {
echo 'web.com/SERIES/'.$row['title_id'].'/SEASON/'.$row['season'].'/EPISODE/'.$row["episode"];
}
THIS IS THE CORRECT FULL CODE.
<?php
$servername = "localhost";
$username = "WRITE YOUR DB username ";
$password = "WRITE YOUR DB password";
$dbname = "WRITE YOUR db name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, label, title_id, season, episode, approved FROM links order by id desc LIMIT 30 OFFSET 1";
$result = $last_id = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Audio</th><th>URL</th><th>Temporada</th><th>Episodio</th><th>Aprobado</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["label"]."</td>";
echo "<td><a href=";
if (empty($row['episode'])) {
echo '/peliculas-online/'.$row["title_id"];
}
else {
echo '/series-online/'.$row['title_id'].'/seasons/'.$row['season'].'/episodes/'.$row["episode"];
}
echo ">".$row["title_id"]."</a></td>";
echo "<td>".$row["season"]."</td><td>".$row["episode"]."</td><td>".$row["approved"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>

Categories