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

Asked

Viewed 815 times

1

I have a user registration system. The person inserts some information such as user, photo, name, a brief personal presentation, phone, and email. Automatically an id is generated for each new registration. So far it’s already running correctly.

Do I need anyone to be able to access something like a website.com/? p=Jobby or website.com/? p=123, being 123 the id of Johnny, and then the page that opens connect to the database and show the requested user information on the page, as in the following picture:

Exemplo Joãozinho

This is the "code" that would represent the page of Johnny.

<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>

3 answers

2


Jonas, you must take the parameter of the url by accessing the variable $_GET["p"] and use the value in your select query that will pick up the line with the user data. Next you print these recovered bank values on your page.

Just follow the code. You should modify in some places the connection data with your database and the table name and fields of your table to see how it works. 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>
  • I used this code and everything worked out, thank you very much!!!

  • Ah that good! Valew! If you want to give a moral, mark as accepted answer and click the arrow up to give me reputation points. :)

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 solve it is by enabling the URL REWRITE on your webserver and create a rule. Give a googleada q vc will find.

NOW ... if you want something like:

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

The hole is deeper...

0

Look you can use it like this:

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

This way you put one if inside the index with a $_GET the page you want to view, if it’s the user page, click it with include and take the id parameter with $_GET again and pull the user id.

I always use it like this in my applications. If you don’t understand how it works I’ll give you a full example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.