PHP Classes

File: Example_validating.php

Recommend this page to a friend!
  Classes of Edoardo   PHP Form To DB   Example_validating.php   Download  
File: Example_validating.php
Role: Example script
Content type: text/plain
Description: Example2
Class: PHP Form To DB
Validate form submission and save to MySQL records
Author: By
Last change: An in-depth example
Date: 11 years ago
Size: 3,325 bytes
 

Contents

Class file image Download
<?php


include_once('FormToDb.class.php');



/*
=====================
UPDATE DATA
=====================
*/
if( isset($_POST['submit']) )
{
   
//EXAMPLE: The inputs that are coming up from the form are: $_POST['NAME'], $_POST['USERNAME'], $_POST['EMAIL'], $_POST['PASSWORD']
    //NOTE: The names of the fields in the table must have the same names as the array keys POST
   
   
    //Instantiate new connection
   
$object = new FormToDb(HOST, USERNAME, PASSWORD, DB_NAME, TABLE_NAME);
   
   
   
//Create a list of validating methods -> In the same order as the input
   
$object->VMethods = array('isName', //for $_POST['NAME']
                             
'isName', //for $_POST['USERNAME']
                             
'isEmail', //for $_POST['EMAIL']
                             
'isPasswd'); //for $_POST['PASSWORD']
   
    //NOTE: if you do not specify validation methods, the class uses the method isMessage for all inputs
   
    //Add fields to ignore
   
$object->toIgnore = array('SUBMIT'); //in this case ignore only the SUBMIT button
   
    //Try update data
   
if(!$object->getFromPostV($_POST, TRUE)) //set true as second parameter to stop operation and show the first error
   
{
       
//If there was an error, get all the invalid fields or errors
       
$invFields = $object->getInvalidFields();
    }
    else
    {
       
//else proceed to update
        //WHERE col name is NAME and value is VALUE
       
$object->update(COLNAME, VALUE);
    }
   
   
//If you obtained any errors operations
   
$errors = $object->getErrors(); //return an array
   
    //THATS ALL FOR UPDATING METHOD
}



/*
=====================
SAVE NEW DATA
=====================
*/
if( isset($_POST['submit']) )
{

   
//EXAMPLE: The inputs that are coming up from the form are: $_POST['NAME'], $_POST['USERNAME'], $_POST['EMAIL'], $_POST['PASSWORD']
    //NOTE: The names of the fields in the table must have the same names as the array keys POST
   
    //Instantiate new connection
   
$object = new FormToDb(HOST, USERNAME, PASSWORD, DB_NAME, TABLE_NAME);
   
   
   
//Create a list of validating methods -> In the same order as the input
   
$object->VMethods = array('isName', //for $_POST['NAME']
                             
'isName', //for $_POST['USERNAME']
                             
'isEmail',//for $_POST['EMAIL']
                             
'isPasswd');//for $_POST['PASSWORD']
                             
    //NOTE: if you do not specify validation methods, the class uses the method isMessage for all inputs
   
    //Add fields to ignore
   
$object->toIgnore = array('SUBMIT'); //in this case ignore only the SUBMIT button
   
    //Try save the data
   
if(!$object->getFromPostV($_POST, TRUE)) //set true as second parameter to stop operation and show the first error
   
{
       
//If there was an error, get all the invalid fields or errors
       
$invFields = $object->getInvalidFields();
    }
    else
    {
       
//else proceed to save
       
$object->save();
    }
   
   
//If you obtained any errors operations
   
$errors = $object->getErrors(); //return an array
   
    //THATS ALL FOR SAVING METHOD
   
}
?>


<html>

<head>
</head>
<body>

<div style="margin:0 auto;">
    <form method="post" target="index.php">
        <input type="text" name="name" value="">
        <input type="text" name="surname" value="">
        <input type="text" name="email" value="">
        <input type="text" name="password" value="">
        <input type="submit" name="submit" value="submit">
    </form>
</div>


</body>