如何将MULTIPLE INSERTS组合到数据库中?

时间:2016-07-04 12:10:14

标签: php mysql sql

我的提交表单工作正常,但代码中有3个插入查询。

让我解释原因。

插入查询1允许将表单上下拉菜单的选定值插入数据库表中。

插入查询2允许将文本区域框内的值插入单独的单独行

插入查询3允许插入所有其他数据。

HTML看起来像这样(它包含在代码中,但我只是包含一些代码段)...

下拉菜单:

  <select name="Name" id="">
  <?php
  while ($row = mysql_fetch_array($result)) {
  ?>
<option value="<?php echo $row['Name']; ?>"><?php echo $row['Name']; ?>     </option>
  <?php
} ?>
</select>

是的,我知道......目前它还没有使用mysqli或PDO ......我已多次被告知......

将包含多行值的textarea:

<textarea rows="10" cols="35" name="BachelorsDegrees"> </textarea>

表单上的其他所有内容都只是<input>。很简单。

PHP ...

将文本区域值插入数据库:

//Writes the information to the database

$text = trim($_POST['BachelorsDegrees']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind

foreach ($textAr as $BachelorsDegrees) {
mysql_query("INSERT INTO Colleges (BachelorsDegrees) VALUES ('$BachelorsDegrees')") or die(mysql_error()) ;

} 

插入所选的下拉菜单值:

 //if (isset ($_POST['upload']))
//{
$Name=trim($_POST['Name']);
$ins=mysql_query("INSERT INTO Colleges (Name) VALUES ('$Name')") or die(mysql_error()) ;
if($ins)
{
echo "<br>".$Name."inserted";
}
else
{
echo mysql_error();
}
//}

插入其他所有内容:

 mysql_query("INSERT INTO Colleges (schoollogo,Motto,Description,Type,ReligiousAffiliation,OtherPartnerships,GenderAdmission,TermType,EntranceExam,EntranceExamDate,TotalEnrolled,collegewebsite,ContactInfo,CampusLocations,Certifications,Diplomas,AssociateDegrees,MastersDegrees,DoctorateDegrees,SpecialDegreePrograms,Accreditation,CostofTuitionLocal,CostofTuitionForeign,Housing,AcceptanceRate,CoE,CoD) VALUES ('$schoollogo', '$Motto', '$Description', '$Type', '$ReligiousAffiliation', '$OtherPartnerships', '$GenderAdmission', '$TermType', '$EntranceExam', '$EntranceExamDate', '$TotalEnrolled', '$collegewebsite', '$ContactInfo', '$CampusLocations', '$Certifications', '$Diplomas', '$AssociateDegrees', '$MastersDegrees', '$DoctorateDegrees','$SpecialDegreePrograms','$Accreditation','$CostofTuitionLocal','$CostofTuitionForeign','$Housing','$AcceptanceRate','$CoE','$CoD')") or die(mysql_error()) ;

当插入数据库时​​,会显示多行数据 1行=学院名称(从下拉菜单中)
第二行=文本区域的度数
第3行=所有其他数据

如何将所有插入查询合并到一个查询中,以便我只有1行?

2 个答案:

答案 0 :(得分:2)

通常,您构建一个查询。

获取每个值并将它们添加到查询中(可能作为参数)。

但是,您似乎只有一个表,而不是多个表。你希望有一个大学的表格,然后是另一个表格,每个大学的每个课程都有一行。

另一种方法是让您的第一个查询返回插入行的唯一ID,然后所有其他插入也会使用ON DUPLICATE KEY UPDATE子句插入此ID。但这将是混乱和缓慢的

EDIT。

单个查询的示例。 请注意,我刚刚使用了已弃用的mysql_ *函数,但您应该切换到mysqli_ *函数或PDO 。无论是逃避数据还是使用参数化查询都非常重要。

这为学院插入一条记录,然后使用mysql_insert_id()获取插入字段的id字段(假设你有一个)。然后使用此ID将每个课程插入另一个列出每个学院课程的表格: -

<?php

$Name = mysql_real_escape_string(trim($_POST['Name']));
$schoollogo = mysql_real_escape_string(trim($_POST['schoollogo']));
$Motto = mysql_real_escape_string(trim($_POST['Motto'])); 
$Description = mysql_real_escape_string(trim($_POST['Description'])); 
$Type = mysql_real_escape_string(trim($_POST['Type'])); 
$ReligiousAffiliation = mysql_real_escape_string(trim($_POST['ReligiousAffiliation']));
$OtherPartnerships = mysql_real_escape_string(trim($_POST['OtherPartnerships'])); 
$GenderAdmission = mysql_real_escape_string(trim($_POST['GenderAdmission'])); 
$TermType = mysql_real_escape_string(trim($_POST['TermType']));
$EntranceExam = mysql_real_escape_string(trim($_POST['EntranceExam']));
$EntranceExamDate = mysql_real_escape_string(trim($_POST['EntranceExamDate']));
$TotalEnrolled = mysql_real_escape_string(trim($_POST['TotalEnrolled'])); 
$collegewebsite = mysql_real_escape_string(trim($_POST['collegewebsite'])); 
$ContactInfo = mysql_real_escape_string(trim($_POST['ContactInfo', 
$CampusLocations = mysql_real_escape_string(trim($_POST['CampusLocations'])); 
$Certifications = mysql_real_escape_string(trim($_POST['Certifications'])); 
$Diplomas = mysql_real_escape_string(trim($_POST['Diplomas'])); 
$AssociateDegrees = mysql_real_escape_string(trim($_POST['AssociateDegrees'])); 
$MastersDegrees = mysql_real_escape_string(trim($_POST['MastersDegrees'])); 
$DoctorateDegrees = mysql_real_escape_string(trim($_POST['DoctorateDegrees']));
$SpecialDegreePrograms = mysql_real_escape_string(trim($_POST['SpecialDegreePrograms']));
$Accreditation = mysql_real_escape_string(trim($_POST['Accreditation']));
$CostofTuitionLocal = mysql_real_escape_string(trim($_POST['CostofTuitionLocal']));
$CostofTuitionForeign = mysql_real_escape_string(trim($_POST['CostofTuitionForeign']));
$Housing = mysql_real_escape_string(trim($_POST['Housing']));
$AcceptanceRate = mysql_real_escape_string(trim($_POST['AcceptanceRate']));
$CoE = mysql_real_escape_string(trim($_POST['CoE']));
$CoD = mysql_real_escape_string(trim($_POST['CoD']));

$ins = mysql_query("INSERT INTO Colleges (Name, schoollogo, Motto, Description,Type, ReligiousAffiliation, OtherPartnerships, GenderAdmission, TermType, EntranceExam, EntranceExamDate, TotalEnrolled, collegewebsite, ContactInfo, CampusLocations, Certifications, Diplomas, AssociateDegrees, MastersDegrees, DoctorateDegrees, SpecialDegreePrograms, Accreditation, CostofTuitionLocal, CostofTuitionForeign, Housing,AcceptanceRate, CoE, CoD) 
                    VALUES ('$Name', 
                    '$schoollogo', 
                    '$Motto', 
                    '$Description', 
                    '$Type', 
                    '$ReligiousAffiliation', 
                    '$OtherPartnerships', 
                    '$GenderAdmission', 
                    '$TermType', 
                    '$EntranceExam', 
                    '$EntranceExamDate', 
                    '$TotalEnrolled', 
                    '$collegewebsite', 
                    '$ContactInfo', 
                    '$CampusLocations', 
                    '$Certifications', 
                    '$Diplomas', 
                    '$AssociateDegrees', 
                    '$MastersDegrees', 
                    '$DoctorateDegrees',
                    '$SpecialDegreePrograms',
                    '$Accreditation',
                    '$CostofTuitionLocal',
                    '$CostofTuitionForeign',
                    '$Housing',
                    '$AcceptanceRate',
                    '$CoE',
                    '$CoD')") or die(mysql_error()) ;
if($ins)
{
    echo "<br>".$Name."inserted";
    $CollegeId = mysql_insert_id();

    $text = trim($_POST['BachelorsDegrees']);
    $textAr = explode("\n", $text);

    foreach ($textAr as $BachelorsDegrees) 
    {
        mysql_query("INSERT INTO Colleges_Courses (CollegeId, BachelorsDegrees) 
                    VALUES (".(int)$CollegeId.", '".mysql_real_escape_string(trim($BachelorsDegrees))."')") or die(mysql_error()) ;
    }
}
else
{
    echo mysql_error();
}

如果你想使用多个插入,依次更新每个字段(这将非常慢并且在这里没有用处),那么你可以插入多次列值并在行已经存在时插入它。在这种情况下,假设您有一个自动增量ID字段: -

<?php

$Name = mysql_real_escape_string(trim($_POST['Name']));
$schoollogo = mysql_real_escape_string(trim($_POST['schoollogo']));
$Motto = mysql_real_escape_string(trim($_POST['Motto'])); 
$Description = mysql_real_escape_string(trim($_POST['Description'])); 
$Type = mysql_real_escape_string(trim($_POST['Type'])); 
$ReligiousAffiliation = mysql_real_escape_string(trim($_POST['ReligiousAffiliation']));
$OtherPartnerships = mysql_real_escape_string(trim($_POST['OtherPartnerships'])); 
$GenderAdmission = mysql_real_escape_string(trim($_POST['GenderAdmission'])); 
$TermType = mysql_real_escape_string(trim($_POST['TermType']));
$EntranceExam = mysql_real_escape_string(trim($_POST['EntranceExam']));
$EntranceExamDate = mysql_real_escape_string(trim($_POST['EntranceExamDate']));
$TotalEnrolled = mysql_real_escape_string(trim($_POST['TotalEnrolled'])); 
$collegewebsite = mysql_real_escape_string(trim($_POST['collegewebsite'])); 
$ContactInfo = mysql_real_escape_string(trim($_POST['ContactInfo', 
$CampusLocations = mysql_real_escape_string(trim($_POST['CampusLocations'])); 
$Certifications = mysql_real_escape_string(trim($_POST['Certifications'])); 
$Diplomas = mysql_real_escape_string(trim($_POST['Diplomas'])); 
$AssociateDegrees = mysql_real_escape_string(trim($_POST['AssociateDegrees'])); 
$MastersDegrees = mysql_real_escape_string(trim($_POST['MastersDegrees'])); 
$DoctorateDegrees = mysql_real_escape_string(trim($_POST['DoctorateDegrees']));
$SpecialDegreePrograms = mysql_real_escape_string(trim($_POST['SpecialDegreePrograms']));
$Accreditation = mysql_real_escape_string(trim($_POST['Accreditation']));
$CostofTuitionLocal = mysql_real_escape_string(trim($_POST['CostofTuitionLocal']));
$CostofTuitionForeign = mysql_real_escape_string(trim($_POST['CostofTuitionForeign']));
$Housing = mysql_real_escape_string(trim($_POST['Housing']));
$AcceptanceRate = mysql_real_escape_string(trim($_POST['AcceptanceRate']));
$CoE = mysql_real_escape_string(trim($_POST['CoE']));
$CoD = mysql_real_escape_string(trim($_POST['CoD']));

$ins = mysql_query("INSERT INTO Colleges (Id, Name) 
                    VALUES (NULL, '$Name')"; 
if($ins)
{
    echo "<br>".$Name."inserted";
    $CollegeId = mysql_insert_id();

    $ins = mysql_query("INSERT INTO Colleges (Id, schoollogo) 
                        VALUES (".(int)$CollegeId.", '$schoollogo') ON DUPLICATE KEY UPDATE schoollogo = VALUES(schoollogo)"; 
    $ins = mysql_query("INSERT INTO Colleges (Id, Motto) 
                        VALUES (".(int)$CollegeId.", '$Motto') ON DUPLICATE KEY UPDATE Motto = VALUES(Motto)"; 
    $ins = mysql_query("INSERT INTO Colleges (Id, Description) 
                        VALUES (".(int)$CollegeId.", '$Description') ON DUPLICATE KEY UPDATE Description = VALUES(Description)"; 
    $ins = mysql_query("INSERT INTO Colleges (Id, Type) 
                        VALUES (".(int)$CollegeId.", '$Type') ON DUPLICATE KEY UPDATE Type = VALUES(Type)"; 
//etc

    $text = trim($_POST['BachelorsDegrees']);
    $textAr = explode("\n", $text);

    foreach ($textAr as $BachelorsDegrees) 
    {
        mysql_query("INSERT INTO Colleges_Courses (CollegeId, BachelorsDegrees) 
                    VALUES (".(int)$CollegeId.", '".mysql_real_escape_string(trim($BachelorsDegrees))."')") or die(mysql_error()) ;
    }
}
else
{
    echo mysql_error();
}

答案 1 :(得分:0)

您可以使用多查询将多个SQL查询合并为一个查询。

多查询示例

$mysqli = new mysqli("example.com", "user", "password", "database");
$sql = "INSERT INTO table VALUES ('My First Table'); ";
$sql.= "INSERT INTO table VALUES ('My Second Table'); ";
$mysqli->multi_query($sql);