this is my contacts class

<?php
class contacts
{

    private $name;
    private $cno;
    private $clocation;


    public function __construct($name,$cno,$clocation)
    {
        $this->name = trim($name);
        $this->cno  =trim($cno);
        $this->clocation=trim($clocation);  
    }

    public function getName()
    {
        return $this->name;
    }

    public function getNO()
    {
        return $this->cno;
    }

    public function getLocation()
    {
        return $this->clocation;
    }
}
class DataBaseAction extends contacts
{
    private $db_info;
    private $db_user;
    private $db_pass;
    private $db;
    private $stmt;


    public  function __construct()
    {
        $this->db_info ='mysql:host=localhost;dbname=finalproject';
        $this->db_user ='root';
        $this->db_pass ='mrhot';
    }

    public static function insert_contact($sql)
    {
        $this->db = new PDO($this->db_info, $this->db_user, $this->db_user);
        $this->stmt= $this->db->prepare($sql);
        $this->stmt =execute(array('$this->getName()','$this->getNO()','$this->clocation()'));
        $this->stmt  =closeCursor();
    }
}
and index.php fil e is

<!DOCTYPE HTML>
<html>
<body>
<form action='inc/post.in.php' method='post'>
contact name:
<input type ='text' name="cname" value="">
<br>
contact no:
<input type ='text' name="cno" value="">
<br>
location:
<input type ='text' name="clocation" value="">
<input type="submit" value="submit">
</form>
</body>
</html>

im getting this error when i submit the form

Fatal error: Using $this when not in object context in /opt/lampp/htdocs/pbook/inc/contacts.php on line 50

static functions cannot use $this, only self. Simple solution, remove static.

every thing is done right but when i post the data to database no contact details shown
$this->getName() $this->getNO() $this->clocation() posted to my database my post file code in given
below

<?php

$cname = $_POST['cname'];

$cno =$_POST['cno'];

$clocation = $_POST['clocation'];

include 'contacts.php';

$classobj = new contacts ($cname,$cno,$clocation);

$obj = new DataBaseAction();

$temp = $obj->insert_contact("INSERT INTO contacts (cname,cno,clocation)

values (?,?,?)");

echo $classobj->getLocation();
?>

i have removed static from function also

Change these to protected or public:

private $name;
private $cno;
private $clocation;

Having them set to private means they cannot be overwritten (if i remember)

EDIT
Also check that you DB is connecting

db is connected and query posted as $this->getName() $this->getNO() $this->geTlocation()

i have changed them to public

Should this line:

$this->stmt =execute(array('$this->getName()','$this->getNO()','$this->clocation()'));

Be:

$this->stmt =execute(array($this->getName(),$this->getNO(),$this->clocation()));

no data posted doing this

but dumped the DataBaseAction object showing this

object(DataBaseAction)#1 (8) { ["db_info":"DataBaseAction":private]=> string(40) "mysql:host=localhost;dbname=finalproject" ["db_user":"DataBaseAction":private]=> string(4) "root" ["db_pass":"DataBaseAction":private]=> string(5) "mrhot" ["db":"DataBaseAction":private]=> NULL ["stmt":"DataBaseAction":private]=> NULL ["name"]=> NULL ["cno"]=> NULL ["clocation"]=> NULL }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.