<?php
// We use the "classicmodels" database as a MySQL sample database.
// Download the SQL at https://www.phpformbuilder.pro/documentation/phpformbuildersampledatabase.sql.zip
// Original source: https://www.mysqltutorial.org/mysql-sample-database.aspx
use phpformbuilder\Form;
use phpformbuilder\Validator\Validator;
use Migliori\PowerLitePdo\Db;
$root = rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR);
/* =============================================
start session and include the autoloader
============================================= */
session_start();
include_once $root . '/phpformbuilder/autoload.php';
/* =============================================
Database connection
============================================= */
$container = require_once $root . '/phpformbuilder/vendor/migliori/power-lite-pdo/src/bootstrap.php';
$db = $container->get(Db::class);
/* =============================================
validation if posted
============================================= */
if ($_SERVER["REQUEST_METHOD"] == "POST" && Form::testToken('db-form') === true) {
// create validator & auto-validate required fields
$validator = Form::validate('db-form');
// check for errors
if ($validator->hasErrors()) {
$_SESSION['errors']['db-form'] = $validator->getAllErrors();
} else {
// Update the record
$update = [
'productlines_id' => $_POST['productlines_id'],
'code' => $_POST['code'],
'name' => $_POST['name'],
'description' => $_POST['description'],
'stock' => $_POST['stock'],
'price' => $_POST['price']
];
$where = [
'id' => $_POST['product_id']
];
if (!$db->update('products', $update, $where)) {
$msg = Form::buildAlert($db->error(), 'bs5', 'danger');
} else {
$msg = Form::buildAlert('Database updated successfully!', 'bs5', 'success');
}
}
}
/*=========================================================
If no error is posted we get the values from the database
==========================================================*/
if (!isset($_SESSION['errors']['db-form']) || empty($_SESSION['errors']['db-form'])) {
if (isset($_GET['product_id']) && is_numeric($_GET['product_id'])) {
$productId = $_GET['product_id'];
} else {
// we set a random $productId for the demo
$productId = rand(331, 440);
}
// we register the product_id to update the record later
$_SESSION['db-form']['product_id'] = $productId;
$row = $db->selectRow('products', 'productlines_id, products.code, products.name, products.description, products.stock, products.price', ['products.id' => $productId], false);
// we register the record values to populate the form values
$_SESSION['db-form']['productlines_id'] = $row->productlines_id;
$_SESSION['db-form']['code'] = $row->code;
$_SESSION['db-form']['name'] = $row->name;
$_SESSION['db-form']['description'] = $row->description;
$_SESSION['db-form']['stock'] = $row->stock;
$_SESSION['db-form']['price'] = $row->price;
}
$form = new Form('db-form', 'horizontal', 'novalidate', 'bs5');
$form->setMode('development');
$form->addInput('hidden', 'product_id');
$form->startFieldset('Update Product', '', 'class=text-center mb-4');
// get the product lines to populate the select options
$columns = array('id', 'name');
$db->select('productlines', $columns);
// loop through the results
while ($row = $db->fetch()) {
$form->addOption('productlines_id', $row->id, $row->name);
}
$form->addSelect('productlines_id', 'Product line', 'data-slimselect=true, required');
$form->addInput('text', 'code', '', 'Code', 'required');
$form->addInput('text', 'name', '', 'Name', 'required');
$form->addInput('text', 'description', '', 'Description', 'required');
$form->addInput('number', 'stock', '', 'Stock', 'required');
$form->addAddon('price', '€', 'after');
$form->addInput('number', 'price', '', 'Price', 'required');
$form->setCols(0, 12);
$form->centerContent();
$form->addBtn('submit', 'submit-btn', 1, 'Submit', 'class=btn btn-primary mt-5, data-ladda-button=true, data-style=zoom-in', 'btn-group');
$form->endFieldset();