How $_GET Variable Works and How to Use It in PHP/MySQL PHP Script


How $_GET Variable Works and How to Use It in PHP/MySQL PHP Script

This tutorial will show you how does $_GET method works and how to use it. $_GET is a kind of method in getting a particular value in the webpage’s url. This will be possible by sending this particular variable in the url of the “go to” page. In this tutorial, I will show you how to send a value in your url and getting that particular value via $_GET method.
Creating our Database

First, we’re going to create a database that contains the user data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as “get”.
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

 

 

CREATE TABLE `user` (
`userid` INT(11) NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(30) NOT NULL,
`lastname` VARCHAR(30) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

 

Inserting Data into our Database

Next, we insert data into our database. These data will serve as our reference.
1. Click our database “get”.
2. Click SQL and paste the below code.

 

INSERT INTO `user` (`firstname`, `lastname`) VALUES
( 'neovic', 'devierte'),
( 'lee', 'ann');

 

Creating our Connection

Next step is to create a database connection and save it as “conn.php”. This file will serve as our bridge between our page and our database. To create the file, open your HTML code editor and paste the code below after the tag.

 

<?php
$conn = mysqli_connect("localhost","root","","get");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

 

Creating our Reference Table

Next step is to create our reference table and name it as “index.php”. This table contains the data that we inserted in our database earlier. To create the table, open your HTML code editor and paste the code below after the tag.

 

<!DOCTYPE html>
<html>
<head>
<title>$_GET tutorial</title>
</head>
<body>
<table border="1">
<thead>
<th>Firstname</th>
<th>Lastname</th>
<th></th>
</thead>
<tbody>
<h2>User Table</h2>
<?php
include('conn.php');
$query=mysqli_query($conn,"select * from `user`");
while($row=mysqli_fetch_array($query)){
?>
<tr>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><a href="goto.php?id=<?php echo $row['userid']; ?>">View</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>

 

Creating our Go to Page

Lastly, we create our go to page and name it as “goto.php”. This page will received the value that we are sending and get that value using the $_GET method. To create the page, open your HTML code editor and paste the code below after the tag.

 

<!DOCTYPE html>
<html>
<head>
<title>$_GET tutorial</title>
</head>
<body>
<?php
include('conn.php');
$id=$_GET['id'];
$query=mysqli_query($conn,"select * from `user` where userid='$id'");
$row=mysqli_fetch_array($query);
?>
<h2></h2>
You Click: <?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?>
</body>
</html>

 

 


Related Post


HTML5 Image Video Audio Gallery with Playlist PHP ... HTML5 Image Video Audio Gallery with PlaylistHTML5 Image Video Audio Gallery with Playlist HTML5 Media Player supports mixing of audio, images and ...
eBus – Online Bus Reservation & Ticket ... eBus - Online Bus Reservation & Ticket Booking SystemeBus - Online Bus Reservation & Ticket Booking System Online Bus Reservation System is...
Simple Ordering APP using PHP/MySQL PHP Script Simple Ordering APP using PHP/MySQL PHP Script Basic CRUD - (create, read, update and delete) -> Products and Category Menu View - using increm...
Karenderia Order Taking App v1.0.4 PHP Script Down... Karenderia Order Taking App v1.0.4Karenderia Order Taking App v1.0.4 Karenderia Order Taking App is merchant order receiving application for Karend...
Update Textbox Based on ComboBox Using PHP and Jav... Update Textbox Based on ComboBox Using PHP and JavaScript PHP Script This code will teach you on how to add combo box data to textbox without loadi...

Read Me

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *