Help me please. I am a beginner, just learning PHP and I can’t add data (which is contained inside the form) to the database via PDO .. I tried a lot of things like that, I don’t give an error, but I don’t add them to the database .. I would be very grateful ..

here is my code:

<? require_once('db/db.php'); //Подключение к базе if(isset($_POST['add'])){ $articles = $_POST['textview']; $tags = $_POST['tags']; $article_tags = $_POST['textarea']; function createArticles($articles, $tags, $article_tags){ global $pdo; $sql = "INSERT INTO tables(articles, tags, article_tags) VALUES (:articles, :tags, :article_tags)"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':articles', $articles, PDO::PARAM_STR); $stmt->bindParam(':tags', $tags, PDO::PARAM_STR); $stmt->bindParam(':article_tags', $article_tags, PDO::PARAM_STR); $stmt->execute(); return true; $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERMODE_EXCEPTION); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Add articles</title> <link rel="stylesheet" href="bootstrap-4.2.1-dist/css/bootstrap.min.css"> <link rel="stylesheet" href="add_articles.css"> </head> <body> <div class="container"> <div class="row"> <div class="col-md-8"> <form method="POST" action="pages/blog_articles.php"> <h1>Добавление новой статьи</h1><br/><br/> <input class="theme_article" type="text" required name="textview" placeholder="Введите тему статьи"><br/><br/> <input class="date" type="date" required value="<?php echo date('dmY');?>"><br/><br/> <input type="text" name="tags" required><br/><br/> <textarea class="text" name="textarea" required cols="60" rows="13" placeholder="Введите текст статьи"></textarea><br/><br/> <input class="btn" type="submit" name="add" value="Добавить статью"> </form> </div> <div class="col-md-4"> <? include('tags/right_tags.php'); *// это просто правый контент не обращайте внимание..* ?> </div> </div> </div> </body> </html> 

And require_once('db/db.php'); just connect to the database. Nothing special

  <? $host = '127.0.0.1'; $db = 'test_db'; $user = 'root'; $pswd = ''; $dsn = "mysql:host=$host;dbname=$db;"; $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, $pswd, $opt); ?> 
  • global $pdo; do not use the global construct. Pass $pdo as a function argument. In extreme cases, you can use the singleton anti-patterns or service locator. This is not the answer to your question, the answer you wrote - ArchDemon

1 answer 1

You only define, but do not call the function. Add this piece of code inside the if block.

 createArticles($articles, $tags, $article_tags); 

after the line $ article_tags = $ _POST ['textarea'];