dBoys dBoys -4 years ago 76
MySQL Question

Obtain the long Post URL from the ID in the Database Table

My site has a url

(example.com/folder/index.php?id=22)


The above URL displays the post id 22. Here's another important part:

|-------|-----------|-------------------|-------------------|
| id | user | title | url |
|-------|-----------|-------------------|-------------------|
| 22 | Admin | My Title Posts | my_title_post |
| | | | |
|-------|-----------|-------------------|-------------------|

include_once 'dbconnect.php';

$query = $con->query("SELECT * FROM posts WHERE id=".$_GET['id']);
$Row=$query->fetch_array();

echo $Row['user'];
echo $Row['title'];
echo $Row['url'];


Results:

-Admin

-My Title Posts

-my_title_post

I'm trying to hide
$_GET['id'];
By adding
.htaccess


RewriteEngine on
RewriteRule ^newfolder/(.*) folder/index.php?id=$1


My new URL is :
example.com/newfolder/22


But, this is still not quite right. I am trying to display a long post URL in the URL section, not the ID.
So I mean, how to make the URL become:

example.com/newfolder/my_title_post


I have changed the SQL to:
"SELECT * FROM posts WHERE url=".$_GET['id']
But the server can not handle the request.

I have also tried to change the Primary Key in the database url column but the request is rejected. Here I set it to Unique, but it still did not work.

Is it possible to do that?
If anyone knows, I accept suggestions via Cpanel, htaccess, SQL or something else. Thanks ..

Answer Source

Your my_title_post is a string. But your query is treated it an integer. Quote the $_GET['id'] to treat it a string.

"SELECT * FROM posts WHERE url='".$_GET['id'] . "'";

Also it is better to use parameter binding to prevent SQL injection. At least you need to use mysqli_real_escape_string to prevent it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download