【问题标题】:How to deal with SELECT(MAX()) in mysql inside a foreach using PHP and Codeigniter如何使用 PHP 和 Codeigniter 在 foreach 中处理 mysql 中的 SELECT(MAX())
【发布时间】:2013-08-25 02:32:25
【问题描述】:

我在foreach 循环中使用SELECT(MAX()),这是我的代码:

foreach($_POST['image_Basename'] as $key=>$image_Basename){


    $image_Title = $this->input->post('image_Title');

    $image_Category_Id = $this->input->post('image_Category_Id');


    $this->db->query("INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id)
                      SELECT 1 + coalesce((SELECT max(image_Group_Id) FROM mg_gallery), 0), '$image_Title', '$image_Basename', '$image_Category_Id'
    ");
}

问题是对于每个image_Basename,查询都会产生一个新数字。

例如,如果我得到 3 个image_Basenames,它将为这三个image_Basenames 插入 1、2 和 3。但我希望它向所有image_Basenames 插入相同的数字。

例如,如果 image_Group_Id 中的最大数字为 1,则为每个 image_Basename 添加数字 2。我怎样才能做到这一点?!我放了

SELECT 1 + coalesce((SELECT max(image_Group_Id) FROM mg_gallery

foreach 循环之外,但它不起作用!!!

下面是我自己添加的答案

【问题讨论】:

  • 你的意思是它为image_Group_Id设置了1,2,3吗?
  • 您应该在循环之前选择最大值。如果您在最大值内执行此操作,则每次都会增加 1。 “它不起作用”是什么意思?显示您尝试过的代码!
  • MAX 更改为MIN。它适用于您的代码。
  • @Anthony,例如,如果image_Group_Id 中的最大数字为1,则在image_Group_Id 中为每个新的image_Basename 添加数字2。

标签: php mysql codeigniter select


【解决方案1】:

它的工作原理:

特别感谢 Niloy Saha,我终于得到了答案,这是我使用的代码:

$getMaxValue  = $this->db->query('SELECT 1 + coalesce((SELECT MAX(image_Group_Id)), 0) AS image_Group_Id FROM mg_gallery');
if($getMaxValue->num_rows() > 0){
    $group_Id    = $getMaxValue->row_array();
    $image_Group_Id  = $group_Id['image_Group_Id'];
}else{
    $image_Group_Id  = 0;
}

foreach($_POST['image_Basename'] as $key=>$image_Basename){
    $image_Title = $this->input->post('image_Title');
    $image_Category_Id = $this->input->post('image_Category_Id');
    $this->db->query("INSERT INTO mg_gallery (image_title, image_Basename, image_Category_Id, image_Group_Id)
                      VALUES ('$image_Title', '$image_Basename', '$image_Category_Id', $image_Group_Id)
    ");
}

【讨论】:

    【解决方案2】:

    首先,您应该从不直接从POST 数组中插入值。但为了解决手头的问题,我将保留代码原样。

    您需要在开始循环之前查询MAX(image_Group_Id),而不是在循环内执行+ 1。像这样:

    $get_group_id = $this->db->query("SELECT 1 + coalesce((SELECT max(image_Group_Id) AS group_id FROM mg_gallery), 0)");
    
    $get_group_id_array = $get_group_id->fetch_assoc();
    $group_id = $get_group_id_array['group_id'];
    
    foreach($_POST['image_Basename'] as $key=>$image_Basename){
    
        $image_Title = $this->input->post('image_Title');
        $image_Category_Id = $this->input->post('image_Category_Id');
    
        $this->db->query("INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id)
                          $group_id, '$image_Title', '$image_Basename', '$image_Category_Id'
        ");
    }
    

    【讨论】:

    • 严重性:4096 消息:CI_DB_mysql_result 类的对象无法转换为字符串文件名:gallery_model.php 行号:45 这是我使用您的代码时遇到的错误!
    • 立即尝试。在发送查询和检索返回的实际值之间,我没有写出代码。我认为,您尝试在循环外检索的问题在于您没有从循环内部删除+ 1,这就是问题所在。
    • 这次的错误是:Fatal error: Call to undefined method CI_DB_mysql_result::fetch_assoc()
    • 经过一些修改,我得到了答案并发布了它
    【解决方案3】:

    已编辑 2
    试试这个,不管它是否有效,

    $maxRs  = $this->db->query('SELECT max(image_Group_Id) AS max FROM mg_gallery');
    echo $this->db->last_query();die;  #run this query in your phpmyadmin and debug it.
    if($maxRs->num_rows() > 0){
        $maxData    = $maxRs->row_array();
            echo "here :".$maxID  = $maxData['max'];die;
        }else{
            $maxID  = 0;
    }
    //echo "max : ".$maxID;die;   #check if its returning the corrent maxid or not.
    foreach($_POST['image_Basename'] as $key=>$image_Basename){
        $image_Title = $this->input->post('image_Title');
        $image_Category_Id = $this->input->post('image_Category_Id');
        $this->db->query("INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id)
                          $maxID, '$image_Title', '$image_Basename', '$image_Category_Id'
        ");
        echo $this->db->last_query();die;  #check the query its generating is correct or not and run directly at phpmyadmin
    }
    

    【讨论】:

    • 我使用您的代码时遇到的错误:错误编号:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法,以便在第 2 行的 'Array、'test'、'8e690ea3a804558066f3682f41e89637.jpg'、'1'' 附近使用 INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id) Array , '测试', '8e690ea3a804558066f3682f41e89637.jpg', '1'
    • 将上述代码更新为调试一次。它应该返回正确的 maxID 而不是数组。检查一次 maxID 是否正确生成并告诉我。
    • 打印:max : Array
    • 而不是 echo "max : ".$maxID;die;echo "max : ".print_r($maxID);die; 让我知道。
    • 不应该发生这种情况是image_Group_Id 列您正在获取auto incrementing 列吗?我已经更新了上面的答案。试试看
    【解决方案4】:

    我不完全确定您拥有什么样的数据以及您想要什么,但我会帮助您朝着正确的方向前进:

    $int_basename = (int)max($this->input->post('image_Basename'));
    $str_image_title = $this->input->post('image_Title');
    $str_image_category_id = $this->input->post('image_Category_Id');
    
    $query   = $this->db->query("SELECT max(image_Group_Id) AS max FROM mg_gallery");
    $int_max = (int)$query->row()->max;
    
    $arr_union = array();
    for($i = 1; $i <= 3; $i++)
        if ($i == 1)
            $arr_union = "SELECT " . ($int_max + $i) . " AS id";
        else $arr_union = "SELECT " . ($int_max + $i);
    
    $str_union = implode(' UNION ', $arr_union);
    
    $this->db->query("
    INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id)
    SELECT h.id, ?, ?, ?
    FROM ({$str_union}) AS h
    ", array($str_image_title, $int_basename, $str_image_category_id));
    

    这只会运行查询两次,让您的数据库免于循环查询的麻烦。我还通过$this-&gt;db-&gt;query() 转义了这些值,以避免 mysql 注入。这并不需要INSERT ... SELECT,因为INSERT ... VALUES 就足够了。

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 2013-04-17
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 2014-06-16
      • 2016-11-04
      相关资源
      最近更新 更多