-1

Hello Developers.

Am working on an application where i have to select data from different tables and views. However some views may not have the corresponding data i have queried yet the other tables have the required data. This gives me undefined error response for the views where the data for a selected variables is not found in the view. How best can i write my code. see below.

error returned

Notice: Undefined variable: fname in C:\wamp\www\Kesa\web\add_shares.php on line 175 Notice: Undefined variable: mname in C:\wamp\www\Kesa\web\add_shares.php on line 175 Notice: Undefined variable: lname in C:\wamp\www\Kesa\web\add_shares.php on line 175 Notice: Undefined variable: passbook_no in C:\wamp\www\Kesa\web\add_shares.php on line 176 Notice: Undefined variable: share_amount in C:\wamp\www\Kesa\web\add_shares.php on line 177

if(isset($_POST['button2'])){

    $id = mysql_real_escape_string($_POST['slt_member_id']);

    //selecting employee details
    $query = "SELECT member_tbl.fname AS fname,
        member_tbl.mname AS mname,
        member_tbl.lname AS lname, 
        member_tbl.member_id AS member_id, 
        member_tbl.Pasbook_no AS passbook_no, 
        share_view.share_amount AS share_amount 
        FROM member_tbl, share_view 
        WHERE member_tbl.member_id = '$id' 
        AND member_tbl.member_id = share_view.member_id";

    $result = mysql_query($query, $connection);
    while($res=mysql_fetch_array($result)){

        $share_amount = $res['share_amount'];

        if(isset($share_amount) || !empty($share_amount)){
            $member_id = $res['member_id'];
            $fname = $res['fname'];
            $mname = $res['mname'];
            $lname = $res['lname'];
            $passbook_no = $res['passbook_no'];
            $share_amount = $res['share_amount'];

        }elseif(!isset($share_amount) || empty($share_amount)){
            $member_id = $res['member_id'];
            $fname = $res['fname'];
            $mname = $res['mname'];
            $lname = $res['lname'];
            $passbook_no = $res['passbook_no'];
            $account_type = 'User has no shares';
            $share_amount = 'User has no shares';

        }else{
            $member_id = '';
            $fname = 'No user selected';
            $mname = '';
            $lname = '';
            $passbook_no = 'No user selected';
            $account_type = 'No user selected';
            $share_amount = 'No user selected';
            }
        }//closing selection from members tbl

}else {
      $member_id = '';
      $fname = 'No user selected';
      $mname = '';
      $lname = '';
      $passbook_no = 'No user selected';
      $account_type = 'No user selected';
      $share_amount = 'No user selected';
}
floriank
  • 25,546
  • 9
  • 42
  • 66
Brian O
  • 1
  • 1
  • I think the code you posted here is not that which is giving error, Post the view code here – Pradeep Singh Jan 19 '18 at 11:46
  • This has nothing at all to do with CakePHP and the mysql_ functions are *deprecated* since years now. There is even a huge red box on top of the manual pages... http://php.net/manual/en/function.mysql-real-escape-string.php Also *if* this code is used inside the CakePHP framework, well you actually managed to ignore the framework completely and might want to do some basic php and CakePHP tutorials. – floriank Jan 19 '18 at 12:09
  • 1
    Possible duplicate of [Notice: Undefined variable](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12778634#12778634) – floriank Jan 19 '18 at 12:11
  • Hello pradeep, here is the view code. however it's just echoing the variables i shared in the first code. i believe the main issue is on the code i first share that's the one i need assistance with. but please advise. code here. – Brian O Jan 19 '18 at 18:26

1 Answers1

0

Fetch your result as an associative array with fetch_assoc(). Then you can access the result via the column name. Currently you are fetching the result as an indexed array by fetch_array(). So you will have to access the values as $result[1].