Error when $_SESSION['id'] is not set

Error when $_SESSION['id'] is not set

Khalid TeliKhalid Teli Posts: 251Questions: 71Answers: 0
edited August 2020 in Free community support

From one table (contract_info.php) I am getting the id of product_code and send it to next page (contract_bal.php)as follows:

          "columnDefs": [
                  
                       {
            "targets": 0,
            "data": "products.product_code",
            "render": function ( data, type, row, meta ) {
              return '<a href="../../examples/simple/contract_bal.php?id='+row.products.product_code+'">'+data+'</a>';     }

And I am receiving it on : contract_bal.php by using:

session_start();
    
    
    if (empty($_GET['id'])) {
    
   
    
    }
    
    else {
    
$id=$_GET['id'];
    
    $_SESSION['id'] = $id;
    
    }

Then this gets saved on server-side by : /contract_bal_fetch.php by using

      ->where(function ($q) {
            $q->where('contract_bal.product_code_fk2',$_SESSION['id']);
        })

The problem is when the session variable is not set , it shows me error undefined variable id. is it possible to haveIF condition inside -> where. By that if it is not set , it will display all records not just the ones that match row's product code

        if (sessionvariable is  set )
         ->where(function ($q) {
                    $q->where('contract_bal.product_code_fk2',$_SESSION['id']);
                })
        
        else{
         ->where(function ($q) {
                    $q->where('contract_bal.product_code_fk2','contract_bal.product_code_fk2');
                })
        
        }

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

This discussion has been closed.