将数组插入数据库

时间:2015-02-11 14:16:15

标签: php arrays mysqli

我使用此代码将问题与选项分组。

<?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 
        )

)

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

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 个答案:

答案 0 :(得分:1)

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

serialize() serialize
json_encode() json_encode

要么效果很好; serialize()会将数组转换为可以保存的字符串;要在以后检索您的数组,请将该字符串传递给unserialize()函数 json_encode()还附带json_decode()功能。要将数据作为数组[而不是此场景中的对象]读取,您必须这样做:
$questions = json_decode($string_from_database, true);

我希望这会有所帮助。

答案 1 :(得分:1)

在您设置规范化数据结构时,我会使用json_encode()

以下是您需要做的细分;

  • 将问题插入questions
  • 抓住last_insert_id并将其存储在变量
  • 将答案插入answers
  • 使用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.

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

相关问题