Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NAME DATABASE : crud
- TABEL :
- CREATE TABLE IF NOT EXISTS `user` (
- `id` int(10) NOT NULL AUTO_INCREMENT,
- `username` varchar(25) NOT NULL,
- `password` varchar(25) NOT NULL,
- PRIMARY KEY (`id`)
- )
- FILE 1 : index.php
- <?php
- $connect=mysql_connect("localhost","root","");
- mysql_select_db("crud",$connect);
- $username;
- $password;
- if(isset($_POST["insert"])){
- if($_POST["insert"]=="yes"){
- $username=$_POST["username"];
- $password=$_POST["password"];
- $query="insert into user(username, password) values('$username', '$password')";
- if(mysql_query($query))
- echo "<center>Record Inserted!</center><br>";
- }
- }
- if(isset($_POST["update"])){
- if($_POST["update"]=="yes"){
- $username=$_POST["username"];
- $password=$_POST ["password"];
- $query="update user set username='$username' , password='$password' where id=".$_POST['id'];
- if(mysql_query($query))
- echo "<center>Record Updated</center><br>";
- }
- }
- if(isset($_GET['operation'])){
- if($_GET['operation']=="delete"){
- $query="delete from user where id=".$_GET['id'];
- if(mysql_query($query))
- echo "<center>Record Deleted!</center><br>";
- }
- }
- ?>
- <html>
- <body>
- <form method="post" action="index.php">
- <table align="center" border="0">
- <tr>
- <td>username:</td>
- <td><input type="text" name="username" /></td>
- </tr>
- <tr>
- <td>password:</td>
- <td><input type="password" name="password" /></td>
- </tr>
- <tr>
- <td> </td>
- <td align="right">
- <input type="hidden" name="insert" value="yes" />
- <input type="submit" value="Insert Record"/>
- </td>
- </tr>
- </table>
- </form>
- <?php
- if(isset($_GET['operation'])){
- if($_GET['operation']=="edit"){
- ?>
- <form method="post" action="index.php">
- <table align="center" border="0">
- <tr>
- <td>username:</td>
- <td><input type="text" name="username" value="<?php echo $_GET['username']; ?>" /></td>
- </tr>
- <tr>
- <td>password:</td>
- <td><input type="text" name="password" value="<?php echo $_GET['password']; ?>"/></td>
- </tr>
- <tr>
- <td> </td>
- <td align="right">
- <input type="hidden" name="id" value="<?php echo $_GET['id'] ?>" />
- <input type="hidden" name="update" value="yes" />
- <input type="submit" value="update Record"/>
- </td>
- </tr>
- </table>
- </form>
- <?php
- }}
- ?>
- <?php
- $query="select * from user";
- $result=mysql_query($query);
- if(mysql_num_rows($result)>0){
- echo "<table align='center' border='1'>";
- echo "<tr>";
- echo "<th>Id</th>";
- echo "<th>Username</th>";
- echo "<th>Password</th>";
- echo "</tr>";
- while($row=mysql_fetch_array($result)){
- echo "<tr>";
- echo "<td>".$row['id']."</td>";
- echo "<td>".$row['username']."</td>";
- echo "<td>".$row['password']."</td>";
- echo "<td><a href='index.php?operation=edit&id=".$row['id']."&username=".$row['username']."&password=".$row['password']."'>edit</a></td>";
- echo "<td><a href='index.php?operation=delete&id=".$row['id']."'>delete</a></td>";
- echo "</tr>";
- }
- echo "</table>";
- }
- else{
- echo "<center>No Records Found!</center>";
- }
- ?>
- </body>
- </html>
- FILE 2 : user.class.php
- <?php
- define("USERS", "users");
- class users {
- private $uid;
- private $username;
- private $password;
- private $name;
- private $email;
- public function setuid($pArg="0") {$this->uid=$pArg;}
- public function setusername($pArg="0") {$this->username=$pArg;}
- public function setpassword($pArg="0") {$this->password=$pArg;}
- public function setname($pArg="0") {$this->name=$pArg;}
- public function setemail($pArg="0") {$this->email=$pArg;}
- public function getuid() {return $this->uid;}
- public function getusername() {return $this->username;}
- public function getpassword() {return $this->password;}
- public function getname() {return $this->name;}
- public function getemail() {return $this->email;}
- public function readObject($array = array()) {
- $qry = "SELECT *".RET."FROM ".USERS.RET;
- $and = "WHERE".RET;
- if($array['uid'] != "") {
- $qry .= $and."uid = '".$array['uid']."'".RET;
- $and = "AND".RET;
- }
- if($array['username'] != "") {
- $qry .= $and."username = '".$array['username']."'".RET;
- $and = "AND".RET;
- }
- if($array['password'] != "") {
- $qry .= $and."password = '".$array['password']."'".RET;
- $and = "AND".RET;
- }
- if($array['name'] != "") {
- $qry .= $and."name = '".$array['name']."'".RET;
- $and = "AND".RET;
- }
- if($array['email'] != "") {
- $qry .= $and."email = '".$array['email']."'".RET;
- $and = "AND".RET;
- }
- $record = Database::select($qry);
- if(count($record[0]) == 0) {
- return array();
- } else {
- $record = $record[0];
- $this->setuid($record['uid']);
- $this->setusername($record['username']);
- $this->setpassword($record['password']);
- $this->setname($record['name']);
- $this->setemail($record['email']);
- return true;
- }
- }
- public static function readArray($array = array()) {
- $qry = "SELECT *".RET."FROM ".USERS.RET;
- $and = "WHERE".RET;
- if($array['uid'] != "") {
- $qry .= $and."uid = '".$array['uid']."'".RET;
- $and = "AND".RET;
- }
- if($array['username'] != "") {
- $qry .= $and."username = '".$array['username']."'".RET;
- $and = "AND".RET;
- }
- if($array['password'] != "") {
- $qry .= $and."password = '".$array['password']."'".RET;
- $and = "AND".RET;
- }
- if($array['name'] != "") {
- $qry .= $and."name = '".$array['name']."'".RET;
- $and = "AND".RET;
- }
- if($array['email'] != "") {
- $qry .= $and."email = '".$array['email']."'".RET;
- $and = "AND".RET;
- }
- $recordset = Database::select($qry);
- $class_objects = array();
- if(is_array($recordset) == true) {
- while(list($i, $record) = each($recordset)) {
- $class_object = new users();
- $class_object->setuid($record['uid']);
- $class_object->setusername($record['username']);
- $class_object->setpassword($record['password']);
- $class_object->setname($record['name']);
- $class_object->setemail($record['email']);
- $class_objects[$class_object->getuid()] = $class_object;
- }
- }
- return $class_objects;
- }
- public function insert() {
- if($this->getuid() != '') {
- $qry = "UPDATE ".USERS.RET."SET".RET.
- "uid = '".$this->getuid()."',".RET.
- "username = '".$this->getusername()."',".RET.
- "password = '".$this->getpassword()."',".RET.
- "name = '".$this->getname()."',".RET.
- "email = '".$this->getemail()."'".RET.
- "WHERE uid = ".$this->getuid().RET;
- Database::insert($qry);
- } else {
- $qry = "INSERT INTO ".USERS." (".RET.
- "username, password, name, email".RET.
- ") VALUES (".RET.
- "'".$this->getusername()."',".RET.
- "'".$this->getpassword()."',".RET.
- "'".$this->getname()."',".RET.
- "'".$this->getemail()."'".RET.
- ")".RET;
- $this->setuid(Database::insert($qry));
- }
- }
- public static function delete($array = array()) {
- $qry = "DELETE".RET."FROM ".USERS.RET;
- $and = "WHERE".RET;
- if($array['uid'] != "") {
- $qry .= $and."uid = '".$array['uid']."'".RET;
- $and = "AND".RET;
- }
- if($array['username'] != "") {
- $qry .= $and."username = '".$array['username']."'".RET;
- $and = "AND".RET;
- }
- if($array['password'] != "") {
- $qry .= $and."password = '".$array['password']."'".RET;
- $and = "AND".RET;
- }
- if($array['name'] != "") {
- $qry .= $and."name = '".$array['name']."'".RET;
- $and = "AND".RET;
- }
- if($array['email'] != "") {
- $qry .= $and."email = '".$array['email']."'".RET;
- $and = "AND".RET;
- }
- Database::delete($qry);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment