?php /* PSWAB (Php Sqlite Web Address Book) v0.1 * 03 June 2010 * WoodLark * based on "Simple PHP web based address book using MySql" by Alpho011 * http://hubpages.com/hub/Simple-PHP-web-based-address-book-using-MySql * but written using SQLite instead of MySql an with a few added features. */ ini_set('display_errors',1); error_reporting(E_ALL); ob_start();//this just buffers the header so that you dont receive an error for returning to the same page if(isset($_GET['id']) && $_GET['mode'] == 'edit'){ //lets get the details for the page title $title = "We are editing: " . $_GET['name'] . " are you sure!!!"; } else{ $title = 'Address Book'; } /* The follwing two lines control access to the database. The "pswabadmin" user (defined by the variable $uname) is * permitted to add, edit and delete database entries. Other users may view, but not change, entries. To implement this feature, * the hosting service must allow you to password protect the directory in which PSWAB is installed. You can set up as many users * as you want with access to the protected directory provided that one of the users is named "pswabadmin". * * If you are running on localhost on a home computer, you are probably safe to leave PSWAB in the "testing" mode. */ //$uname = $_SERVER['REMOTE_USER']; $uname = "pswabadmin"; // for testing only, uncomment this line and comment out the previous one. ?> <?php echo $title; ?> <?php //set path to database (NOTE: database must be in the same directory as index.php, or you need to change the next line). // also note, that the webserver and php must have read and write permissions in the directory. $db = 'pswab.db'; // Define the database structure $tbl = 'tblAddress'; $fld1='id integer PRIMARY KEY'; $fld2='lname text NOT NULL'; $fld3='fname text NOT NULL'; $fld4='adrs1 text'; $fld5='adrs2 text'; $fld6='city text'; $fld7='state text'; $fld8='zip text'; $fld9='phone text'; $fld10='cell text'; $fld11='email text'; $fldlist=$fld1 . ', ' . $fld2 . ', ' . $fld3 . ', ' . $fld4 . ', ' . $fld5 . ', ' . $fld6 . ', ' . $fld7 . ', ' . $fld8 . ', ' . $fld9 . ', ' . $fld10 . ', ' . $fld11; $createquery = 'CREATE TABLE ' . $tbl . ' (' . $fldlist . ' )'; // Connects to your Database and creates it if it doesn't already exist $dbhandle = sqlite_open($db); if ($dbhandle) { $query = sqlite_query($dbhandle, "SELECT name FROM sqlite_master WHERE type='table' and name='tblAddress'"); $totaltables = sqlite_num_rows($query); if ($totaltables<1){ $createresult=sqlite_query($dbhandle,$createquery); if (!$createresult) { die("Error in query: ".sqlite_error_string(sqlite_last_error($dbhandle))); } } } else { die ($sqliteerror); } //we will use a case switch to look for the variable to make the decisions on what to show //this is the variable that will control the switch case //begin the switch if (isset($_GET['mode'])) { $mode = $_GET['mode']; } else { $mode=''; } switch($mode){ //add a record case 'add': if ($uname != "pswabadmin"){ header('Location:' . $_SERVER['HTTP_REFERER']); break; } ?>

Add Contact

Last Name:
First Name:
Address 1:
Address 2:
City:
State:
Zip:
- -
Phone:
- -
Cell:
- -
Email:
Back |
<?php break; //added a record case 'added': //first setup the vars $lname = $_POST['lname']; $fname = $_POST['fname']; $adrs1 = $_POST['adrs1']; $adrs2 = $_POST['adrs2']; $city = $_POST['city']; $state = strtoupper($_POST['state']); $zip = $_POST['zip1'] . "-" . $_POST['zip2']; $phone = $_POST['ph1'] . "-" . $_POST['ph2'] . "-" . $_POST['ph3']; $cell = $_POST['ce1'] . "-" . $_POST['ce2'] . "-" . $_POST['ce3']; $email = $_POST['email']; //then lets use'em $sql = "INSERT INTO tblAddress (lname, fname,adrs1,adrs2,city,state,zip, phone, cell, email) "; $sql = $sql . "VALUES ('" . $lname . "','" . $fname . "','" . $adrs1 . "','" . $adrs2 . "','" . $city . "','" . $state . "','" . $zip . "','" . $phone . "','" . $cell . "','" . $email . "')"; //echo $sql; //return; sqlite_query($dbhandle,$sql) or die("Error in query: ".sqlite_error_string(sqlite_last_error($dbhandle))); //done take me back to the main page header('location: ' . $_SERVER['PHP_SELF']); break; //edit a record case 'edit': if ($uname != "pswabadmin"){ header('Location:' . $_SERVER['HTTP_REFERER']); break; } ?>

Editing: <?php echo $_GET['lname'].", ".$_GET['fname'];?>

<?php $zipi = $_GET['zi']; ?> <?php $ph = $_GET['phone']; ?> <?php $ph = $_GET['cell']; ?>
Last Name:
First Name:
Address 1:
Address 2:
City:
State:
Zip:
-
Phone:
- -
Cell:
- -
Email:
Back |
<?php break; //record has been edited case 'edited': //again clarify the vars $lname = $_POST['lname']; $fname = $_POST['fname']; $adrs1 = $_POST['adrs1']; $adrs2 = $_POST['adrs2']; $city = $_POST['city']; $state = strtoupper($_POST['state']); $zip = $_POST['zip1'] . "-" . $_POST['zip2']; $phone = $_POST['ph1'] . "-" . $_POST['ph2'] . "-" . $_POST['ph3']; $cell = $_POST['ce1'] . "-" . $_POST['ce2'] . "-" . $_POST['ce3']; $email = $_POST['email']; $id = $_POST['id']; //do the query $sql = "UPDATE tblAddress SET lname = '" . $lname ."', fname = '" . $fname ."', adrs1 = '" . $adrs1 ."' , adrs2 = '" . $adrs2 ."' , city = '" . $city ."' , state = '" . $state ."' , zip = '" . $zip ."' phone = '" . $phone . "', cell = '" . $cell . "', email = '" . $email . "', WHERE id = '" . $id . "'"; sqlite_query($dbhandle,$sql) or die("Error in query: ".sqlite_error_string(sqlite_last_error($dbhandle))); //echo $sql; //return; //below you can either redirect show a message or put a link, and if you think harder you can probably do alot more //done take me back to the main page header('location: ' . $_SERVER['PHP_SELF']); break; //remove a record case 'remove': if ($uname != "pswabadmin"){ header('Location:' . $_SERVER['HTTP_REFERER']); break; } $id = $_GET['id']; //lets remove the record this one is easy $sql ="delete from tblAddress where id= '" . $id ."'"; //run the query sqlite_query($dbhandle,$sql) or die("Error in query: ".sqlite_error_string(sqlite_last_error($dbhandle))); //echo $sql; //return; //done take me back to the main page header('location: ' . $_SERVER['PHP_SELF']); break; case 'detail': ?>

Detail for: <?php echo $_GET['lname'].", ".$_GET['fname'];?>

Last Name:
First Name:
Address 1:
Address 2:
City:
State:
Zip:
Phone:
Cell:
Email:
Back
<?php break; //this will show the table structure by default (ie, no actions) default: //opening query $sql = "SELECT * FROM tblAddress ORDER BY lname, fname ASC"; $result = sqlite_query($dbhandle,$sql) or die("Error in query: ".sqlite_error_string(sqlite_last_error($dbhandle))); //you can put in an error statement if no records or just display, just do what makes sense to you, the rest will come ?>

Addresses

user: <?php print($uname); ?>

<?php //lets set a variable for offest colored rows $rowColor = 0; //here is the loop using the statement above while($info = sqlite_fetch_array($result)){ if($rowColor==0){ ?> <?php $rowColor =1; }elseif($rowColor==1){ ?> <?php $rowColor = 0; } ?> <?php } ?>
Last Name First Name Phone Cell Email Admin
Add Contact
<?php echo $info['lname'];?> <?php echo $info['fname'];?> <?php echo $info['phone'];?> <?php echo $info['cell'];?> <?php echo $info['email'];?> Detail Edit Remove
<?php break; }//end the switch ?> <?php ob_flush();?>