mardi 23 juillet 2019

How to make adding records to a table from html fields?

There is a php pdo code that adds static data to the table. How to declare variables that will add records from html fields to a table. For example, without the declared variables $name and $description, the page becomes unavailable

<?php 
$host = 'localhost';
    $db   = 'ch12997_ti';
    $user = 'ch12997_ti';
    $pass = 'nikitaa';
    $charset = 'utf8';

    $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
    $opt = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    $pdo = new PDO($dsn, $user, $pass, $opt);


$stmt = $pdo->prepare("INSERT INTO posts (name, description) VALUES (:name, :description)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':description', $description);
$name = '';
$description = '';
$stmt->execute();

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>


        <h2>Добавить новую модель</h2>
<form method="POST">
<p>Введите модель:<br> 
<input type="text" name="name" /></p>
<p>Производитель: <br> 
<input type="text" name="description" /></p>
<input type="submit" value="Добавить">
</form>

</body>
</html>




Aucun commentaire:

Enregistrer un commentaire