Writing variables to the database and retrieving them and assigning value

0

I need to write a contract in HTML format to the database. This contract will have the personal data of the people. This personal data will come from a database query. I need to populate this data dynamically.

Format Saved to Database

<html>
....
Nome: $nome
.....
</html>

<?php
$contrato = $contratobanco;
echo $contrato;
?>

I need to populate this $ name field. How to write this reference in the database then retrieves them to correctly assign the $ contract variable with the name of the filled person coming from a DB query?

    
asked by anonymous 06.03.2016 / 20:56

2 answers

3

As you have not mentioned where the variables come from, I'll give you a general example of a templates system:

DB Information:

<html>
    Nome: $nome$
</html>

PHP

<?php
    $contrato = /* aqui você pega do DB os dados */;
    $nome = 'José Maria'.
    $idade = '17';

    $contrato = str_replace( '$nome$', $nome, $contrato ); 
    $contrato = str_replace( '$idade$', $idade , $contrato ); 
    ... faça o mesmo para todos os campos ...

    echo $contrato;
?>

Note that in DB you can use a markup that does not confuse, such as $nome$ instead of $nome , in order to be unambiguous if there is something like $nomenclatura (which starts with $nome too).

If you already have a lot in the DB, you should at least be careful, with words started in the same way, to exchange the longest ones first in the sequence of replaces (change $nomenclatura before $nome , otherwise the $nome will move in something that should not).

View the syntax of str_replace using array to make several substitutions at once:

    $contrato = str_replace(
       array( '$nome$', '$idade$', '$endereco$' ), // Palavras a trocar
       array(  $nome  ,  $idade  , $endereco    ), // O que vai por no lugar de cada uma
       $contrato
    )

The following question can also help:
How to create a function to page dynamically created PHP page and change certain text

    
06.03.2016 / 21:52
-1

To enter data into the database create a form with the required inputs. In the action of this form point to a file named insere.php .

formulario.html

<form action="insere.php" method="post">
Nome:<br>
<input type="text" name="nome"><br>
Contrato:<br>
<input type="text" name="contrato">
</form>

insere.php

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

//PEGAR OS VALORES DO FORMULARIO
$nome=$_REQUEST['nome']; /*Aqui o nome é o 'name="nome"' que vem do   formulario via POST.*/
$contrato=$_REQUEST['contrato'];//A mesma coisa

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO info (nome, contrato)
VALUES ('$nome', '$contrato')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Ready has been added to your bd.

Now let's create the mostra.php where it will display the data that exists in your info table.

mostra.php

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT nome, contrato FROM info";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//MOSTRA O RESULTADO
    echo "nome: " . $row["nome"]. " - Contrato: " . $row["contrato"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

Okay, I hope this has helped. For more information on INSERT and SELECT , please see:

link link

* Configure the user / password / database name, as well as the name of your table

    
06.03.2016 / 21:16