PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of William Lang   Auto Insert   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Instructions and usage
Class: Auto Insert
Add or change MySQL records based on form values
Author: By
Last change: update
Date: 16 years ago
Size: 5,107 bytes
 

Contents

Class file image Download
<?
   
include('autoInsert.php');
   
$a = new autoInsert();
   
   
$a->iConnect('root', 'password', 'localhost');
   
$a->iSelect('database');
   
    if (isset(
$_POST['insert'])){
       
$a->iInsert('testTable', $_POST);
    }
   
    if (isset(
$_POST['update'])){
       
$conditions = array('field_id' => 1);
       
$a->iUpdate('testTable', $_POST, $conditions);
    }
   
   
$a->iDisconnect();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AutoInsert</title>
<style type="text/css">
    .code{
        border:1px solid #000000;
        padding:20px;
        font-size:11px;
        background:#E0E0E0;
    }
</style>
</head>
<body>
<h1>AutoInsert</h1>

<p>This class is very simple. It depends primarily on how you name the fields in your form and in your MySQL tables. This class can be <em>easily</em> modified to work with all sorts of different database engines simply by changing the functions used.</p>
<p>To use the class your field names must be the same as the text fields in your form. It can even be used to update your tables as well. </p>
<h2>Testing</h2>
<p>Run the following query in your favourite database administration tool. This will set up a temporary table for us to muck around with. In the code for this page change the values to connect to your MySQL server and database.</p>
<div class="code">
CREATE TABLE IF NOT EXISTS `testtable` (<br />
`field_id` int(11) NOT NULL auto_increment,<br />
`field_one` varchar(255) NOT NULL,<br />
`field_two` varchar(255) NOT NULL,<br />
`field_three` varchar(255) NOT NULL,<br />
`field_four` varchar(255) NOT NULL,<br />
KEY `field_id` (`field_id`)<br />
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
</div>
<p>So now that we have our table we have our two forms below. The one on the <em>left</em> will perform our insert. The one on the <em>right</em> will perform our update. For simplicity's sake I've seperated the two forms with corresponding hidden fields called 'insert' and 'update' to differentiate which function to run. Go ahead and enter data for the <em>left</em> form and press 'Submit'. It will add a row with field_id = 1.</p>
<p>Go check out your table to see what's in there. Should've inserted some stuff for you. I added basic security to input with the htmlspecialchars() function. Now to update simply enter data in the <em>right</em> form. It will update the first record. Now let's see how we did all that.</p>
<p>Inserting data is fairly simple.</p>

<div class="code">$a-&gt;iInsert('testTable', $_POST);</div>
<p>This calls the function iInsert. The first argument is the name of the table to insert into. The second, obviously, is the post data. No matter what post data you send, if there is no field in the table that does not match the post data, it will be ignored.</p>
<p>Updating is just as simple, but with on change. We pass a conditions array so the function knows what rows to update.</p>
<div class="code">
$conditions = array('field_id' =&gt; 1); <br />
$a-&gt;iUpdate('testTable', $_POST, $conditions);</div>
<p>The conditions array works as follows: array[FIELD] = VALUE.</p>
<table border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td>
    <form method="post" action="index.php">
     <table border="0" cellspacing="1" cellpadding="1">
      <tr>
        <td>Field One</td>
        <td><input type="text" name="field_one" id="field_one" /></td>
      </tr>
      <tr>
        <td>Field Two</td>
        <td><input type="text" name="field_two" id="field_two" /></td>
      </tr>
      <tr>
        <td>Field Three</td>
        <td><input type="text" name="field_three" id="field_three" /></td>
      </tr>
      <tr>
        <td>Field Four</td>
        <td><input type="text" name="field_four" id="field_four" /></td>
      </tr>
      <tr>
        <td><input type="hidden" name="insert" value="1" /></td>
        <td style="text-align:right;"><input type="submit" name="Submit" id="Submit" value="Submit" /></td>
      </tr>
     </table>
    </form>
    </td>
    <td>
    <form method="post" action="index.php">
     <table border="0" cellspacing="1" cellpadding="1">
      <tr>
        <td>Field One</td>
        <td><input type="text" name="field_one" id="field_one" /></td>
      </tr>
      <tr>
        <td>Field Two</td>
        <td><input type="text" name="field_two" id="field_two" /></td>
      </tr>
      <tr>
        <td>Field Three</td>
        <td><input type="text" name="field_three" id="field_three" /></td>
      </tr>
      <tr>
        <td>Field Four</td>
        <td><input type="text" name="field_four" id="field_four" /></td>
      </tr>
      <tr>
        <td><input type="hidden" name="update" value="1" /></td>
        <td style="text-align:right;"><input type="submit" name="Submit2" id="Submit2" value="Submit" /></td>
      </tr>
     </table>
     </form>
    </td>
  </tr>
</table>
</body>
</html>