0

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();

}

?>
2
  • your properties in the customer class should not be private. try public Commented Feb 28, 2018 at 6:14
  • How many fields do you have in your table customer. Commented Feb 28, 2018 at 6:20

2 Answers 2

1

If there are more than 2 columns in customer table, Add Column Names in your query.

$sql="insert into customer (column1,column2) values('".$this->name."','".$this->mobile."')";

Example Query.

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";

OR

Add null for remaining fields. Suppose you have 3 columns.

  • id
  • name
  • mobile

    $sql="insert into customer values(NULL,'".$this->name."','".$this->mobile."')";

NOTE: Fields are in the same order and same number as the MySQL table fields.

Sign up to request clarification or add additional context in comments.

4 Comments

if OP has only 2 columns, there is no need to specify the columns. maybe confirm from OP first?
In this question mysqli procedural is used. Try to run the query without column names you get this error. 1136 - Column count doesn't match value count at row 1
Customer table have more than 2 columns.
0

Your query to insert should work provided the customer table only has two columns. The reason nothing gets saved is because your properties in the customer class are private. Make them public

Change

private $name;
private $mobile;

To

public $name;
public $mobile;

Also FYI your insert query is subject to SQL injection attacks. Use prepared statements. Have a look at the manual for more information

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.