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:
- Complaints (in classes/ class-complaints.php)
- DBConnector (in classes/ class-db-connector.php)
- 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.