0

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

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,它将为这三个插入 1、2 和 3 image_Basenames。但我希望它向所有image_Basenames.

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

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

foreach循环之外,但它没有工作!!!

答案是我自己在下面添加的

4

4 回答 4

1

EDITED 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
}
于 2013-08-22T11:44:15.420 回答
0

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

$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->db->query()以避免 mysql 注入。这并不真正需要INSERT ... SELECTINSERT ... VALUES足够了。

于 2013-08-22T11:45:02.430 回答
0

首先,您永远不应该直接从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'
    ");
}
于 2013-08-22T11:46:31.600 回答
0

有用:

特别感谢 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)
    ");
}
于 2013-08-22T20:24:02.803 回答