0

Im learning how to use classes in php

Im trying to insert information on my database, but its giving me this error

Fatal error: Call to undefined method Produto::introduzirProduto() in C:\xampp\htdocs\kapa\classes\lol.php on line 7

Connection code:

<?php

class dbConnect{
private $db_host;
            private $db_user;  
            private $db_pass;  
            private $emp_db = "mydb";

            var $connection;
            var $db_select;
            var $result;
            var $row;

            function __construct(){
                $this->db_host = "localhost";
                $this->db_user = "root";
                $this->db_pass = "";
            }

            function connect(){
                $this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);   //create connection
                    checkErr($this->$connection);

                $this->db_select = mysql_select_db($this->emp_db, $connection);
                    checkErr($this->db_select);
            }
}

?>

Form:

<?php
require_once 'introduzir.class.php';
?>
<form action="lol.php" method="POST" id="">
  <input type="text" name="id"/>
  <input type="submit" value="ok.." />
</form>

Insert code

<?php 
  require_once('conectar.php');

  class Produto  extends dbConnect {
        var $bd;
  }

     function Produto() {
        global $emp_db, $db_user, $db_pass, $db_host;
                $this->bd = new dbConnect();
                $this->bd->ligarBD($emp_db, $db_user, $db_pass, $db_host);
        }

        function introduzirProduto($id) {
            $sql = "INSERT INTO produto VALUES ('$id')";
            if($this->bd->executarSQL($sql)) return true;
            else return false;
        }


?>

<?php
require_once 'introduzir.class.php';

if($_REQUEST['id']!="") {

                            $produto = new Produto();
                            if($produto->introduzirProduto($_REQUEST['id'])) {

                                echo "Registo efectuado com sucesso !!!<br>";
                            } else {
                                echo "Problema encontado no Registo !!!<br>";
                            }


            } else {
                echo "ERRO: Deve introduzir a sua password...<br>";
            }

?>

what do i need to change?

1
  • 2
    Make a proper indentation first.
    – u_mulder
    Commented Nov 9, 2013 at 16:43

2 Answers 2

1

The class you've created called Produto has no functions, you've defined the $bd variable, then you've ended the definition of the class. You need to remove that curly bracket after the variable declaration and put it after the functions, like this:

<?php 
  require_once('conectar.php');

  class Produto  extends dbConnect {
        var $bd;

        function Produto() {
                global $emp_db, $db_user, $db_pass, $db_host;
                $this->bd = new dbConnect();
                $this->bd->ligarBD($emp_db, $db_user, $db_pass, $db_host);
        }

        function introduzirProduto($id) {
            $sql = "INSERT INTO produto VALUES ('$id')";
            if($this->bd->executarSQL($sql)) return true;
            else return false;
        }
   }

?>

As a side note, you've got a function that starts with a capital, and one that doesn't, try and stick to a stricter naming convention, for example: always start with a lower case letter and the words after use a capital letter (as with your 2nd function)

0

You've closed the braces for your Produto class before the definition of the functions Produto() and introdizirProduto(). I strongly suggest you indent the code properly to avoid similar errors in future.

<?php 
require_once('conectar.php');

class Produto  extends dbConnect {
    public $bd;

    function Produto() {
        global $emp_db, $db_user, $db_pass, $db_host;
        $this->bd = new dbConnect();
        $this->bd->ligarBD($emp_db, $db_user, $db_pass, $db_host);
    }

    function introduzirProduto($id) {
        $sql = "INSERT INTO produto VALUES ('$id')";
        if($this->bd->executarSQL($sql)) return true;
        else return false;
    }
}

//Rest of your code follows.
?>

PS: var is deprecated in PHP, avoid its use.\

Not the answer you're looking for? Browse other questions tagged or ask your own question.