PHP Classes

File: Example.UseReturnOfFormToCreateRecord.php

Recommend this page to a friend!
  Classes of Dustin Ruckman   DB Access using PDO   Example.UseReturnOfFormToCreateRecord.php   Download  
File: Example.UseReturnOfFormToCreateRecord.php
Role: Example script
Content type: text/plain
Description: Create a new user record from a form.
Class: DB Access using PDO
Query MySQL tables with prepared queries using PDO
Author: By
Last change:
Date: 9 years ago
Size: 1,390 bytes
 

Contents

Class file image Download
<?php

if (!isset($_POST['Action']))
   
ShowFormAndDie();

require(
'path/to/DatabaseAccess.Class.php');

$DbAccess = new DatabaseAccess("yourHost", "dbName", "dbUser", "dbUserPassword");

// Create a blank row of data to create an ID
$numberOfRowsUpdated = $DbAccess->SimpleNon("INSERT INTO `UserAccounts` (`FirstName`) VALUES ('')");

// Clean up anything in the $_POST that is not account data.
unset($_POST['Action']);
// Now add the new ID to the _POST array
$_POST['ID'] = $DbAccess->LastInsertID();

// Use the Post array to update the database
$numberOfRowsUpdated = $DbAccess->UpdateTableViaArrayByID("UserAccounts", "ID", $_POST);



function
ShowFormAndDie() {
   
// Note that the name of each input is the same as the Table Column names
   
?>

<form method="post" action="./">
    <input type="hidden" name="Action" value="CreateUser" />
    <label for="FirstName">First Name</label>
        <input type="text" id="FirstName" name="FirstName" value="" />
    <label for="LastName" class="col-sm-2 control-label">Last Name</label>
        <input type="text" id="LastName" name="LastName" value="" />
    <label for="EMail">Email</label>
        <input type="email" id="EMail" name="EMail" value="" />
    <label for="ZipCode">ZipCode</label>
        <input type="text" id="ZipCode" name="ZipCode" value="" />
    <input type="submit">
</form>

    <?php
   
die();
}