Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. OOP VALIDATON INSERT :->
  2.  
  3. <?php
  4.  
  5. class webapps{
  6.  
  7. public $localhost ="localhost";
  8. public $username = "root";
  9. public $password = "";
  10. public $database = "crud";
  11. public $link;
  12.  
  13. public function __construct(){
  14.  
  15. $this->link = new mysqli($this->localhost,$this->username,$this->password,$this->database);
  16.  
  17. if($this->link){
  18. echo "connected";
  19. }else{
  20. echo "not connected";
  21. }
  22.  
  23. }
  24.  
  25. public function insert($data){
  26. $result = $this->link->query($data);
  27. if($result){
  28. throw new exception();
  29. }
  30. }
  31.  
  32. public function select($data){
  33. $result = $this->link->query($data);
  34. if($result){
  35. return $result;
  36. }
  37. }
  38.  
  39. }
  40.  
  41. if(isset($_POST['submit'])){
  42.  
  43.  
  44.  
  45. $webapps = new webapps;
  46.  
  47. function validation($data){
  48. $data = trim(strip_tags($data));
  49. return $data;
  50. }
  51.  
  52.  
  53.  
  54. $name = validation($_POST['name']);
  55. $email = validation($_POST['email']);
  56. $password = validation($_POST['password']);
  57. $retype_pass = validation($_POST['retype_pass']);
  58. $phone = validation($_POST['phone']);
  59. $date = validation($_POST['date']);
  60.  
  61.  
  62. $error = 0;
  63.  
  64.  
  65. if(!$name){
  66. $error = $error+1;
  67. }
  68. if(!isset($_POST['email']) || filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) == false){
  69. $error = $error+1;
  70. }
  71. if(!isset($_POST['password']) || !preg_match('/@?[A-Z]/',$_POST['password'])){
  72. $error = $error+1;
  73. }
  74. if(!isset($_POST['retype_pass']) || $_POST['password']!==$_POST['retype_pass']){
  75. $error = $error+1;
  76. }
  77. if(!isset($_POST['phone']) || !is_numeric($_POST['phone']) || strlen($_POST['phone'])!=11){
  78. $error = $error+1;
  79. }
  80. if(!isset($_POST['date']) || !preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $_POST['date'])){
  81. $error = $error+1;
  82. }
  83. if(isset($_POST['gender'])){
  84. $gender = $_POST['gender'];
  85. }else{
  86. $error = $error+1;
  87. }
  88. if($_POST['country'] != 0){
  89. $country = $_POST['country'];
  90. }else{
  91. $error = $error+1;
  92. }
  93. if(isset($_FILES['image']) && substr($_FILES['image']['name'],-4) == ".jpg"){
  94. $image = $_FILES['image'];
  95. }else{
  96. $error = $error+1;
  97. }
  98. if($error !=0){
  99. echo "error have";
  100. }else{
  101.  
  102.  
  103. try{
  104. $webapps = new webapps;
  105. $data = "Insert into insertdata values('','$name','$email','$password','$phone','$date','$gender','$country','$image')";
  106. $webapps->insert($data);
  107.  
  108. }catch(Exception $e){
  109. echo "ok";
  110. }
  111.  
  112. }
  113. }
  114.  
  115.  
  116.  
  117. ?>
  118.  
  119.  
  120.  
  121.  
  122. edit.php
  123.  
  124.  
  125.  
  126. <?php
  127.  
  128. require("link.php");
  129.  
  130. if(isset($_GET['edit'])){
  131.  
  132. $webapps = new webapps;
  133. $id = $_GET['edit'];
  134. $data = "SELECT * from insertdata WHERE id='$id'";
  135. $data = $webapps->edit($data);
  136. $view = $data->fetch_object() ?>
  137.  
  138. <form method="post" enctype="multipart/form-data">
  139. <input type="text" name="name" value="<?php echo $view->name ?>"> <br/><br/>
  140. <input type="text" name="email" value="<?php echo $view->email ?>"> <br/><br/>
  141. <input type="text" name="password" value="<?php echo $view->password_data ?>"><br/><br/>
  142. <input type="text" name="phone" value="<?php echo $view->phone ?>"> <br/><br/>
  143. <input type="date" name="date" value="<?php echo $view->date_data ?>"> <br/><br/>
  144. Male : <input type="radio" name="gender" <?php if($view->gender == "m"){echo "checked";} ?>>
  145. Female : <input type="radio" name="gender" <?php if($view->gender == "f"){echo "checked";} ?>><br/><br/>
  146. <select name="country">
  147. <option value="0">Select a country</option>
  148. <?php
  149. for($ini = 1905;$ini<=2017;$ini++){ ?>
  150.  
  151. <option value="<?php echo $ini ?>" <?php if($ini==$view->country)
  152.  
  153. { echo "selected"; } ?>><?php echo $ini ?></option>
  154.  
  155. <?php }
  156. ?>
  157. </select><br/><br/>
  158. <img src="img/<?php echo $view->image ?>" height="100px" /> <br/><br/>
  159. <input type="hidden" name="old_name" value="<?php echo $view->image ?>"/>
  160. <input type="hidden" name="id" value="<?php echo $view->id ?>"/>
  161. <input type="file" name="new_image" />
  162. <input type="submit" name="update"/>
  163. </form>
  164.  
  165. <?php }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement