I was trying to insert data to the database through a form using object oriented php validation. I'm not getting any errors but data is not inserted to the database. Can someone please find me out what's wrong. A simple explanation would be highly appreciated because I'm a beginner.
These are my codes:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Customer Registration</h1>
<form action="oovalidation.php" method="post">
<label>Name</label>
<input type="text" name="name" id="nameField"/>
<br>
<label>Mobile</label>
<input type="text" name="mobile" id="mobileField"/>
<br>
<button type="submit" name="submit"> Add</button>
</form>
<script type="text/javascript">
function checkName(){
var text=document.getElementById("nameField").value;
if(text.length>=3){
alert("Name is ok");
return true;
}else{
alert("Wrong name");
return false;
}}
function checkMobile(){
var text=document.getElementById("mobileField").value;
if(text.length==10){
alert("Mobile is ok");
return true;
}else{
alert("Wrong mobile");
return false;
}}
function checkForm(){
var x=checkName();
var y=checkMobile();
return x&&y;
}
</script>
</body>
</html>
<?php
Class customer{
private $name;
private $mobile;
public function setName($name){
$namelen=strlen($name);
if($namelen>=3){
$this->name=$name;
return true;
}else{
echo "Wrong Name";
return false;
}
}
public function getName(){
return $this->name;
}
public function setMobile($mobile){
$mobilelen=strlen($mobile);
if($mobilelen==10){
$this->mobile=$mobile;
return true;
}else{
echo "Wrong Mobile";
return false;
}
}
public function getMobile(){
return $this->mobile;
}
public function save(){
$db=new DBManager();
$con=$db->getConnection();
$sql="insert into customer values('".$this->name."','".$this->mobile."')";
mysqli_query($con,$sql);
mysqli_close($con);
}
}
Class DBManager{
private $hostname='localhost';
private $dbuser='root';
private $dbpass='123';
private $dbname='sem3';
public function getConnection(){
return mysqli_connect($this->hostname,$this->dbuser,$this->dbpass,$this->dbname);
}
}
if(isset($_POST['submit'])){
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$x=new customer();
$nameValidity=$x->setName($name);
$mobileValidity=$x->setMobile($mobile);
if($nameValidity && $mobileValidity)
$x->save();
}
?>