0

我在不同的 MYSQL 表中有两组数组。这是我想做的

               What I Want TO Do
   TABLE_ONE            connect to the table.
                        get the value we want from session_id
                        THEN get the array associated with the value (session_id)
                        explode the array to get individual values.
                        NOW::::: - GO TO TABLE_TWO

   TABLE_TWO            Go straight to the first value from array (TABLE_ONE)
                        Explode the array associated with it.
                        Delete the number that's equal to the session_id
                       _____________________________________________________
                       And so fort....

更直观的解释如下: session_id = 4

表一:

   id            array1
   1             4
   2             1
   3             2,5
   4             1,3,4,5
   5             4,5

表_二:

   id            array2
   1             4,6,9,2
   2             3,7,8,2
   3             7,12,4,9
   4             1,5,4,8
   5             3,6,12,3,5,4

所以,因为session_id = 4,我们去 TABLE_ONE id 4。id-4 的数组是 1,3,4,5。所以现在我们知道 4 可以在id 1,3,4,5 of TABLE_TWO 我们现在应该分解 TABLE_TWO 的数组并4从那里删除数组。内爆数组并将新值保存到数据库。

这就是我所做的——它只会从 id-3 中删除“4”并删除 id-4 中的所有值。请帮忙!!

    $SESSION = 4;
    $depSQL = mysql_query("SELECT array1 FROM TABLE_ONE WHERE id='$SESSION' LIMIT 1"); 
while($row=mysql_fetch_array($depSQL)) { $depARRAY = $row["array1"]; }
$explodedDEP = explode(",", $depARRAY);
foreach ($explodedDEP as $key1 => $value1) {

    $stSQL = mysql_query("SELECT array2 FROM TABLE_TWO WHERE id='$value1'"); 
    while($get=mysql_fetch_array($stSQL)) { $stARRAY = $get["array2"];}
    $explodedST = explode(",", $stARRAY);
        foreach ($explodedST as $key2 => $value2) {
              if ($value2 == $SESSION) {
                      unset($explodedST[$key2]);
              }
        }
         $newST = implode(",", $explodedST);
     $sql = mysql_query("UPDATE TABLE_TWO SET array2 ='$newST' WHERE id='$value2'");

}
exit();

请帮忙!!!我真的很挣扎。我已经尝试了几个小时,但我还没有真正得到任何地方。我认为问题在于插入数据库。请帮忙。

4

2 回答 2

1

$SESSION是一个数组,你不能像这样赋值$SESSION = 4;

像这样赋值

$SESSION['id'] = 4;
if ($value2 == $SESSION['id']) {
于 2011-02-17T19:53:47.273 回答
1

您可以通过在第二个 SQL 中直接使用 array1 来避免一个循环,因为它已经是一个逗号分隔的列表。

试试这个: 编辑:测试后更新代码。

    $SESSION['ID'] = 4;

$depSQL = mysql_query("SELECT array1 FROM TABLE1 WHERE id='".$SESSION['ID']."' LIMIT 1"); 

while($row=mysql_fetch_array($depSQL)) 
{ 
    $depARRAY = $row["array1"]; 
}
$stSQL = mysql_query("SELECT id, array2 FROM TABLE2 WHERE id IN ($depARRAY)") or die("Query Error"); 
while($get=mysql_fetch_array($stSQL)) { 
    $stARRAY = $get["array2"];
         $id =  $get["id"];
    $explodedST = explode(",", $stARRAY);
    foreach ($explodedST as $key2 => $value2) {
            if ($value2 == $SESSION['ID']) {
                            unset($explodedST[$key2]);
            }
    }
    $newST = implode(",", $explodedST);
    echo $id . " " . $newST . "<BR/>" ;
    $sql = mysql_query("UPDATE TABLE2 SET array2 ='$newST' WHERE id='$id'");
}
exit();
于 2011-02-17T19:59:32.167 回答