Created
July 4, 2017 04:24
-
-
Save armanhakimsagar/444bc228c73b38b5721f4dd05e2a0cc7 to your computer and use it in GitHub Desktop.
php oop crud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OOP VALIDATON INSERT :-> | |
<?php | |
class webapps{ | |
public $localhost ="localhost"; | |
public $username = "root"; | |
public $password = ""; | |
public $database = "crud"; | |
public $link; | |
public function __construct(){ | |
$this->link = new mysqli($this->localhost,$this->username,$this->password,$this->database); | |
if($this->link){ | |
echo "connected"; | |
}else{ | |
echo "not connected"; | |
} | |
} | |
public function insert($data){ | |
$result = $this->link->query($data); | |
if($result){ | |
throw new exception(); | |
} | |
} | |
public function select($data){ | |
$result = $this->link->query($data); | |
if($result){ | |
return $result; | |
} | |
} | |
} | |
if(isset($_POST['submit'])){ | |
$webapps = new webapps; | |
function validation($data){ | |
$data = trim(strip_tags($data)); | |
return $data; | |
} | |
$name = validation($_POST['name']); | |
$email = validation($_POST['email']); | |
$password = validation($_POST['password']); | |
$retype_pass = validation($_POST['retype_pass']); | |
$phone = validation($_POST['phone']); | |
$date = validation($_POST['date']); | |
$error = 0; | |
if(!$name){ | |
$error = $error+1; | |
} | |
if(!isset($_POST['email']) || filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) == false){ | |
$error = $error+1; | |
} | |
if(!isset($_POST['password']) || !preg_match('/@?[A-Z]/',$_POST['password'])){ | |
$error = $error+1; | |
} | |
if(!isset($_POST['retype_pass']) || $_POST['password']!==$_POST['retype_pass']){ | |
$error = $error+1; | |
} | |
if(!isset($_POST['phone']) || !is_numeric($_POST['phone']) || strlen($_POST['phone'])!=11){ | |
$error = $error+1; | |
} | |
if(!isset($_POST['date']) || !preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $_POST['date'])){ | |
$error = $error+1; | |
} | |
if(isset($_POST['gender'])){ | |
$gender = $_POST['gender']; | |
}else{ | |
$error = $error+1; | |
} | |
if($_POST['country'] != 0){ | |
$country = $_POST['country']; | |
}else{ | |
$error = $error+1; | |
} | |
if(isset($_FILES['image']) && substr($_FILES['image']['name'],-4) == ".jpg"){ | |
$image = $_FILES['image']; | |
}else{ | |
$error = $error+1; | |
} | |
if($error !=0){ | |
echo "error have"; | |
}else{ | |
try{ | |
$webapps = new webapps; | |
$data = "Insert into insertdata values('','$name','$email','$password','$phone','$date','$gender','$country','$image')"; | |
$webapps->insert($data); | |
}catch(Exception $e){ | |
echo "ok"; | |
} | |
} | |
} | |
?> | |
edit.php | |
<?php | |
require("link.php"); | |
if(isset($_GET['edit'])){ | |
$webapps = new webapps; | |
$id = $_GET['edit']; | |
$data = "SELECT * from insertdata WHERE id='$id'"; | |
$data = $webapps->edit($data); | |
$view = $data->fetch_object() ?> | |
<form method="post" enctype="multipart/form-data"> | |
<input type="text" name="name" value="<?php echo $view->name ?>"> <br/><br/> | |
<input type="text" name="email" value="<?php echo $view->email ?>"> <br/><br/> | |
<input type="text" name="password" value="<?php echo $view->password_data ?>"><br/><br/> | |
<input type="text" name="phone" value="<?php echo $view->phone ?>"> <br/><br/> | |
<input type="date" name="date" value="<?php echo $view->date_data ?>"> <br/><br/> | |
Male : <input type="radio" name="gender" <?php if($view->gender == "m"){echo "checked";} ?>> | |
Female : <input type="radio" name="gender" <?php if($view->gender == "f"){echo "checked";} ?>><br/><br/> | |
<select name="country"> | |
<option value="0">Select a country</option> | |
<?php | |
for($ini = 1905;$ini<=2017;$ini++){ ?> | |
<option value="<?php echo $ini ?>" <?php if($ini==$view->country) | |
{ echo "selected"; } ?>><?php echo $ini ?></option> | |
<?php } | |
?> | |
</select><br/><br/> | |
<img src="img/<?php echo $view->image ?>" height="100px" /> <br/><br/> | |
<input type="hidden" name="old_name" value="<?php echo $view->image ?>"/> | |
<input type="hidden" name="id" value="<?php echo $view->id ?>"/> | |
<input type="file" name="new_image" /> | |
<input type="submit" name="update"/> | |
</form> | |
<?php } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment