将数组插入数据库

Inserting array to database

我使用此代码将问题与选项组合在一起。

<?php 

$entries = preg_split('/(?=[a-z\d]+\.(?!\d))/', $str, -1, PREG_SPLIT_NO_EMPTY); 

  $questions = array();
  $currentQuestion = null;
  $id = 0;

  foreach($entries as $entry) {
    if(is_numeric(substr($entry, 0, 1)) === true) {
        $currentQuestion = $entry;
        $questions[$entry] = array();
        $id++;
        // echo "INSERT INTO question (id, q_name) VALUES ($id, $currentQuestion)"."<br>";
        // mysqli_query($con, "INSERT INTO question (id, q_name) VALUES (NULL, '$currentQuestion')");
        continue;

    }

    // mysqli_query($con, "INSERT INTO answers (id, choices, question, correct) VALUES (NULL, 'choices', $id , 0);");
    // echo "INSERT INTO answers (id, choices, question, correct) VALUES (NULL, 'choices', $id , 'stuff')"."<br>";
    $questions[$currentQuestion][] = $entry;
  }

这是数组的结果。

Array
(
    [1. What is love?] => Array
        (
            [0] => a. Haddaway

            [1] => b. Haxxaway

            [2] => c. Hassaway

            [3] => d. Hannaway

        )

    [2. What is love? ] => Array
        (
            [0] => a. Haddaway

            [1] => b. Haxxaway

            [2] => c. Hassaway

            [3] => d. Hannaway

        )

    [3. What is love 1.1? ] => Array
        (
            [0] => a. Haddaway

            [1] => b. Haxxaway

            [2] => c. Hassaway

            [3] => d. Hannaway

        )

    [4. What is love? ] => Array
        (
            [0] => a. Haddaway

            [1] => b. Haxxaway

            [2] => c. Hassaway

            [3] => d. Hannaway 
        )

)

这是我的数据库结构:table answers 中的 question 列是 questions table 中的主键,它将确定哪个问题选择属于...

questions
+-------+--------------------------+
|  id   | q_name                   | 
+-------+--------------------------+
|   1   |   1.) What is foo?       |
|   2   |   2.) What is foo?       |
+-------+--------------------------+

answers
+-------+-------------+-----------------------+
|   id  | choices     |  question | correct   | 
+-------+-------------+-----------------------+
|   1   |   a. foo1   | 1         |   0       | 
|   2   |   b. foo2   | 1         |   0       |
|   3   |   c. foo3   | 1         |   1       |
|   4   |   a. foo3   | 2         |   0       |
|   5   |   b. foo2   | 2         |   1       |
|   6   |   c. foo1   | 2         |   0       |
+-------+-------------+-----------------------+

我设法将问题插入到数据库中,但我在插入选项时遇到了问题,因为我对应该如何对 $questions 做些什么来获得选项感到困惑...

任何建议都可以!

要存储数组,您需要将其作为字符串写入数据库。我想到了两 (2) 个函数:

serialize() serialize
json_encode() json_encode

两者都很好; serialize() 会将数组转换为字符串,然后您可以保存该字符串;要稍后检索您的数组,请将字符串传递给 unserialize() 函数。
json_encode() 也有附带的 json_decode() 功能。要将您的数据作为数组 [而不是本方案中的对象] 读取,您必须这样做:
$questions = json_decode($string_from_database, true);

希望对您有所帮助。

不会为此使用json_encode(),因为您正在设置规范化数据结构。

这是您需要做的事情的细目分类;

  • 将问题插入 questions
  • 抓取 last_insert_id 并将其存储在变量中
  • 将答案插入 answers
  • Link问题的答案使用last_insert_id

现在进入代码。

收集数据

$arrAnswers = array();
$arrQuestions = array();
$id = 0; //Assuming your table is empty

foreach($entries as $entry) { //Loop through the grabbed records
  if(is_numeric(substr($entry, 0, 1)) === true) { //Is it a question?
     $id++;     
     $arrAnswers[$id] = array();
     $arrQuestions[$id] = '(\''. $entry .'\')'; 
  } else { //Ok, it's a possible answer to a question
     $arrAnswers[] = '(\''. $entry .'\', '. $id .', 0)';
  }
}

插入问题

现在我们有了一个包含问题所有答案的数组。数组键将是数据库中的问题 ID。我们现在可以插入问题了;

$strDbQuery = "INSERT INTO `questions` (`q_name`) VALUES ". implode(", ", $arrQuestions);
// Execute the query.

插入答案

现在您已插入问题,现在可以插入答案了。

$strDbQuery = "INSERT INTO `answers` (`choices`, `question`, `correct`) VALUES ". implode(", ", $arrAnswers);
// Execute the query.

因为您的数组(在您的问题中)没有包含指示答案是否正确的值,所以您必须手动执行此操作。