Php > Databases
Add, Edit & Update All in one Contact Management Form
Add, Edit & Update All in one Contact Management Form Here is a fun example on how to use functions to squeeze the most out of one page. This page lists users, adds users and edits users. /* CREATE TABLE `kids` ( `id` int(10) NOT NULL auto_increment, `first_name` varchar(30) NOT NULL default '', `last_name` varchar(50) NOT NULL default '', `email` varchar(75) default NULL, `contact_status` tinyint(1) NOT NULL default '0', PRIMARY KEY (`id`) ) TYPE=MyISAM ; */ ?>
Manage contact's data
Manage Contact's Data Form
First Name:
Last Name:
Email Address:
Stop Contact?
>
}//end function /* list all the contacts in the db */ function list_users() { $y = 0; //counter $sql = "select * from contacts "; //may want to add the option where clause to only take kids with an active status $result = conn($sql); echo "
Manage Contacts Data Form
Add a new contact
"; if (mysql_num_rows($result)){ //show a list of kids with name as a link to the prepopulated form with their data in it while($rows = mysql_fetch_array($result)){ //change row background color (($y % 2) == 0) ? $bgcolor = "#8FBC8F" : $bgcolor=" #9ACD32"; //build strings to make life easier $name = $rows['first_name'].' '.$rows['last_name']; $status = $rows['contact_status']; $id = $rows['id']; //convert status to readable string from 1 or 0 ($status == 0) ? $status = "Available to contact" : $status = "Do not contact at present."; //echo out the row echo "
$name
$status
"; $y++; //increment the counter }//end while echo "
"; }else{ //handle no results echo "
No data found.
"; }//endif } /* add / update the contact's data*/ function process_form() { $fname = ''; $lname = ''; $email = ''; $id = ''; $action = ''; $status = 0; //default value $fname = @$_POST['first_name']; $lname = @$_POST['last_name']; $email = @$_POST['email']; $id = @$_POST['id']; $action = @$_GET['action']; $status = @$_POST['status']; //if no status is set, defaults to 0 (allow contact) if ($status == ''){$status = 0; } if (($fname=='')||($lname=='')||($email=='')) { $msg = "Some data from the form was forgotten. Please fill in the entire form."; confirm($msg); $data = "$fname|$lname|$email|$id"; show_form('',$data); die(); }//end if //You could add some validation of the data ( I recommend it and its a great way to get your feet wet with php ) if ($action == "add") { $sql = "insert into contacts (first_name, last_name, email, contact_status) values('$fname','$lname','$email',$status)"; $msg = "Record successfully added"; }elseif($action=="edit"){ $sql = "update contacts set first_name = '$fname', last_name = '$lname', email = '$fname', contact_status = '$status' where id = $id"; $msg = "Record successfully updated"; } $result = conn($sql); if (mysql_errno()==0) { confirm($msg); list_users(); }else{ $msg = "There was a problem adding the user to the database. Error is:".mysql_error(); confirm($mag); }//end if } /* db connection function */ function conn($sql) { $host = "localhost"; $user = "user"; $pass = "pass"; $db = "my_db"; //echo "commnecing connection to local db
"; if (!($conn=mysql_connect($host, $user, $pass))) { printf("error connecting to DB by user = $user and pwd=$pass"); exit; } $db3=mysql_select_db($db,$conn) or die("Unable to connect to local database"); $result = mysql_query($sql) or die ("Can't run query because ". mysql_error()); return $result; }//end function /* alert box popup confimation message function */ function confirm($msg) { echo ""; }//end function ?>
Php Codes
Algorithms
Arrays
Authentication
Calendar
Code Snippets
Programs
Content Manage
Contest Related
Cookies
Credit Cards
DBase Related
Databases
Date Time
Directories
E-Mail
Errors
File
File System
Forms
Handling
Graphics
HTML and PHP
Informix
Ingres
InterBase
LDAP
Look and Feel
Miscellaneous
MySQL
Other
PHP Classes
Searching
Navigation
Statistics
Strings
User Manage