Jump to content

Warning: Undefined variable


Recommended Posts



I am having issues and i cant see where.

i was wondering if somebody can point me where i am going wrong.

This is my query

    $candidate_id = $_SESSION['candidate_id'];
                        $query = "SELECT * FROM candidate where candidate_id='$candidate_id' ";
                    $query_run = mysqli_query($conn,$query);
                    while($row = mysqli_fetch_array($query_run))
                        $Title= $row['Salutation'];
                        $FirstName= $row['FirstName'];
                        $Surname= $row['LastName'];
                        $DateOfBirth= $row['DateOfBirth'];
                        $NiNumber= $row['NiNumberPaye'];
                        $MobileNumber= $row['Mobile'];
                        $HomeNumber= $row['HomeNumber'];
                        $Email= $row['Email1'];
                        $PostCode= $row['Postcode'];
                        $Address= $row['Address1'];
                        $P46 = $row['P46BoxPaye'];
                        $SortCode = $row['BankSortCodePaye'];
                        $AccountNumber = $row['BankAccountNoPaye'];
                    $query = "SELECT * FROM candidate_paye where CandidateID='$candidate_id' ";
                    $query_run = mysqli_query($conn,$query);
                    if($row = mysqli_fetch_array($query_run))
                          $Nationality = $row['Nationality'];
                          $NextofKin = $row['NextofKin'];
                          $NextofKinContactNo = $row['NextofKinContactNo'];
                          $NameOfBank= $row['NameOfBank'];
                          $AccountHoldersName = $row['AccountHoldersName'];
                          $BuildingSocietyRefNo = $row['BuildingSocietyRefNo'];
                          $JobType = $row['JobType'];
                          $Position = $row['Position'];
                          $EmploymentDates = $row['EmploymentDates'];
                          $CompanyName = $row['CompanyName'];
                          $ManagerName = $row['ManagerName'];
                          //$P46 = $row['P46'];
                          $StudentLoan = $row['StudentLoan'];
                          $Rehabilitation = $row['Rehabilitation'];
                          $Disabled = $row['Disabled'];
                          $SignatureTemporaryWorkerCondition = $row['SignatureTemporaryWorkerCondition'];
                          $FullNameTemporaryWorkerCondition = $row['FullNameTemporaryWorkerCondition'];
                          $DateTemporaryWorkerCondition = $row['DateTemporaryWorkerCondition'];
                          $SignatureTemporaryWorkerCondition1 = $row['SignatureTemporaryWorkerCondition1'];
                          $FullNameTemporaryWorkerCondition1 = $row['FullNameTemporaryWorkerCondition1'];
                          $DateTemporaryWorkerCondition1 = $row['DateTemporaryWorkerCondition1'];


On the field

div class="col-lg-4">
                            <div class="top-inputs d-grid">
                              <label for="Nationality">Nationality</label>
                               <input type="text" name="Nationality" class="form-control" value="<?php echo $Nationality ?>">

I am geting a undefined variable error


Can you anyone point out my issue?





Link to comment
Share on other sites


for a query that's expected to match data for the page to work, its an application error if it doesn't and you should setup and display a message for the visitor telling them so, rather than to blindly try to use data that doesn't exist.

don't use a loop to fetch data from a query that's expected to match at most one row of data. just fetch that single row of data and test if a row was fetched (like the 2nd query code is doing.)

you need to validate all inputs before using them. the session variable is an input to this block of code. if it doesn't contain an expected value, that's an application error and you should setup and display a message for the visitor telling them so.

don't put external, unknown, dynamic values directly into sql query statements. use a prepared query instead.

lastly, don't copy variables to other variables for nothing. just use the original variables. after you execute the single  (LEFT?) JOIN query, just fetch the data into an appropriately named php array variable, then use elements in this array variable throughout the rest of the code.

Link to comment
Share on other sites

Thanks for the replies

The page that this is on should fetch data for the fields if it exists, if it dosn't exist it should be free to be entered.

So it shouldn't find data in certain fields on candidate_paye but if not shouldn't show a error, should just show blank so a user can input them.


Link to comment
Share on other sites

if you do the things that have been suggested, you won't get undefined variable/index errors.

if there might not be a related row in the candidate_paye table, you would use a LEFT JOIN in the single query. this will result in null values for the columns you select from that table. the column (index) will exist in the fetched data. when you echo a null value in the php code, there is no output, i.e. a blank value.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.