How to pass a variable from one page to another with PHP? - 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']

Related

How to use ? in URL?

I am trying to figure out a way to have one PHP page to display all of my blog post but have the URL decide what post is requested from that database. Something kind of like this: localhost/bolg/posts.php?pid=1 In my database I have it set up to where each post has an ID associated with it. So what I want is something that put the pid=1 and put it in the MySQL code. Here is the PHP code of the post.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, title, content, date FROM posts where id =3";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h1> ". $row["title"]. "</h1>". $row["content"]. "" . $row["date"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Assuming you enter example.com?pid=10 in the browser address bar, you can capture that variable pid using the $_GET (docs) array which PHP automatically fills for you when a page is called with a querystring.
Using your existing code as a start point you can
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
if (isset($_GET['pid'])) {
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT id, title, content, date FROM posts where id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $_GET['pid']);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// output data of each row
// while looop is not necessary, you are only returning one row
$row = $result->fetch_assoc();
echo "<h1> ". $row["title"]. "</h1>". $row["content"]. "" . $row["date"] . "<br>";
}
$conn->close();
} else {
echo "0 results";
}
Notice I took the liberty of amending your database access code to use prepared and parameterised query and binding the values to avoid SQL Injection Attack. You should always use this technique in the future

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! :)

Save id of link clicked as a session

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

background refresh information from database AJAX

I have created a website which retrieves text which has been uploaded to my database. the problem with this is i want a refresh of the content from that database ever second. but i can't find how to do this anywhere. i want this done in the background. and if possible a way to make the textbook that i will put in later to input the data into the server, also not do a full refresh but send the content in the background. thank you every answer will count.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests ORDER BY id ASC LIMIT 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div id='message'> <br> ". $row["firstname"]. " " . $row["lastname"] . "<br> </div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
In a file, simply echo out the results. Then in another file, load the echoed results in a div like this:
db_results.php file which will echo out results:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests ORDER BY id ASC LIMIT 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<p>". $row["firstname"]. " " . $row["lastname"] . "</p>";
}
} else {
echo "0 results";
}
$conn->close();
?>
display.php file which will refresh the results in a div:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
setInterval(function() {
$("#load_results").load("db_results.php");
}, 1000);
});
</script>
</head>
<body>
<div id = "load_results"></div>
</body>
</html>
db_results.php file will query database, will fetch the results and will echo them out. The second file (display.php) loads the first file in a div and refreshes the div every second, so updated results are loaded in the concerned div.
P.S.: Keep both files in same directory or adjust the path accordingly.

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

Categories