Recording variables in the database and how to retrieve them and assign value

Asked

Viewed 1,709 times

0

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

Format Saved to Database

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

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

I need to fill out this $name field. How to write this reference in the database then retrieve them to correctly assign in the $contract variable with the completed person’s name coming from a BD query ?

2 answers

3


Since you didn’t mention where the variables come from, I’ll give you a general example of a system of templates:

Information from the DB:

<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 it pays to use a marking that does not confuse, as $nome$ instead of $nome, to avoid ambiguity if there is something like $nomenclatura (that begins with $nome also).

If you already have a lot in DB, at least be careful, with words started in the same way, change the longer ones first in the sequence of places (change the $nomenclatura before $nome, other than the $nome will touch something that should not).

See the syntax of str_replace using array to make several replacements 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 may also help:
How to create a function to scroll through a dynamically created PHP page and change certain text

  • That’s right. Thank you very much!

-1

To enter data into the database create a form with the necessary inputs. In the action of this form point to a file called 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 in your comic book.

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

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 helped. For more information on INSERT and the SELECT, please consult:

http://www.w3schools.com/php/php_mysql_insert.asp http://www.w3schools.com/php/php_mysql_select.asp

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

  • Thank you. I don’t think I explained it correctly. The Value of the $name variable will not be recorded in the database. The Contract Template will be saved. The data to fill out this contract will come from another table. So I need to know a way to record these references of the variables of this contract in the model and then assign the corresponding value that will come from the database. Did you give endernder? recorded in the database: <html>.... Name: $name ...</html> ai fill this $name with a value that will come from the dataset, from the client table.

Browser other questions tagged

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