更新三个相互链接的表时出现问题

时间:2018-12-23 19:40:30

标签: php sql arrays database

我正在尝试更新数据库中彼此链接的三个表。

我的表格设置如下:

模板

id
title
id_company

questioncat

id
title
ordering
tid (id of templates)

问题

id
question
catid (id of questioncat)
ordering

templates包含我的问题列表的名称,questioncat包含我的列表中的所有类别,而questions包含所有属于该类别的问题。

插入数据效果很好,我这样做是这样的:

首先,我的PHP脚本获得了一个数组,例如:

Array
(
    [0] => Array
        (
            [name] => lijsttitle
            [value] => Lijst nieuw
        )

    [1] => Array
        (
            [name] => category[]
            [value] => cat1
        )

    [2] => Array
        (
            [name] => sortorder
            [value] => 1
        )

    [3] => Array
        (
            [name] => question[]
            [value] => q1
        )

    [4] => Array
        (
            [name] => category[]
            [value] => cat2
        )

    [5] => Array
        (
            [name] => sortorder
            [value] => 2
        )

    [6] => Array
        (
            [name] => question[]
            [value] => q1
        )

)

我检查列表是否已经存在,

$check = '
SELECT *
FROM templates
WHERE title = "'.$conn->real_escape_string($title["value"]).'"';
$checkcon = $conn->query($check);
$check = $checkcon->fetch_assoc();
// If there is more than 1 result, update data instead of inserting
if($checkcon->num_rows > 0){

// Else insert data as new list
}else{
    // Insert template title and companyid
    $inserttemplate = '
    INSERT INTO templates (title, id_company) VALUES ("'.$conn->real_escape_string($title["value"]).'","'.$conn->real_escape_string($companyid).'")';
    $inserttemplatecon = $conn->query($inserttemplate);
    $lastinserted = $conn->inserted_id();

    $currCat = '';
    $sortorder = '';

    foreach($arr as $a) {
      $val = $a['value'];
      // handle category
      if($a['name'] == 'category[]') {
        // save cat name
        $currCat = $val;
        // init questions array
        $store[$currCat] = [];
      }else if($a['name'] == 'sortorder') {
            $sortorder = $val;
        $store[$currCat]['sortorder'] = $val;
      }else {
        // add question to question array
        $store[$currCat]['question'][] = $val;
      }
    }

    array_shift($store);
    // $key is de waarde van de categorie, $lijst is een array met alles onder de categorie
    foreach($store as $keycat => $lijst){
        $sortorder = $lijst['sortorder'];

        $insertcats = '
        INSERT INTO questioncat (title, tid, ordering) VALUES ("'.$conn->real_escape_string($keycat).'", "'.$conn->real_escape_string($lastinserted).'", "'.$conn->real_escape_string($sortorder).'")';
        $insertcatscon = $conn->query($insertcats);
        $lastinserted1 = $conn->inserted_id();

        $questionarr = $lijst['question'];

            foreach($questionarr as $q){
                $insertquestions = '
                INSERT INTO questions (question, catid) VALUES ("'.$conn->real_escape_string($q).'", "'.$conn->real_escape_string($lastinserted1).'")';
                $insertquestionscon = $conn->query($insertquestions);
            }
    }

    echo 'Uw lijst is toegevoegd';
}

如果有0个结果,则该列表不存在,并将其添加到我的数据库中,这可以正常工作;但是,如果有多个结果,则表示该列表已存在,需要更新。

这是我开始遇到问题的地方。

我可以在一个查询中更新所有三个表吗?

我首先尝试在if语句(检查模板列表是否存在的语句)中使用以下代码仅更新类别名称:

foreach($arr as $a) {
    $val = $a['value'];
    // handle category
    if($a['name'] == 'category[]') {
        // save cat name
        $currCat = $val;
        // init questions array
        $store[$currCat] = [];
    }else if($a['name'] == 'sortorder') {
        $sortorder = $val;
        $store[$currCat]['sortorder'] = $val;
    }else {
        // add question to question array
        $store[$currCat]['question'][] = $val;
    }

    $updatetemplate = '
    UPDATE questioncat c
    INNER JOIN templates t
    ON c.tid = t.id
    SET t.title = "'.$conn->real_escape_string($title["value"]).'",
    c.title = "'.$conn->real_escape_string($currCat).'",
    c.ordering = "'.$conn->real_escape_string($sortorder).'"
    WHERE t.title = "'.$conn->real_escape_string($getcats['title']).'"
    AND c.id = "'.$conn->real_escape_string($getcats["id"]).'"';
    $updatetemplatecon = $conn->query($updatetemplate);
    echo 'Uw lijst is gewijzigd.';
}

但是所有类别标题都相同,顺序也一样。

是否可以在一个查询中更新三个表?它们基本上是三个级别,模板-模板下的类别-类别下的问题 有可能吗?如果可以,怎么办?

3 个答案:

答案 0 :(得分:4)

您可能想开始寻找PL / SQL过程,它们可以为您节省编码并带来更大的安全性。

偷窃:

  

在第一个表中发生事件时   可以在数据库上执行多个事件,事件可以是(ON   删除,更新或插入时),具有AFTER和BEFORE条件。

PL / SQL触发器:Source

定义

  

触发器是存储的程序,在某些事件发生时会自动执行或触发。

对于语法,它根据您所使用的DBMS有所不同。 我希望我的解释很好。

答案 1 :(得分:0)

要同时更新三个表,您应该在一个数据库事务中完成。

如果在应该执行的那些表上有一些触发器(ON UPDATE / DELETE / INSERT),则某些数据库引擎(如PostgreSQL)允许defer execution moment of triggers。如果触发器被推迟,它将在提交事务后立即执行,而且由于它,您甚至可以更新所有键类型(主键,外键,唯一键,复杂键等)。

答案 2 :(得分:0)

尝试使用单个查询更新不会使过程更简单。这将是一个非常复杂的更新查询。仍然需要一种方法来识别每个“父”(模板,类别)的“子”行(类别,问题)。

具有多个更新/插入查询的解决方案:

在您的示例中,每个表都有唯一的ID。您可以使用此ID更新特定行。在您的php数组(POST / GET?)中,您具有每个项目的名称和值,但没有ID。只需添加“ id”即可。

如果id为空=>插入新行(此项不是来自数据库)

如果定义了id =>更新具有特定id的行(此项目已经存在,可能已更改)

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => lijsttitle
            [value] => Lijst nieuw
        )

    [1] => Array
        (
            [id] => 17
            [name] => category[]
            [value] => cat1
        )

    [2] => Array
        (
            [name] => sortorder
            [value] => 1
        )

    [3] => Array
        (
            [id] => 35
            [name] => question[]
            [value] => q1
        )

    [4] => Array
        (
            [id] => 
            [name] => category[]
            [value] => cat2
        )

    [5] => Array
        (
            [name] => sortorder
            [value] => 2
        )

    [6] => Array
        (
            [id] => 
            [name] => question[]
            [value] => q1
        )

)
相关问题