0

Hi Guys I have a monitoring system which it could monitor the tenants that comes in and comes out in a stall. The INSERT works properly if you inserted a tenant on that stall for the first time, but after that tenant was not renting and making that tenant status inactive, that stall he/she left will be now available. And after that I cannot INSERT a tenant on that stall.

here is my code:

<?php
include ('dbcon.php');

if (isset($_POST['add'])){
$tenant_fname = $_POST['tenant_fname'];
$tenant_mname = $_POST['tenant_mname'];
$tenant_lname = $_POST['tenant_lname'];
$tenant_address = $_POST['tenant_address'];
$tenant_contact = $_POST['tenant_contact'];
$stall_id = $_POST['stall_id'];
$stall_name = $_POST['stall_name']; 
$stall_category = $_POST['stall_category'];

$q1 = $conn->prepare("SELECT * FROM tenant WHERE stall_id = ? ");
$q1->execute(array($stall_id));
$c = $q1->rowCount();

if (!$stall_id){
header('location:pages/tenants.php');
}else{

if ($c > 0){
    header('location:pages/tenants.php');
}else{

    $add = $conn->prepare("INSERT INTO tenant (rate_id,tenant_fname, tenant_mname, tenant_lname, tenant_address, tenant_contact, stall_id, stall_name, stall_category)
        VALUES (?,?,?,?,?,?,?,?,?) ");
    $add->execute(array(1,$tenant_fname,$tenant_mname,$tenant_lname,$tenant_address,$tenant_contact,$stall_id,$stall_name,$stall_category));
    $count = $add->rowCount();
    $tenant_id = $conn->lastInsertId();
    $date_today = date('F d, Y');

    if ($count > 0){
        $add1 = $conn->prepare("INSERT INTO tenant_status (tenant_id, date_started, date_ended, tenant_status) VALUES(?,?,?,?) ");
        $add1->execute(array($tenant_id,$date_today,0,1));

        $add2 = $conn->prepare("INSERT INTO rent (tenant_id,stall_id,rent_status) VALUES (?,?,?)");
        $add2->execute(array($tenant_id,$stall_id,1));

        $add3 = $conn->prepare("INSERT INTO receipt(tenant_id) VALUES (?)");
        $add3->execute(array($tenant_id));

    }
    header('location:pages/tenants.php');
}
}

}
?>

BTW the value 0 is for the tenant Inactive and 1 for active.

Ronnel.D
  • 67
  • 1
  • 1
  • 10
  • And what error do you get? – Ken Y-N Oct 18 '16 at 00:45
  • @KenY-N there is no error, it just does not INSERT some values on the database – Ronnel.D Oct 18 '16 at 00:48
  • Well, that's because you [don't check the return value of `execute()`](https://stackoverflow.com/questions/9991882/stmt-execute-how-to-know-if-db-insert-was-successful). – Ken Y-N Oct 18 '16 at 00:54
  • @KenY-N i cant really understand. – Ronnel.D Oct 18 '16 at 00:57
  • Consider providing error messages, have you ever tried "Trial and error"? Google it –  Oct 18 '16 at 01:10
  • @Ronnel.D, `execute()` returns `false` if it fails, but the code above does not check the return value or look to see what `$add1/2/3->error` holds, as discussed in the second link; `INSERT` will by definition fail if there is already a record there with the same unique key. – Ken Y-N Oct 18 '16 at 01:31

0 Answers0