Sample code from this class
esse quam videri
RWW Class
$config = array('db'=>'rhamilton', 'host'=>'localhost', 'password'=>'q:4PYFRVuzV:htNP', 'username'=>'rhamilton'); class ReadWriteWeb { var $hdb; function __construct() { global $config; $this->hdb = mysql_connect($config['host'], $config['username'], $config['password']); mysql_select_db('rhamilton')or die('Failed'); } function ReadWriteWeb() { $this->__construct(); } function getStudents($id=null) { $student_array = array(); if($id) { $query = 'SELECT * FROM `students` WHERE id="'.addslashes($id).'" ORDER BY name ASC'; } else { $query = 'SELECT * FROM `students` ORDER BY name ASC'; } $students = mysql_query($query); while($associated = mysql_fetch_assoc($students)) { $student_array[] = $associated; } return $student_array; } function saveStudent($data) { if(isset($data['active']) && $data['active']=='on') { $active = 1; } else { $active = 0; } if(strlen($data['name'])>3) { $name = addslashes($data['name']); } else { return false; exit(); } $bio = addslashes($data['bio']); $url = addslashes($data['url']); if(!isset($data['id'])) { mysql_query('INSERT INTO `students` VALUES(NULL, "'.$active.'", "'.$name.'", "'.$bio.'", "'.$url.'")'); } else { $id = addslashes($data['id']); $query = 'UPDATE `students` SET(id="'.$id.'", active="'.$active.'", name="'.$name.'", bio="'.$bio.'", url="'.$url.'")'; mysql_query($query); var_dump(mysql_error()); } return true; } function deleteStudent($id) { } }
Edit Page
require_once('libraries/config.php'); require_once('libraries/class.ReadWriteWeb.php'); $rww = new ReadWriteweb(); if(!empty($_POST)) { $result = $rww->saveStudent($_POST); if(!$result) { $error = true; } $student = $rww->getStudents($_POST['id']); } else { $student = $rww->getStudents($_GET['id']); } ?> <form action="edit.php?id=<?php echo $_GET['id']; ?>" method="post"> <?php if(isset($error)) { ?> <div class="error_msg"> Whoops, there was a problem with the form below. </div> <?php } ?> <input type="hidden" name="id" id="id" value="<?php echo $student[0]['id']; ?>" /> <div class="input"> <label for="name">Name</label> <input type="text" name="name" id="name" value="<?php echo $student[0]['name']; ?>" /> </div> <div> <label for="active">Active?</label> <input type="checkbox" id="active" name="active" <?php if($student[0]['active']==1) { echo 'checked'; } ?> /> </div> <div class="input"> <label for="url">URL</label> <input type="text" id="url" name="url" value="<?php echo $student[0]['url']?>" /> </div> <div> <label for="bio">Bio</label> <textarea name="bio" id="bio"><?php echo $student[0]['bio']?></textarea> </div> <div> <input type="submit" value="submit" /> </div>
New Page
<?php require_once('libraries/config.php'); require_once('libraries/class.ReadWriteWeb.php'); $rww = new ReadWriteweb(); if(!empty($_POST)) { $result = $rww->saveStudent($_POST); if(!$result) { $error = true; } } ?> <form action="demo2.php" method="post"> <?php if(isset($error)) { ?> <div class="error_msg"> Whoops, there was a problem with the form below. </div> <?php } ?> <div class="input"> <label for="name">Name</label> <input type="text" name="name" id="name" /> </div> <div> <label for="active">Active?</label> <input type="checkbox" id="active" name="active" /> </div> <div class="input"> <label for="url">URL</label> <input type="text" id="url" name="url" /> </div> <div> <label for="bio">Bio</label> <textarea name="bio" id="bio"></textarea> </div> <div> <input type="submit" value="submit" /> </div> </form>