0

I'm very new to PHP and this might seen like a foolish question :(.

Currently I'm interacting with 3 classes of my project named as:

  1. Complaints (in classes/ class-complaints.php)
  2. DBConnector (in classes/ class-db-connector.php)
  3. People (in classes/ class-people.php)

Also, I'm using process-complaints.php (in src folder) to include these classes and add data to them in relevant ways.

This is a part from process-complaints.php file

require "./classes/class-db-connector.php";
require "./classes/class-people.php";
require "./classes/class-complaints.php";

use classes\DBConnector;
use classes\People;
use classes\Complaints;

$dbcon = new DBConnector();

if(isset($_POST["add"])){
    $con = $dbcon->getConnection();

    if(empty($city)){
        //$peopleObject->addPerson($con); // This line works fine
        $complaintObject->addComplaint($con, "NULL");
        echo "OK";
        //$complaintObject->addRoleInCase($con, $peopleObject->getNIC());
    }
}

This is class-complaints.php file

namespace classes;
use PDOException;

class Complaints{
    private $date;

    public function addComplaint($con, $location_id){
        $query = "INSERT INTO complaint(date) VALUES(?)";
    
        try{
            echo "$this->date"; // Debug Line to check whether values are set. Works

            $pstmt = $con->prepare($query);
            echo "query prepared"; // Debug line. Works

            $pstmt->bindValue(1, $this->date);
            echo "values binded"; // Debug line. Works

            $a = $pstmt->execute();
            echo "query executed"; // Debug line and Does not work
    
        }catch(PDOException $e){
            $e->getMessage();
        }
}

The execute in the Complaints class does not work and I cannot find why.

The addPerson() method is in the classes\People class and works as expected when uncommented. The addComplaint() method in classes\Complaints does not work after $pstmt->execute(). All other debug lines are being printed (even the "OK" line in the process-complaints.php)

The file does not show any errors and even the db fields are correct and db connection is valid.

  • This question is quite simple, you can't find why just because your error reporting is a cargo cult code without any meaning. Just remove the try { line and the entire contents of the catch block and you should be fine – Your Common Sense Jul 22 '23 at 13:04
  • Unrelated: your code is quite good, but consider making $con a pricate class variable set through constructor, while $date shouldn't really be a class variable. – Your Common Sense Jul 22 '23 at 13:06
  • Thank you helping out! I finally could hunt the error: removed the try catch, then it said it's an internal server error :(, read some articles on the internet and found about PHP log files, the was inserting a data which is not supported by the data type in the column in DB. PS: Thank you very much for code suggestions too :). Appreciate the help! – CST20004 A.M.S.I. Attanayake Jul 22 '23 at 18:17

0 Answers0