You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP预编译SQL返回空JSON数组:单选框下拉加载失败

解决单选按钮触发下拉列表加载空数组的问题

看起来你遇到的核心问题是SQL查询逻辑错误,导致数据库返回空结果,进而前端拿到空数组。咱们一步步来排查和修复:

1. 修正PHP中的SQL查询(最关键的问题)

你的SQL语句里的关联条件写错了——你现在是把列名当作值去匹配GR027DEVISE,而不是关联两张表的对应字段。比如当选中table_fixe时,你绑定的$secolumn是CR002_DEV_DIN,这时候原SQL的WHERE D.GR027DEVISE = ?相当于把字符串CR002_DEV_DIN和货币代码对比,肯定查不到数据。

修改后的PHP代码:

<?php 
include "connect.php";//to connect to the mysql DB 
$table =''; 
$secolumn =''; 
$currency_array=[];

if (isset($_GET["table_cond"])) { 
    $choice = $_GET["table_cond"]; 
    if ($choice == "table_fixe") { 
        $table = "cr002t_cours_fixe"; 
        $secolumn ="CR002_DEV_DIN"; 
    } elseif ($choice == "table_billets") { 
        $table = "cr004t_cours_billet"; 
        $secolumn = "CR004_DEV_BILLC"; 
    } elseif ($choice == "table_itqb") { 
        $table = "cr005t_cours_intbq_comp"; 
        $secolumn = "CR005_DEV_COMP"; 
    } else { 
        echo json_encode(['currency_array'=>$currency_array, 'error'=>"Please select a valid option"]);
        exit;
    } 
} else {
    echo json_encode(['currency_array'=>$currency_array, 'error'=>"No option selected"]);
    exit;
}

// 确保表名和列名有效后再执行查询
if (!empty($table) && !empty($secolumn)) {
    // 用JOIN语法关联两张表,正确匹配对应字段
    $sql ="SELECT DISTINCT D.GR027DEVISE, D.GR027LIB 
           FROM gr027t_devises D 
           JOIN ".$table." T ON D.GR027DEVISE = T.".$secolumn.";";
    $stmt = mysqli_stmt_init($conn); 
    if (!mysqli_stmt_prepare($stmt, $sql)) { 
        echo json_encode(['currency_array'=>$currency_array, 'error'=>"SQL statement failed"]);
        exit;
    } else { 
        mysqli_stmt_execute($stmt); 
        $result = mysqli_stmt_get_result($stmt); 
        while ($row = mysqli_fetch_assoc($result)) { 
            $id = $row['GR027DEVISE']; 
            $name = $row['GR027LIB']; 
            $currency_array[] =array("id"=>$id,"name"=>$name); 
        }
    }
}

echo json_encode(['currency_array'=>$currency_array]); 
?>

关键修改点:

  • 把错误的WHERE D.GR027DEVISE = ?改成JOIN ... ON D.GR027DEVISE = T.$secolumn,实现两张表的字段关联
  • 添加了前置判断,避免未选中单选按钮时执行无效SQL
  • 返回错误信息,方便前端调试

2. 优化前端AJAX逻辑

原来的前端代码可以做一些优化,提升稳定性和调试效率:

function checkTABLE(table_cond){
    var sel_curr_name = document.querySelector('#sel_curr_name');
    var content = '';
    
    $.ajax({
        type: 'get',
        url: '/myfolder/getcurrencylist.php',
        data: { table_cond: table_cond }, // 用data参数传递,避免URL拼接的编码问题
        dataType: 'json', // 让jQuery自动解析JSON,不用手动parse
        success: function(data){
            console.log(data.currency_array);
            // 先清空下拉列表
            sel_curr_name.innerHTML = '';
            
            if(data.currency_array.length > 0){
                $.each(data.currency_array, function(key, value){
                    // 给value.id加引号,避免特殊字符问题
                    content += '<option value="'+value.id+'">'+value.name+'</option>';
                });
                sel_curr_name.innerHTML = content;
            } else {
                sel_curr_name.innerHTML = '<option value="">No currencies found</option>';
                // 如果有错误信息,打印到控制台
                if(data.error) console.error('Load error:', data.error);
            }
        },
        error: function(xhr, status, error){
            // 添加错误处理,方便排查请求问题
            console.error('AJAX request failed:', status, error);
            sel_curr_name.innerHTML = '<option value="">Error loading data</option>';
        }
    });
}

优化点:

  • 用data参数传递参数,比URL拼接更安全
  • 设置dataType: 'json',省去手动解析JSON的步骤
  • 添加错误处理函数,请求失败时能及时反馈
  • 处理空数组的情况,给用户明确提示

3. 额外调试建议

如果还是没数据,可以试试这些方法:

  • 在PHP中执行SQL前,临时添加echo $sql; exit;,查看生成的SQL语句是否符合预期,直接在数据库客户端执行验证
  • 检查目标表(比如cr002t_cours_fixe)的对应列(CR002_DEV_DIN)是否有和gr027t_devises.GR027DEVISE匹配的数据
  • 确认connect.php的数据库连接权限,确保能访问所有涉及的表

内容的提问来源于stack exchange,提问作者Kousayla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 07:27:31