steveb1471 Posted March 3, 2022 Share Posted March 3, 2022 Hi 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']; $Generated_pdf=$row['Generated_pdf']; } 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 ?>"> </div> I am geting a undefined variable error Can you anyone point out my issue? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/314567-warning-undefined-variable/ Share on other sites More sharing options...
Barand Posted March 3, 2022 Share Posted March 3, 2022 My guess would be that you didn't find a matching record in candidate_paye in your second query. Don't run two queries, use a single query with a JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/314567-warning-undefined-variable/#findComment-1594199 Share on other sites More sharing options...
mac_gyver Posted March 3, 2022 Share Posted March 3, 2022 additionally, 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. Quote Link to comment https://forums.phpfreaks.com/topic/314567-warning-undefined-variable/#findComment-1594200 Share on other sites More sharing options...
steveb1471 Posted March 3, 2022 Author Share Posted March 3, 2022 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. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/314567-warning-undefined-variable/#findComment-1594202 Share on other sites More sharing options...
mac_gyver Posted March 3, 2022 Share Posted March 3, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314567-warning-undefined-variable/#findComment-1594203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.