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

}

?>
Rashed
  • 2,349
  • 11
  • 26

2 Answers2

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', 'john@example.com')";

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.

Adnan Haider
  • 265
  • 5
  • 16
  • if OP has only 2 columns, there is no need to specify the columns. maybe confirm from OP first? – Rotimi Feb 28 '18 at 06:11
  • 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 – Adnan Haider Feb 28 '18 at 06:13
  • https://stackoverflow.com/questions/1871331/php-mysql-insert-into-without-using-column-names-but-with-autoincrement-field – Rotimi Feb 28 '18 at 06:16
  • Customer table have more than 2 columns. – Adnan Haider Feb 28 '18 at 06:24
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

Rotimi
  • 4,783
  • 4
  • 18
  • 27