/** * Main method * * */ public function __construct() { // Validates the form input submitted, before writing to database if ($_POST) { $firstName = filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_STRING); $lastName = filter_input(INPUT_POST, 'lastname', FILTER_SANITIZE_STRING); $mail = filter_input(INPUT_POST, 'mail', FILTER_VALIDATE_EMAIL); // Performing validation if (empty($firstName) || empty($lastName)) { $error = 'Enter your name and last name'; } else { if (isset($_POST['mail']) && !empty($_POST['mail']) && $mail == FALSE) { $error = 'Enter a valid email address'; } } // Displays a message in case of errors if (isset($error)) { self::$message = '<i style="color: #f50;">ERROR: ' . $error . '</i><br /><br />'; } else { // SQL insertion in JSON format $json = ' insert : [ { table: "users" , values: { firstname: "' . $firstName . '", lastname: "' . $lastName . '", mail: "' . $mail . '" } } ] '; // Performs the new record and store the result list($total) = PDO4You::execute($json); // Displays a success message self::$message = 'Register #' . $total . ' added successfully!!<br /><br />'; } } // Capture records of all registered users self::$hasRecords = PDO4You::select('SELECT * FROM users ORDER BY id DESC'); }
<?php // Loading demo class require 'DemoRegister.php'; // Example with Read Records echo '<h2><a href=".">DEMOS</a> › READING RECORDS</h2>'; // Creating an instance $demo = new DemoRegister(); ?> <div class="pdo4you"> <form method="post"> <h3><?php echo $demo::ADD_NEW_USER; ?> </h3> <div><?php echo $demo::FIRST_NAME; ?> : <input type="text" name="firstname" /></div> <div><?php echo $demo::LAST_NAME; ?> : <input type="text" name="lastname" /></div> <div><?php echo $demo::MAIL; ?> : <input type="text" name="mail" /></div> <div><input type="submit" value="Register" /></div> </form>