0

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

3
Contributors
9
Replies
38
Views
4 Years
Discussion Span
Last Post by kiLLer.zoh_1
0

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

Edited by pritaeas

0

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

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

Edited by Squidge

0

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

1

Should this line:

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

Be:

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

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 }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.