I have a php project in which I use postgresql and XAMPP.
The project itself is very small and it’s layout looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Электроэнергия</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Система учета электроэнергии</h1>
</header>
<main>
<section id="consumer-form">
<h2>Добавить потребителя</h2>
<form action="add_consumer.php" method="post">
<label for="name">ФИО / Название:</label>
<input type="text" id="name" name="name" required>
<label for="address">Адрес:</label>
<input type="text" id="address" name="address" required>
<button type="submit">Добавить</button>
</form>
</section>
<!-- Другие разделы -->
</main>
</body>
</html>
And php code looks like this:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$db = pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=kirilltop228");
if (!$db) {
die("Ошибка подключения к базе данных: " . pg_last_error());
}
// Получение данных из формы
$name = $_POST['name'];
$address = $_POST['address'];
// Запрос к базе данных для добавления потребителя
$result = pg_query_params($db, "INSERT INTO потребители (ФИО_название, адрес) VALUES ($1, $2)", array($name, $address));
// Проверка успешности запроса
if ($result) {
echo "Потребитель успешно добавлен.";
} else {
echo "Ошибка при добавлении потребителя: " . pg_last_error($db);
}
// Закрытие соединения с базой данных
pg_close($db);
?>
I am indeed connected to my DB (i checked it via var_dump()), but when i try to submit my form, it gives me 500 error.
I figured out that my problem is:
Fatal error: Uncaught Error: Call to undefined function pg_connect()
From what I can understand, i don’t have the pgsql
lib. I tried to install it and edit my php.ini file, but it didn’t work for me. What can i do?
p.s. im not sure if that is important, but i use macos