JuliusSecret JuliusSecret - 5 months ago 18
SQL Question

PHP/MySQL - how to add MySQL data to url in address bar without a form

I have a website at

localhost:8888/documents/index.php
.

In this file, I load a random row from my MySQL table. I have around 1000 rows, and it will randomly pick one row, and return some data from that row into my page. For example, if it chooses row 467 with:

name = George
age = 23
key = fe4v6


It will show on my
index.php
something like:
hello, George (23 years old)
. The key is a random, unique variable for each row.

What I want: I want my URL in the address bar to update automatically on refresh with the key attribute. So I want:
localhost:8888/documents/index.php
?key=fe4v6
. When I refresh the page, I want it to update the URL with the new key value for the corresponding row in my MySQL database.

I don't work with forms. I make a connection with my database and I use queries and
fetch_assoc()
on the random row. In my
index.php
I show the name like this:

<?php echo $row['name'] ?>


I don't know how easy or difficult this is, maybe there is a much easier solution. I would like to hear from you, thanks!

Answer

A quick solution would be to use a header redirect

if(!isset($_GET['UniqueKey'] {
    header('location: localhost:8888/documents/index.php?UniqueKey='.$row['name']);
}

The code firsts checks if UniqueKey is set, so it doesn't redirect infinitely.