irwan

PHP BASIC (Create Read Update Delete)

Feb 15th, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.35 KB | None | 0 0
  1. NAME DATABASE : crud
  2.  
  3. TABEL :
  4.  
  5. CREATE TABLE IF NOT EXISTS `user` (
  6.   `id` int(10) NOT NULL AUTO_INCREMENT,
  7.   `username` varchar(25) NOT NULL,
  8.   `password` varchar(25) NOT NULL,
  9.   PRIMARY KEY (`id`)
  10. )
  11.  
  12.  
  13.  
  14.  
  15. FILE 1 : index.php
  16.  
  17. <?php
  18. $connect=mysql_connect("localhost","root","");
  19. mysql_select_db("crud",$connect);
  20. $username;
  21. $password;
  22. if(isset($_POST["insert"])){
  23.     if($_POST["insert"]=="yes"){
  24.     $username=$_POST["username"];
  25.     $password=$_POST["password"];
  26.  
  27. $query="insert into user(username, password) values('$username', '$password')";
  28. if(mysql_query($query))
  29. echo "<center>Record Inserted!</center><br>";
  30.     }
  31. }
  32.  
  33. if(isset($_POST["update"])){
  34.     if($_POST["update"]=="yes"){
  35.     $username=$_POST["username"];
  36.     $password=$_POST ["password"];
  37.  
  38. $query="update user set username='$username' , password='$password' where id=".$_POST['id'];
  39. if(mysql_query($query))
  40. echo "<center>Record Updated</center><br>";
  41.     }
  42. }
  43.  
  44. if(isset($_GET['operation'])){
  45. if($_GET['operation']=="delete"){
  46. $query="delete from user where id=".$_GET['id'];
  47. if(mysql_query($query))
  48. echo "<center>Record Deleted!</center><br>";
  49. }
  50. }
  51. ?>
  52. <html>
  53. <body>
  54. <form method="post" action="index.php">
  55. <table align="center" border="0">
  56. <tr>
  57. <td>username:</td>
  58. <td><input type="text" name="username" /></td>
  59. </tr>
  60. <tr>
  61. <td>password:</td>
  62. <td><input type="password" name="password" /></td>
  63. </tr>
  64. <tr>
  65. <td>&nbsp;</td>
  66. <td align="right">
  67. <input type="hidden" name="insert" value="yes" />
  68. <input type="submit" value="Insert Record"/>
  69. </td>
  70. </tr>
  71. </table>
  72. </form>
  73. <?php
  74.  
  75. if(isset($_GET['operation'])){
  76. if($_GET['operation']=="edit"){
  77. ?>
  78. <form method="post" action="index.php">
  79. <table align="center" border="0">
  80. <tr>
  81. <td>username:</td>
  82. <td><input type="text" name="username" value="<?php echo $_GET['username']; ?>" /></td>
  83. </tr>
  84. <tr>
  85. <td>password:</td>
  86. <td><input type="text" name="password" value="<?php echo $_GET['password']; ?>"/></td>
  87. </tr>
  88. <tr>
  89. <td>&nbsp;</td>
  90. <td align="right">
  91. <input type="hidden" name="id" value="<?php echo $_GET['id'] ?>" />
  92. <input type="hidden" name="update" value="yes" />
  93. <input type="submit" value="update Record"/>
  94. </td>
  95. </tr>
  96. </table>
  97. </form>
  98. <?php
  99. }}
  100. ?>
  101. <?php
  102. $query="select * from user";
  103. $result=mysql_query($query);
  104. if(mysql_num_rows($result)>0){
  105.     echo "<table align='center' border='1'>";
  106.     echo "<tr>";
  107.     echo "<th>Id</th>";
  108.     echo "<th>Username</th>";
  109.     echo "<th>Password</th>";
  110.     echo "</tr>";
  111.     while($row=mysql_fetch_array($result)){
  112.     echo "<tr>";
  113.     echo "<td>".$row['id']."</td>";
  114.     echo "<td>".$row['username']."</td>";
  115.     echo "<td>".$row['password']."</td>";
  116.     echo "<td><a href='index.php?operation=edit&id=".$row['id']."&username=".$row['username']."&password=".$row['password']."'>edit</a></td>";
  117.     echo "<td><a href='index.php?operation=delete&id=".$row['id']."'>delete</a></td>";
  118.     echo "</tr>";
  119.     }
  120.     echo "</table>";
  121. }
  122. else{
  123. echo "<center>No Records Found!</center>";
  124. }
  125. ?>
  126. </body>
  127. </html>
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. FILE 2 : user.class.php
  136.  
  137. <?php
  138.  
  139. define("USERS", "users");
  140. class users {
  141.  
  142.     private $uid;
  143.     private $username;
  144.     private $password;
  145.     private $name;
  146.     private $email;
  147.  
  148.     public function setuid($pArg="0") {$this->uid=$pArg;}
  149.     public function setusername($pArg="0") {$this->username=$pArg;}
  150.     public function setpassword($pArg="0") {$this->password=$pArg;}
  151.     public function setname($pArg="0") {$this->name=$pArg;}
  152.     public function setemail($pArg="0") {$this->email=$pArg;}
  153.  
  154.     public function getuid() {return $this->uid;}
  155.     public function getusername() {return $this->username;}
  156.     public function getpassword() {return $this->password;}
  157.     public function getname() {return $this->name;}
  158.     public function getemail() {return $this->email;}
  159.  
  160.     public function readObject($array = array()) {
  161.         $qry = "SELECT *".RET."FROM ".USERS.RET;
  162.         $and = "WHERE".RET;
  163.  
  164.         if($array['uid'] != "") {
  165.             $qry .= $and."uid = '".$array['uid']."'".RET;
  166.             $and = "AND".RET;
  167.         }
  168.  
  169.         if($array['username'] != "") {
  170.             $qry .= $and."username = '".$array['username']."'".RET;
  171.             $and = "AND".RET;
  172.         }
  173.  
  174.         if($array['password'] != "") {
  175.             $qry .= $and."password = '".$array['password']."'".RET;
  176.             $and = "AND".RET;
  177.         }
  178.  
  179.         if($array['name'] != "") {
  180.             $qry .= $and."name = '".$array['name']."'".RET;
  181.             $and = "AND".RET;
  182.         }
  183.  
  184.         if($array['email'] != "") {
  185.             $qry .= $and."email = '".$array['email']."'".RET;
  186.             $and = "AND".RET;
  187.         }
  188.  
  189.         $record = Database::select($qry);
  190.         if(count($record[0]) == 0) {
  191.             return array();
  192.         } else {
  193.             $record = $record[0];
  194.             $this->setuid($record['uid']);
  195.             $this->setusername($record['username']);
  196.             $this->setpassword($record['password']);
  197.             $this->setname($record['name']);
  198.             $this->setemail($record['email']);
  199.             return true;
  200.         }
  201.     }
  202.  
  203.     public static function readArray($array = array()) {
  204.         $qry = "SELECT *".RET."FROM ".USERS.RET;
  205.         $and = "WHERE".RET;
  206.  
  207.         if($array['uid'] != "") {
  208.             $qry .= $and."uid = '".$array['uid']."'".RET;
  209.             $and = "AND".RET;
  210.         }
  211.  
  212.         if($array['username'] != "") {
  213.             $qry .= $and."username = '".$array['username']."'".RET;
  214.             $and = "AND".RET;
  215.         }
  216.  
  217.         if($array['password'] != "") {
  218.             $qry .= $and."password = '".$array['password']."'".RET;
  219.             $and = "AND".RET;
  220.         }
  221.  
  222.         if($array['name'] != "") {
  223.             $qry .= $and."name = '".$array['name']."'".RET;
  224.             $and = "AND".RET;
  225.         }
  226.  
  227.         if($array['email'] != "") {
  228.             $qry .= $and."email = '".$array['email']."'".RET;
  229.             $and = "AND".RET;
  230.         }
  231.  
  232.         $recordset = Database::select($qry);
  233.         $class_objects = array();
  234.         if(is_array($recordset) == true) {
  235.             while(list($i, $record) = each($recordset)) {
  236.                 $class_object = new users();
  237.                 $class_object->setuid($record['uid']);
  238.                 $class_object->setusername($record['username']);
  239.                 $class_object->setpassword($record['password']);
  240.                 $class_object->setname($record['name']);
  241.                 $class_object->setemail($record['email']);
  242.                 $class_objects[$class_object->getuid()] = $class_object;
  243.             }
  244.         }
  245.         return $class_objects;
  246.     }
  247.  
  248.     public function insert() {
  249.         if($this->getuid() != '') {
  250.             $qry  = "UPDATE ".USERS.RET."SET".RET.
  251.             "uid = '".$this->getuid()."',".RET.
  252.             "username = '".$this->getusername()."',".RET.
  253.             "password = '".$this->getpassword()."',".RET.
  254.             "name = '".$this->getname()."',".RET.
  255.             "email = '".$this->getemail()."'".RET.
  256.             "WHERE uid = ".$this->getuid().RET;
  257.  
  258.             Database::insert($qry);
  259.         } else {
  260.             $qry  = "INSERT INTO ".USERS." (".RET.
  261.             "username, password, name, email".RET.
  262.                 ") VALUES (".RET.
  263.             "'".$this->getusername()."',".RET.
  264.             "'".$this->getpassword()."',".RET.
  265.             "'".$this->getname()."',".RET.
  266.             "'".$this->getemail()."'".RET.
  267.             ")".RET;
  268.  
  269.             $this->setuid(Database::insert($qry));
  270.         }
  271.     }
  272.  
  273.     public static function delete($array = array()) {
  274.         $qry = "DELETE".RET."FROM ".USERS.RET;
  275.         $and = "WHERE".RET;
  276.  
  277.         if($array['uid'] != "") {
  278.             $qry .= $and."uid = '".$array['uid']."'".RET;
  279.             $and = "AND".RET;
  280.         }
  281.  
  282.         if($array['username'] != "") {
  283.             $qry .= $and."username = '".$array['username']."'".RET;
  284.             $and = "AND".RET;
  285.         }
  286.  
  287.         if($array['password'] != "") {
  288.             $qry .= $and."password = '".$array['password']."'".RET;
  289.             $and = "AND".RET;
  290.         }
  291.  
  292.         if($array['name'] != "") {
  293.             $qry .= $and."name = '".$array['name']."'".RET;
  294.             $and = "AND".RET;
  295.         }
  296.  
  297.         if($array['email'] != "") {
  298.             $qry .= $and."email = '".$array['email']."'".RET;
  299.             $and = "AND".RET;
  300.         }
  301.  
  302.         Database::delete($qry);
  303.     }
  304. }
  305.  
  306. ?>
Advertisement
Add Comment
Please, Sign In to add comment