我想知道为什么我在
$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;
}
答案 0 :(得分:3)
如果查询没有返回任何行,mysqli_fetch_assoc()
将在第一次返回false
,因此您永远不会进入循环,也不会分配给变量。如果您尝试使用该变量,则会收到该警告。
您可以在循环之前为其指定默认初始值,也可以将测试更改为:
if (!empty($query_add_landdev_temp_payroll))
顺便说一下,变量只包含循环最后一次迭代中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
答案 1 :(得分:1)
只需像这样更新if
条件,
if(isset($query_add_landdev_temp_payroll) && $query_add_landdev_temp_payroll)
答案 2 :(得分:0)
看起来你在while
命令中使用了一个变量。所以在该命令之外,这个变量不会定义。尝试在$query_add_landdev_temp_payroll = null;
命令之前使用while
。希望对你有帮助!