Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组值存储在单独的行中_Php_Mysql_Arrays_Pdo_Foreach - Fatal编程技术网

Php 将数组值存储在单独的行中

Php 将数组值存储在单独的行中,php,mysql,arrays,pdo,foreach,Php,Mysql,Arrays,Pdo,Foreach,这里,我将动态数组值插入数据库表。它完美地将数据插入到数据库表中。但是,我需要对这段代码进行一些修改。它使用,插入数据。但是,我想将数组值存储在一个单独的行中 现在我有: id field_name 1 aaa, bbb, ccc, ddd 但我需要: id field_name 1 aaa 2 bbb 3 ccc 4 ddd 我怎样才能做到这一点 $choose_general_rules = $_POST['choose

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

现在我有:

  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();
}

您需要为insert执行一个循环以获得多个insert。此外,内爆将数组转换为字符串。您需要循环数组并使用$value变量作为bindParam()方法的输入参数

您还双重初始化了一个变量

$answer_4 = $_POST['answer_4'];
        $answer_three = array();
        foreach($answer_3 as $value) {
         $answer_three[] = $value;
        }
上面的代码是

$answer_3 = $_POST['answer_3'];
        $answer_three = array();
        foreach($answer_3 as $value) {
         $answer_three[] = $value;
        }
        $result_answer_three = implode(',', $answer_three);
添加的结果是一个逗号分隔的字符串。内爆将数组转换为带除雾器的字符串


您需要一个循环来插入x次数据,但也需要循环每个问题数组。如果没有完整的代码,我只能建议您。

如果需要多次运行,请插入多条记录。插入/执行4次,或者
插入到foo(a)值('foo')、('bar')等中。
mysql语法扩展