将值插入关系数据库

发布于 2025-01-02 22:36:11 字数 2145 浏览 1 评论 0原文

我正在尝试通过 PHP 将一些数据插入到关系数据库(使用 InnoDB 引擎的 MySQL)中,其中包含下面的表和列。

  • Quiz(id, name)
  • Questions(quiz_id, id, name)
  • Answers(question_id, id, name)

主键 id 都是自增的。

这将使我能够搜索特定的测验,然后获取属于该测验的问题以及(反过来)属于这些问题的多项选择可能的答案。每个问题至少有两个可能的答案。

我正在创建一个允许用户创建新测验的界面。我已经看到了如何使用事务和 LAST_INSERT_ID() 来确保主键匹配的示例,但是当存在依赖于先前“父级”的多个插入时,无法使其正常工作(可能的答案必须链接到正确的问题)。

即过程将是:

  1. 插入新测验

  2. 使用测验 id 插入问题

  3. 使用测验 id 插入问题

    使用问题 id 插入该问题的所有可能答案

  4. 重复步骤 2 和 3 3 直到所有问题和答案都已输入,然后提交事务

我已尝试在 PHP 中概述这一点)。问题和答案通过 POST 作为字符串提供,以逗号分隔。我使用explode()将它们转换为数组,然后将变量分配给数组中的每个值。我已经开始使用 for 循环,我认为它原则上适用于问题,但不适用于答案。

$dbc = @mysqli_connect($host, $user, $password, $db) or die ('Could not connect to MySQL: ' . mysqli_connect_error());


$qN= ($_POST['quizName']); 
$quizName = mysqli_real_escape_string($qN);


$qu = ($_POST['question']);  
$question = mysqli_real_escape_string($qu);

$questionArray = explode(',', $question);
$numberQuestions = count($questionArray);

$i = 1;

foreach ($questionArray as $variable)
{ 
  ${'q_'.$i} = $variable;     
  ++$i;
}   

$an = ($_POST['answer']); 
$answer = mysqli_real_escape_string($an);

$answerArray = explode(',', $answer);
$numberAnswers = count($answerArray);

$j = 1;

foreach ($answerArray as $variable)
{ 
  ${'a_'.$j} = $variable;     
  ++$j;
}

//turn off AUTOCOMMIT, then run the required queries
$dbc->autocommit(FALSE);

$dbc->query("INSERT INTO Quiz(name) 
VALUES
(".$quizName.")");

$quizID = $dbc->insert_id;

$n = 1;

for ($x=0; $x<$numberQuestions; $x++)
{
  $dbc->query("INSERT INTO Question (quiz_id, question_name) 
  VALUES
  (".$quizID.",".${'q_'.$n}.")");

  $questionID = $dbc->insert_id;
  echo $questionID;
  ++$n;

  $m = 1;

  for ($y=0; $y<$numberAnswers; $y++)
  {
    $dbc->query("INSERT INTO Answer(question_id, name) 
    VALUES
    (".$questionID.",".${'a_'.$m}.")");

    ++$m;
  }
}
// commit transaction
$dbc->commit();
// close connection
$dbc->close();

我非常感谢您能提供的任何帮助 - 如果您需要更多信息或任何说明,请告诉我。

I'm trying to insert some data into a relational database (MySQL using InnoDB engine) with the tables and columns below via PHP.

  • Quiz(id, name)
  • Questions(quiz_id, id, name)
  • Answers(question_id, id, name)

The primary key ids are all auto-increment.

This will allow me to search for a particular quiz, then get the questions belonging to that quiz and (in turn) the multiple choice possible answers belonging to those questions. Each question will have at least two possible answers.

I'm creating an interface that will allow users to create a new quiz. I've seen examples of how you can use transactions and LAST_INSERT_ID() to ensure that the primary keys match up, but cannot get this to work when there are multiple inserts that are dependent on the previous 'parent' (the possible answers must be linked to the correct question).

i.e. The process will be:

  1. Insert new quiz

  2. Use the quiz id to insert a question

  3. Use the question id to insert all possible answers to that question

  4. Repeat steps 2 & 3 until all questions and answers have been entered, then commit the transaction

I've attempted to outline this in PHP. The questions and answers are provided via POST as strings, separated by commas. I use explode() to convert these into arrays, and then assigned variables to each value in the array. I've started using for loops, which I think will work in principle for the questions, but not the answers.

$dbc = @mysqli_connect($host, $user, $password, $db) or die ('Could not connect to MySQL: ' . mysqli_connect_error());


$qN= ($_POST['quizName']); 
$quizName = mysqli_real_escape_string($qN);


$qu = ($_POST['question']);  
$question = mysqli_real_escape_string($qu);

$questionArray = explode(',', $question);
$numberQuestions = count($questionArray);

$i = 1;

foreach ($questionArray as $variable)
{ 
  ${'q_'.$i} = $variable;     
  ++$i;
}   

$an = ($_POST['answer']); 
$answer = mysqli_real_escape_string($an);

$answerArray = explode(',', $answer);
$numberAnswers = count($answerArray);

$j = 1;

foreach ($answerArray as $variable)
{ 
  ${'a_'.$j} = $variable;     
  ++$j;
}

//turn off AUTOCOMMIT, then run the required queries
$dbc->autocommit(FALSE);

$dbc->query("INSERT INTO Quiz(name) 
VALUES
(".$quizName.")");

$quizID = $dbc->insert_id;

$n = 1;

for ($x=0; $x<$numberQuestions; $x++)
{
  $dbc->query("INSERT INTO Question (quiz_id, question_name) 
  VALUES
  (".$quizID.",".${'q_'.$n}.")");

  $questionID = $dbc->insert_id;
  echo $questionID;
  ++$n;

  $m = 1;

  for ($y=0; $y<$numberAnswers; $y++)
  {
    $dbc->query("INSERT INTO Answer(question_id, name) 
    VALUES
    (".$questionID.",".${'a_'.$m}.")");

    ++$m;
  }
}
// commit transaction
$dbc->commit();
// close connection
$dbc->close();

I'd really appreciate any help you can give - please let me know if you need any more information or any clarifications.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住我的手 2025-01-09 22:36:11
$dbc->query("INSERT INTO Question (quiz_id, question_name) 
  VALUES
  (".$surveyID.",".${'q_'.$n}.")");

应该更改为:

$dbc->query("INSERT INTO Question (quiz_id, question_name) 
  VALUES
  (".$quizID.",".${'q_'.$n}.")");

正如代码中前面的那样,您所做的:

$quizID = $dbc->insert_id;

事实上,在代码中的其他任何地方都找不到 $surveyID

$dbc->query("INSERT INTO Question (quiz_id, question_name) 
  VALUES
  (".$surveyID.",".${'q_'.$n}.")");

Should be changed to:

$dbc->query("INSERT INTO Question (quiz_id, question_name) 
  VALUES
  (".$quizID.",".${'q_'.$n}.")");

as earlier in the code, you do:

$quizID = $dbc->insert_id;

in fact, $surveyID, isn't found anywhere else in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文