How to change page content according to the end inserted in the link (URL)?

1

I have a user registration system. The person inserts some information like user, photo, name, a brief personal presentation, phone, and email. An id is automatically generated for each new entry. Until now it is already running correctly.

I need it to be possible for anyone to access something like site.com/?p=joaozinho or site.com/?p=123, being 123 the id of Joaozinho, and then the page that opens to connect with the database and display the requested user information on the page, as in the following photo:

Thisisthe"code" that would represent Johnny's page.

<html>
	<body>
	  <img src="img/joaozinho.jpg">
  	  <h2>Joãozinho</h2>
      <h3>Sou um cara legal!</h3>
      <h3>9999-8888</h3>
      <h3>[email protected]</h3>
</body>
</html>
    
asked by anonymous 03.03.2017 / 06:46

3 answers

2

Jonas, you should get the url parameter by accessing the $_GET["p"] variable and use the value in your select query that will take the row with the user data. You then print these values retrieved from the database on your page.

Follow the code. You should modify in some places the connection data with your database and the name of the table and the fields of your table to see it working. In addition, your server must have the PDO extension enabled.

<?php

//CONFIG CONEXÃO com o banco de dados
// Mudar os valores abaixo pelos valores que vc usa para conectar no seu banco
$db_name = 'testdb';
$db_host= '127.0.0.1';
$db_user = 'root';
$db_password = '';  

$dsn = 'mysql:dbname='.$db_name.';host='.$db_host;

$pdo_connection = new PDO($dsn, $db_user, $db_password);
$pdo_connection->exec("SET CHARACTER SET utf8");


// Recupera o id do usuário pela variável passada na url
$user_id = $_GET["p"];

// Utiliza esse id como parâmetro na sua query
//Lembrar de mudar nomedasuatabela para o nome correto da sua tabela e id para nome do campo que é chave primária dessa tabela
$stmt = $pdo_connection->prepare("SELECT * FROM nomedasuatabela WHERE id=?");
$stmt->execute(array($user_id));

//Recupera a linha correspondente da tabela
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if($row==false)
{
    die("Não existe usuário com o id informado.");
}

// lembrar de mudar nomedocampo em cada $row['nomedocampo'] pelos nomes dos campos correspondentes que existem na sua tabela
$nome       = $row['nome'];
$descricao  = $row['descricao']; 
$telefone   = $row['telefone']; 
$email      = $row['email']; 
$urlimagem  = $row['urlimagem'];

?>

<html>
    <body>
      <img src="<?php echo $urlimagem;  ?>">
      <h2><?php echo $nome;  ?></h2>
      <h3><?php echo $descricao;  ?></h3>
      <h3><?php echo $telefone;  ?></h3>
      <h3><?php echo $email;  ?></h3>
</body>
</html>
    
03.03.2017 / 08:14
0

If the problem is to put the fields in the url, type:

Http://site/usuários.asp?id=123

And you want something like:

Http://site/usuário/123

The easiest way to resolve this is to enable the REWRITE URL in your webserver and create a rule. Give a googleada that you will find.

NOW ... if you want something like this:

Http://site/usuário/123/foto/1/apagar

The hole is deeper ...

    
03.03.2017 / 08:11
0

Look you can use it like this:

www.teste.com.br?pagina=user&id=123

In this way you put a if within the index with a $_GET the page you want to view, if it is the user page, load it with include and get the id parameter with $_GET again and pull the% User ID.

I always use this in my applications. If you do not understand how it works I'll give you a complete example.

    
03.03.2017 / 10:59