2017-09-28 86 views
0

我想知道爲什麼我得到了一個錯誤說注意:未定義的變量:query_add_landdev_temp_payroll在C:\ XAMPP \ htdocs中\上線XX運\ AJAX \ ajaxLanddevNTPPayroll.php。在過去的兩週裏,它的工作非常完美。當我今天試圖執行代碼時,我遇到了那種錯誤。這裏是我的代碼:當它的外循環時未定義的變量 - PHP

$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; 
} 
+1

也許這個代碼'$行= mysqli_fetch_assoc($ query_get_payroll)'返回的* falsy *值?因此,while循環沒有運行。 – Wreigh

+0

@Wreigh我沒有注意到...非常感謝。 –

+0

不用客氣,您可以按照Naga的回答來檢查* variable *是否已設置,或者iCoders是否回答以確保*變量*存在。 – Wreigh

回答

3

如果查詢沒有返回任何行,mysqli_fetch_assoc()將首次返回false,因此您將永遠不會進入循環,並且永遠不會分配給變量。如果你然後嘗試使用該變量,你會得到該警告。

你可以給它一個默認的初始值的循環之前,也可以將您的測試更改爲:

if (!empty($query_add_landdev_temp_payroll)) 

BTW,該變量只包含在循環的最後一次迭代的INSERT的結果。也許你應該把if塊放在循環中,在INSERT之後?

另外,在循環中不需要做INSERT。你可以做整個事情有一個查詢:

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 
+0

這是對的。它返回FALSE,因爲我的$ query_get_payroll沒有數據。非常感謝答案。 –

1

剛剛更新if條件就是這樣,

if(isset($query_add_landdev_temp_payroll) && $query_add_landdev_temp_payroll) 
+0

非常感謝。 –

0

看起來像您使用的是while命令中的變量。所以在這個命令之外,這個變量不會定義。嘗試在while命令前使用$query_add_landdev_temp_payroll = null;。希望它能幫助你!

+0

我只注意到我的$ query_get_payroll沒有值,這就是爲什麼它沒有定義。感謝@Wreigh –