I have the following code where I get data from the Urls
if (isset($_GET['id'])){
$url = $_GET['id'];
}
I check the value that the variable is giving me $url
as a result:
book-php/
Currently the data is obtained in such a way because I have the file .htaccess
in friendly Urls .
The data is correct at the time of making the query:
$stmtID = $con->prepare("SELECT idlibro,libro,titulo,precio FROM biblioteca WHERE url=?");
Now my question:
What value does he give id
to $_GET
?
If I change this way the code does not work, all the variables of the file lose their validation for the reason that they no longer receive the ideal result for the query.
if (isset($_GET['url'])){
$url = $_GET['url'];
}
What does the in have to do id
with it $_GET
?
I don't have any product or data in the database that has a column ,
id
all IDS are defined like thisid_producto
,id_usuario
etc.
Why giving it another name does not work the same?
Update
.htaccess file
#Regla Url amigable
RewriteRule ^([a-zA-Z0-9-/]+)$ detalle.php?id=$1
As the PHP Manual says :
For example in a URL like this:
The value of
$_GET
will be an array like this:If you want to get the values of
GET
:to solve your problem
Seeing that your URL is constructed like this:
It would suffice to change the value in the rule
id
tourl
If, on the contrary, the value
url
were taken from another element, or if it was equivalent to some file through which you build friendly URLs, then you would have to modify the name of the element (for example an HTML element) or the name of the file if it were a File, Archive.Other things about
GET
I suppose you are going to use
GET
orPOST
more than once, so I allow myself to leave some indications...When you submit HTML forms, GET will associate the values using the value of the elements tag
name
as the key.Example:
If you want to match the keys that you will get via GET with the column names of your database, you only have to change the value of the tag
name
if you use an HTML form, or the values of the URL.Example:
HTML form
When you submit this form, what is actually produced is a URL like this:
So, with
GET
we get an array with three keys and their three respective values, each pair (key->value) is separated by&
in the URL:id_usuario => 1
id_producto => 24890
url => example.com/registrar
file.php or file that receives the data from the previous form
In the PHP file you would retrieve each value like this:
Notes from the PHP Manual and from experience
Some indications regarding the use of
GET
(which are also valid forPOST
):name
HTML elements tag, not theid
.It is often used
isset
to verify that the searched value exists in the URL (if it exists among the posted data). For example, to find out if itid_usuario
was posted:This comparison can be refined, looking not only if it exists, but also if it is not blank.
This is a 'superglobal' or an automatic global variable. It simply means that it is a variable that is available anywhere in the script. It is not necessary to do
global $variable;
to access it from functions or methods.GET variables are passed via
urldecode()
.$_POST
works the same way and is sometimes recommended overGET
, but that would be another matter.Never directly send fetched values with
GET
or withPOST
or any other values coming from outside to database queries . Doing so would make the code vulnerable to SQL Injection, through which malicious users could take control not only of the database, but of your entire system. Those values must be sent using prepared queries. There are several questions here on SO dealing with this topic.Short answer:
Change your
.htaccess
to the following:Long Answer:
The variables
$_GET
in general terms are those that are sent by the URL I would explain this but A.Cedano already took the trouble.When you make "friendly URLs" via
.htaccess
orvirtualhost
in the same way you send parameters viaGET
as you can see in your.htaccess
you are overriding the url and internally sending the id :
If you want me to change this and receive
url
in your code you should change yourvirtualhost
or.htaccess
to the following:What happens is that what you add in
$_GET['nombre']
is the name of the variable that is being sent from the Front-End to the Back-End, in some of your files that variable is being sent with the name ofid
and that is how you must receive it, if you want to change the name you should then change it at the time of sending and receive itphp
with the new nameI hope I have been clear in the explanation, in the same way I leave you this Link with more information about the GET variables