dqls67891 2017-09-28 03:29
浏览 70
已采纳

在循环外面的未定义变量 - PHP

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条回答 默认 最新

  • dongraobei6719 2017-09-28 03:36
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路