You have tagged this question with the tags php and database-database . Although I did not use the , I see in your code sample which is calling mysqli
, so I'm assuming you're using some version of MySQL. If you're wrong, please tell us.
Not enough information was provided to "guess" how you are tracking those searches. Or what their intention is with them. If you want to crawl each search individually (for example, generate a chronological log) I suggest you write this to a dedicated table and use a id
primary key with type AUTO_INCREMENT
. It may also be interesting to have one or two fields that store the timestamp of the query, so you can have chronological and not just numeric traceability.
Keeping a id
column of type AUTO_INCREMENT
in primary key is a wildcard to solve most of the relational logics you may have. It allows you to search the oldest, latest, sort, group or filter with ease. If you have the date fields as auxiliaries, you still have more granularity.
CREATE TABLE query_log (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
query_string TEXT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL
);
INSERT INTO query_log (query_string, created_at, updated_at)
VALUES("frango frito", NOW(), NOW());
If you want to store unique queries to your search system (eg you want to know that you searched for "fried chicken" but do not want an entry for each query) the same field id
with AUTO_INCREMENT
as I mentioned, just make sure the query text already exists before saving.
Using your "example":
include_once('conexao.php');
$nome = $_GET('nome'); // Isso aqui deveria ser a "frase" da consulta???
// $n = rand(0, 100000); // Não precisamos mais "gerar" o ID
$cons = "INSERT INTO query_log (query_string, created_at, updated_at) VALUES ('$nome', '2018-11-07 11:21:00', '2018-11-07 11:21:00')";
$executar = mysqli_query($conexao, $cons);
How you would like to handle dates (in the case of dealing with dates) is at your discretion. You can manually type the functions that suit you best, or if you want a library that provides this kind of help you can try Carbon . p>
The truth is that without more specific information, any response will be speculative and its accuracy will be as subjective as the question itself. There are countless ways to implement what you have written, right or wrong depends on what you want; which is exactly what is unclear.
You could generate a hash of the query, encrypt the results and make a XOR
then invert the bits and send them to a server to verify authenticity and then return to your backend. Need to do this? Most likely not . But only you can tell us.
I honestly believe that, in this case, using date literals or UID functions are overkill, unnecessary or will bring other problems in the future.
By the way, for PHP, one of the problems of PHP code shown above is the ease of a malicious person to inject code into your database and cause all sorts of problems. There are native functions to prepare your query and as far as possible avoid an injection of malicious code through your system.
I suggest you read the official MySQLi PHP documentation before continue with your project.