0

I was wondering why I got an error saying Notice: Undefined variable: query_add_landdev_temp_payroll in C:\xampp\htdocs\op\ajax\ajaxLanddevNTPPayroll.php on line xx. for the past 2 weeks, it works perfect. When I tried to execute the code today, I got that kind of error. Here is my code:

$query_get_payroll = mysqli_query($new_conn, "SELECT ntp_with_ob_payroll_transaction.ntp_id, ntp_with_ob_payroll_transaction.allotment_code, ntp_with_ob_payroll_transaction.category_name, ntp_with_ob_payroll_transaction.block_number, ntp_with_ob_payroll_transaction.activity, ntp_with_ob_payroll_transaction.lot_number, ntp_with_ob_payroll_transaction.labor_cost, other_budget.quantity FROM ntp_with_ob_payroll_transaction JOIN other_budget ON ntp_with_ob_payroll_transaction.ob_id = other_budget.ob_id WHERE other_budget.transaction_id = $transaction_id AND other_budget.ob_number = '$ntp_number' AND is_payroll = 0");

while($row = mysqli_fetch_assoc($query_get_payroll)) {

    $ntp_id = $row['ntp_id'];
    $allotment_code = $row['allotment_code'];
    $category_name = $row['category_name'];
    $block_number = $row['block_number'];
    $activity = $row['activity'];
    $lot_number = $row['lot_number'];
    $labor_cost = $row['labor_cost']; //this is equivalent to unit cost
    $quantity = $row['quantity']; //this is equivalent to total percentage


    $query_add_landdev_temp_payroll = mysqli_query($new_conn, "INSERT INTO temp_payroll_landdev(ntp_id, userid, transaction_id, ntp_number, contractor_name, allotment_code, category_name, block_number, activity, lot_numbers, regular_labor, quantity) VALUES($ntp_id, $_SESSION[userid], $transaction_id, '$ntp_number', '$fullname', '$allotment_code', '$category_name', $block_number, '$activity', '$lot_number', '$labor_cost', '$quantity')");
}

if($query_add_landdev_temp_payroll) {

    echo 1;

    //database file name
    $database_file = $database.'.sql';
    $new_database_file = $new_database.'.sql';

    if(file_exists('backup/'.$new_database_file)) {

        unlink('backup/'.$new_database_file);

        //backup project database
        $command = "C:/xampp/mysql/bin/mysqldump --host=$new_host --user=$new_user --password=$new_pass $new_database > backup/$new_database_file";
        system($command);

    } else {

        //backup project database
        $command = "C:/xampp/mysql/bin/mysqldump --host=$new_host --user=$new_user --password=$new_pass $new_database > backup/$new_database_file";
        system($command);
    }
} else {

    echo 0;
}
3
  • 1
    maybe this code $row = mysqli_fetch_assoc($query_get_payroll) returned a falsy value? thus, the while loop didn't run. Commented Sep 28, 2017 at 3:32
  • @Wreigh I didn't notice that... thanks so much. Commented Sep 28, 2017 at 3:33
  • you're welcome, you can follow Naga's answer to check if the variable was set, or iCoders answer to make sure that the variable is present. Commented Sep 28, 2017 at 3:35

3 Answers 3

3

If the query doesn't return any rows, mysqli_fetch_assoc() will return false the first time, so you'll never go into the loop, and never assign to the variable. If you then try to use the variable you get that warning.

You can give it a default initial value before the loop, or you can change your test to:

if (!empty($query_add_landdev_temp_payroll))

BTW, the variable only contains the result of the INSERT in the last iteration of the loop. Maybe you should put the if block inside the loop, right after the INSERT?

Also, there's no need to do INSERT in a loop. You can do the whole thing with a single query:

INSERT INTO temp_payroll_landdev(ntp_id, userid, transaction_id, ntp_number, contractor_name, allotment_code, category_name, block_number, activity, lot_numbers, regular_labor, quantity)
SELECT ntp_with_ob_payroll_transaction.ntp_id, ntp_with_ob_payroll_transaction.allotment_code, ntp_with_ob_payroll_transaction.category_name, ntp_with_ob_payroll_transaction.block_number, ntp_with_ob_payroll_transaction.activity, ntp_with_ob_payroll_transaction.lot_number, ntp_with_ob_payroll_transaction.labor_cost, other_budget.quantity 
FROM ntp_with_ob_payroll_transaction 
JOIN other_budget ON ntp_with_ob_payroll_transaction.ob_id = other_budget.ob_id 
WHERE other_budget.transaction_id = $transaction_id AND other_budget.ob_number = '$ntp_number' AND is_payroll = 0
Sign up to request clarification or add additional context in comments.

1 Comment

this is right. it returned FALSE since my $query_get_payroll has no data. thanks so much for the answer.
1

just update the if condition like this,

if(isset($query_add_landdev_temp_payroll) && $query_add_landdev_temp_payroll) 

Comments

0

Looks like you are using a variable inside a while command. So outside that command this variable will not define. Try use $query_add_landdev_temp_payroll = null; before while command. Hope it help you!

1 Comment

I just noticed that my $query_get_payroll has no value that's why it got undefined. thanks to @Wreigh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.