I've created forms in php and want to update the data in the sql server database.
Following is my ad.php file.
<html>
<title>FORM</title>
<body><br><br>
<form action ="db_connection.php" method="post">
Activity:<input type="text" name="name"><br><br>
Units: <input id="number"><br><br>
<input type="submit" value="submit">
</form>
The data inserted in the above form should be updated in the table 'ad' of the database.
Following is my db_connection.php file:
<?php
class DB{
protected static $db;
const SERVER = "";
const SCHEMA = "";
const USER = "";
const PASS = "";
/*
* Sets up the database connection
* @return the database instance
*/
public static function databaseConnection(){
if(!self::$db){
try {
$databse = "sqlsrv:server=" . self::SERVER . ";Database=" . self::SCHEMA;
self::$db = new PDO($databse, DB::USER, DB::PASS);
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch(PDOException $e){
die("Error " . $e->getMessage());
}
}
return self::$db;
}
}
class ad{
public function dbAdmin(vat $Activity, int $Units): bool {
$conn = databaseConnection::getConnection();
$conn->beginTransaction();
$sql = "INSERT INTO ad('Activity', 'Units') VALUES (:Activity, :Units)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':Units', $Units);
$stmt->bindParam(':Activity', $Activity);
if ($stmt->execute()) {
$conn->commit();
return true;
} else {
$conn->rollback();
return false;
}
}
}
I'm getting a blank page after submitting the form and the database is not updating with the new data being submitted. Can you please check the code and help me with the corrections.
I had to reproduce this using Docker. The container "microsoft/mssql-server-linux" is being used.
I am using this extension **pdo_sqlsrv** and this driver https://breeto.id.au/2017/05/docker-alpine-linux-php-sql-server/
index.html
<!DOCTYPE>
<html>
<head>
<title>FORM</title>
</head>
<body>
<br/>
<br/>
<form action="index.php" method="post">
<label for="activity">Activity: </label>
<input id="activity" type="text" name="activity">
<br/>
<br/>
<label for="number">Units: </label>
<input id="number" type="number" name="units">
<br/>
<br/>
<input type="submit" value="submit">
</form>
</body>
</html>
index.php
<?php
declare(strict_types=1);
require_once __DIR__ . '/Db.php';
require_once __DIR__ . '/Ad.php';
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
ini_set('log_errors', '1');
$post = ['activity', 'units'];
if (!isset($_POST['activity'], $_POST['units'])) {
header('Invalid form', TRUE, 422);
exit;
}
$activity = filter_input(INPUT_POST, 'activity', FILTER_SANITIZE_SPECIAL_CHARS);
$units = (int) filter_input(INPUT_POST, 'units', FILTER_SANITIZE_NUMBER_INT);
if (!is_int($units)) {
header('A number must be given', TRUE, 422);
exit;
}
$adTable = new Ad();
$adTable->dbAdmin($activity, $units);
echo '<h1>Done</h1>';
echo "<p>Activity: {$activity}</p>";
echo "<p>Units: {$units}</p>";
Db.php
<?php
declare(strict_types=1);
/**
* Class Db
*/
class Db {
protected static $db;
public const SERVER = 'mssql';
public const SCHEMA = 'stackoverflow';
public const USER = 'sa';
public const PASS = '';
/**
* Sets up the database connection
*
* @return \PDO
*/
public static function databaseConnection(): \PDO {
if (!self::$db) {
try {
$dsn = 'odbc:Driver={FreeTDS};Server=' . self::SERVER . ';Database=' . self::SCHEMA . ';Port=1433';
self::$db = new PDO($dsn, DB::USER, DB::PASS);
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die('Error ' . $e->getMessage());
}
}
return self::$db;
}
}
Ad.php
<?php
declare(strict_types=1);
/**
* Class Ad
*/
class Ad {
public function dbAdmin(string $activity, int $units): bool {
$conn = Db::databaseConnection();
$conn->beginTransaction();
$sql = /** @lang TSQL */
'INSERT INTO stackoverflow.dbo.ad (Activity, Units) VALUES (:Activity, :Units)';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':Activity', $activity, PDO::PARAM_STR);
$stmt->bindParam(':Units', $units, PDO::PARAM_INT);
if ($stmt->execute()) {
$conn->commit();
return TRUE;
}
$conn->rollBack();
return FALSE;
}
}
SQL Server (Microsoft) Table
CREATE TABLE ad
(
id INT IDENTITY
PRIMARY KEY,
Activity VARCHAR(255),
Units INT
)
GO