【问题标题】:insert array values in a separate row在单独的行中插入数组值
【发布时间】:2014-06-28 12:08:07
【问题描述】:

在这里,我将动态数组值插入到数据库表中。它将数据完美地插入到数据库表中。但是,我需要对这段代码进行一些修改。它使用, 插入数据。但是,我想将数组值存储在单独的行中。

现在我知道了

  id  field_name

   1   aaa, bbb, ccc, ddd

但我需要

   id  field_name

   1   aaa
   2   bbb
   3   ccc
   4   ddd

我怎样才能做到这一点?

$choose_general_rules = $_POST['choose_general_rules'];
        $choose_no_of_questions = $_POST['choose_no_of_questions'];
        $choose_mark_questions = $_POST['choose_mark_questions'];

        $choose_question_name = $_POST['choose_question_name'];
        $question = array();
        foreach($choose_question_name as $value) {
         $question[] = $value;
        }
        $result_question = implode(',', $question);

        $answer_1 = $_POST['answer_1'];
        $answer_one = array();
        foreach($answer_1 as $value) {
         $answer_one[] = $value;
        }
        $result_answer_one = implode(',', $answer_one);

        $answer_2 = $_POST['answer_2'];
        $answer_two = array();
        foreach($answer_2 as $value) {
         $answer_two[] = $value;
        }
        $result_answer_two = implode(',', $answer_two);

        $answer_3 = $_POST['answer_3'];
        $answer_three = array();
        foreach($answer_3 as $value) {
         $answer_three[] = $value;
        }
        $result_answer_three = implode(',', $answer_three);

        $answer_4 = $_POST['answer_4'];
        $answer_three = array();
        foreach($answer_3 as $value) {
         $answer_three[] = $value;
        }
        $result_answer_three = implode(',', $answer_three);

        try
        {
            $stmt = $dbh->prepare("INSERT INTO choose_the_correct_answer ( reference_id, general_rules, no_of_questions, mark_each_questions, question, answer_option_one, answer_option_two, answer_option_three, answer_option_four ) VALUES ( :ref_id, :choose_general_rules, :choose_no_of_questions, :choose_mark_questions, :choose_question_name, :answer_1, :answer_2, :answer_3, :answer_4 )");
            $stmt->bindParam(':ref_id', $lastid, PDO::PARAM_INT);
            $stmt->bindParam(':choose_general_rules', $choose_general_rules, PDO::PARAM_STR);
            $stmt->bindParam(':choose_no_of_questions', $choose_no_of_questions, PDO::PARAM_STR);
            $stmt->bindParam(':choose_mark_questions', $choose_mark_questions, PDO::PARAM_STR);
            $stmt->bindParam(':choose_question_name', $result_question, PDO::PARAM_STR);
            $stmt->bindParam(':answer_1', $result_answer_one, PDO::PARAM_STR);
            $stmt->bindParam(':answer_2', $result_answer_two, PDO::PARAM_STR);
            $stmt->bindParam(':answer_3', $result_answer_three, PDO::PARAM_STR);
            $stmt->bindParam(':answer_4', $result_answer_three, PDO::PARAM_STR);
            $stmt->execute();
        }
        catch(PDOException $e)
        {
            echo "Error Inserting datas into database :" .$e->getMessage();
        }

【问题讨论】:

    标签: php mysql arrays pdo foreach


    【解决方案1】:

    您应该更改此代码中的所有内容:

    implode(',', $question);
    

    进入

    implode("\n", $question);
    

    当然一样

     implode(',', $answer_one);
    

    进入

     implode("\n", $answer_one);
    

    等等

    【讨论】:

    • 没有。它不起作用。它像aaa\nbbb\nccc\n 这样插入数据。我想将这些值存储在单独的行中...
    • 你用过 "\n" 还是 '\n' ?您需要使用“\n”来解释它是一个换行符。
    • 它转到同一 td 中的下一行。但是,这不是我所期望的。 INSERT IN A SEPARATE ROW WITH SPECIFIC AUTO ID
    • 因此您应该创建特定的表并为它们循环插入数据,而不是将数据插入到一个公共表中
    猜你喜欢
    • 2016-06-16
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2020-12-31
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多