php - 如何在表中插入章节数行

标签 php mysql

我的问题表有 584 行,即 584 个问题,章节表中的章节表有类型列,我为不同类型添加了章节,即 18 章用于类型 1,18 章用于类型 2 和 18类型3的章节,因此章节的id不同。

现在我在类型 1 的问题表中添加了 584 个问题,现在我想在同一问题表中添加这 584 个问题,但对于类型 2,问题表有 Chapter_id 列,因此 Chapter_id 为 80 到 97。如果我们将 584 除以章节编号(即 18),则每一章的结果为 32,因此对于每一章,将添加 32 个问题和章节 ID,例如前 32 章的章节 ID 为 80,其他章节的 ID 为 81,依此类推.

但是 18*32 = 576 和 584-576= 08,所以按照我的逻辑,8 个问题不会插入到表中,但是如果我使用 foreach 章节 id 变量,我不会获得增量。

    <?php

require 'Database.php';


$dbh = new PDO('mysql:host=localhost;dbname=airman_handbook','siddhi', 'siddhi');

$sth = $dbh->prepare("SELECT * FROM `questions`");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);

$count = $sth->rowCount();


$chapterId = 80;
$i = 0;
foreach ($result as $chapter) {

    while ($i <= 32)
    {
        $stmt = $dbh->prepare("INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer,type,chapterId) VALUES (?, ?, ?, ?, ?, ?, ?,?)");
        $stmt->execute(array($result['question'], $result['answer_a'], $result['answer_b'], $result['answer_c'], $result['answer_d'], $result['answer'], 2, $chapterId));
    }
    $i++;
    $chapterId ++;
}

?>

Chapter table :

id
title
type

33
Ch 1: Air Force heritage
1

34
Ch 2: Enlisted History
1

35
Ch 3: Organization
1

36
Ch 4: Air Force doctrine, AEF, Joint force
1

37
Ch 5: Emergency management
1

38
Ch 6: Standards of conduct
1

39
Ch 7: Enforcing standards and legal issues
1

40
Ch 8: Military customs, courtesies, and protocol f...
1

41
Ch 9: Enlisted force development
1

42
Ch 10: Leadership
1

43
Ch 12: Training and education
1

44
Ch 13: Resource management
1

45
Ch 14: Communicating in today's Air Force
1

46
Ch 15: Personnel programs
1

47
Ch 16: Wing support
1

48
Ch 17: Dress and appearance
1

49
Ch 18: Fit force
1

50
Ch 19: Security
1

80
Ch 1: Air Force heritage
2

81
Ch 2: Enlisted History
2

82
Ch 3: Organization
2

83
Ch 4: Air Force doctrine, AEF, Joint force
2

84
Ch 5: Emergency management
2

85
Ch 6: Standards of conduct
2

86
Ch 7: Enforcing standards and legal issues
2

87
Ch 8: Military customs, courtesies, and protocol f...
2

88
Ch 9: Enlisted force development
2

89
Ch 10: Leadership
2

90
Ch 12: Training and education
2

91
Ch 13: Resource management
2

92
Ch 14: Communicating in today's Air Force
2

93
Ch 15: Personnel programs
2

94
Ch 16: Wing support
2

95
Ch 17: Dress and appearance
2

96
Ch 18: Fit force
2

97
Ch 19: Security
2

98
Ch 1: Air Force heritage
3

99
Ch 2: Enlisted History
3

100
Ch 3: Organization
3

101
Ch 4: Air Force doctrine, AEF, Joint force
3

102
Ch 5: Emergency management
3

103
Ch 6: Standards of conduct
3

104
Ch 7: Enforcing standards and legal issues
3

105
Ch 8: Military customs, courtesies, and protocol f...
3

106
Ch 9: Enlisted force development
3

107
Ch 10: Leadership
3

108
Ch 12: Training and education
3

109
Ch 13: Resource management
3

110
Ch 14: Communicating in today's Air Force
3

111
Ch 15: Personnel programs
3

112
Ch 16: Wing support
3

113
Ch 17: Dress and appearance
3

114
Ch 18: Fit force
3

115
Ch 19: Security
3


Questions :


id
question
answer_a
answer_b
answer_c
answer_d
answer
type
chapterId


56
_______ achieved the first powered, sustained, con...
Otto Lilienthal
Orville and Wilbur Wright
The Tuskegee Airmen
Lieutenant Thomas E. Selfridge
B
1
24


57
_______ achieved the first powered, sustained, con...
Otto Lilienthal
Orville and Wilbur Wright
The Tuskegee Airmen
Lieutenant Thomas E. Selfridge
B
1
33


58
The U.S. and ___________ signed the Anti-Ballistic...
Iran
North Korea
the Union of Soviet Socialist Republics
Vietnam War
C
1
33


59
The purpose of Operation Desert Shield was to: (Ch...
defend Saudi Arabia and Persian Gulf states
respond to the attack on the U.S. Embassy in Iran
avoid a repetition of the Iranian hostage crisis
all of the above
A
1
24

编辑:

require 'Database.php';

ini_set('max_execution_time', 300);

$dbh = new PDO('mysql:host=localhost;dbname=airman_handbook','siddhi', 'siddhi');

$sth = $dbh->prepare("SELECT * FROM `questions`");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);

$count = $sth->rowCount();

$chapterId = 80;
$i = 0;

foreach ($result as $chapter) {

    while ($i <= 32)
    {
        $stmt = $dbh->prepare("INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer,type,chapterId) VALUES (?, ?, ?, ?, ?, ?, ?,?)");
        $stmt->execute(array($result['question'], $result['answer_a'], $result['answer_b'], $result['answer_c'], $result['answer_d'], $result['answer'], 2, $chapterId));

     //   $count = $stmt->rowCount();

        if($stmt)
        {
            echo 'row inserted';
        }
    }
    $i++;
    if($i > 32)
    {
        $i = 0;
    }
    $chapterId ++;
    if($chapterId > 91)
    {
        $chapterId = 80;
    }
}

?>

我能为此做什么?请帮忙谢谢。

最佳答案

经过讨论,下面将复制问题,并为每个章节分配(大致)相同的编号。

require 'Database.php';

ini_set('max_execution_time', 300);

$dbh = new PDO('mysql:host=localhost;dbname=airman_handbook','siddhi', 'siddhi');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $dbh->prepare("SELECT * FROM `questions` WHERE `type` = 1 ORDER BY `id`;");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);

$count = $sth->rowCount();

$StartChapterId = 79;   //  Will update first through foreach, so it should be 1 less than the 1st ID.
$NumChapters = 18;
$questionsPerChapter = ceil(584 / $NumChapters);
$chapterId = 0;
$num_inserted = 0;

foreach($result as $rowno => $chapter) {
    if($rowno % $questionsPerChapter === 0) {
        $chapterId++;
        echo "<li>".$num_inserted." inserted in chapterId: ".$chapterId + $StartChapterId."</li>\n";
        $num_inserted = 0;
    }
    $stmt = $dbh->prepare("INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer,type,chapterId) VALUES (?, ?, ?, ?, ?, ?, ?,?)");
    $stmt->execute(array($chapter['question'], $chapter['answer_a'], $chapter['answer_b'], $chapter['answer_c'], $chapter['answer_d'], $chapter['answer'], 2, $chapterId + $StartChapterId));
    $num_inserted++;
}
echo "<li>".$num_inserted." inserted in chapterId: ".$chapterId + $StartChapterId."</li>\n";

作为替代方案,这里有一个 SQL 语句,它将根据 chapters 表中的内容进行复制。

这是一个 SQL 语句,它应该复制问题,并根据您的要求更改类型和 ChapterId。

INSERT INTO `questions`
(`question`,
    `answer_a`,
    `answer_b`,
    `answer_c`,
    `answer_d`,
    `answer`,
    `type`,
    `chapterId`)
SELECT `question`,
    `answer_a`,
    `answer_b`,
    `answer_c`,
    `answer_d`,
    `answer`,
    c2.`type` as `type`,
    c2.`id` as `chapterId`
FROM `questions` q
JOIN `chapters` c1
ON c1.`id` = q.`chapterId` AND c1.`type` = 1
JOIN `chapters` c2
ON c2.`title` = c1.`title` AND c2.`id` <> c1.`id`
WHERE q.`type` = 1
ORDER BY c2.`type`, c2.`id`, q.`id`;

关于php - 如何在表中插入章节数行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298295/

相关文章:

php - 无法获取 php.ini 的更新值

php - Eloquent - 从登录用户获取特定列

mysql - 1114(HY000): The table is full

php - 如何使用 PHP 从列中回显值?

php - Qt/c++相当于php例程

php - 使用 $_GET 接收单选按钮的标签

php - 如何提高bemusic服务器文件上传限制?

php - 渲染 Highcharts 的问题 - 通过 PHP/MySQL 填充

mysql - 数据库设计 - 建议。标准化

php - 从时间戳中选择唯一的年份 - 结果不符合预期