Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.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_Html - Fatal编程技术网

PHP插入到

PHP插入到,php,html,Php,Html,我有这样一个html表单: <tr> <td colspan="2"> Mentees :<br> 1. Student No: <input name="mentee1" type="text"> <br> 2. Student No: <input name="mentee2" type="text"> <br> 3. Student No: <i

我有这样一个html表单:

<tr>
    <td colspan="2">
    Mentees :<br>
    1. Student No: <input name="mentee1" type="text">
    <br>
    2. Student No: <input name="mentee2" type="text">
    <br>
    3. Student No: <input name="mentee3" type="text">
    <br>
    4. Student No: <input name="mentee4" type="text">
    <br>
    5. Student No: <input name="mentee5" type="text">
    <br>
    6. Student No: <input name="mentee6" type="text">
    <br>
    7. Student No: <input name="mentee7" type="text">
    <br>
    8. Student No: <input name="mentee8" type="text">
    <br>
    9. Student No: <input name="mentee9" type="text">
    <br>
    10. Student No: <input name="mentee10" type="text">
    </td>
</tr>

学员:
1.学号:
2.学号:
3.学号:
4.学号:
5.学号:
6.学号:
7.学号:
8.学号:
9学号:
10学号:
用户可以输入全部或部分学员并提交

下面是我的PHP代码的一部分: (抱歉,代码太长太乱) PHP应该检查$mentee1-10 post是空的还是空的,并根据它们进行查询,但我尝试了代码,它似乎只发送$mentee1,而忽略了其余的,即使它们不是空的

if ($mentor1 != NULL || $mentor1 != "") {
    if ($mentor2 != NULL || $mentor2 != "") {
        if ($mentor3 != NULL || $mentor3 != "") {
            if ($mentor4 != NULL || $mentor4 != "") {
                if ($mentor5 != NULL || $mentor5 != "") {
                    if ($mentor6 != NULL || $mentor6 != "") {
                        if ($mentor7 != NULL || $mentor7 != "") {
                            if ($mentor8 != NULL || $mentor8 != "") {
                                if ($mentor9 != NULL || $mentor9 != "") {
                                    if ($mentor10 != NULL || $mentor10 != "") {
                                        $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1'),('$staff_no', '$mentee2'),('$staff_no', '$mentee3'),
                                        ('$staff_no', '$mentee4'),('$staff_no', '$mentee5'),('$staff_no', '$mentee6'),('$staff_no', '$mentee7'),('$staff_no', '$mentee8'),
                                        ('$staff_no', '$mentee9'),('$staff_no', '$mentee10')";
                                    }
                                }
                                else {
                                    $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1'),('$staff_no', '$mentee2'),('$staff_no', '$mentee3'),
                                        ('$staff_no', '$mentee4'),('$staff_no', '$mentee5'),('$staff_no', '$mentee6'),('$staff_no', '$mentee7'),('$staff_no', '$mentee8'),
                                        ('$staff_no', '$mentee9')";
                                }
                            }
                            else {
                                $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1'),('$staff_no', '$mentee2'),('$staff_no', '$mentee3'),
                                        ('$staff_no', '$mentee4'),('$staff_no', '$mentee5'),('$staff_no', '$mentee6'),('$staff_no', '$mentee7'),('$staff_no', '$mentee8')";
                            }
                        }
                        else {
                            $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1'),('$staff_no', '$mentee2'),('$staff_no', '$mentee3'),
                                        ('$staff_no', '$mentee4'),('$staff_no', '$mentee5'),('$staff_no', '$mentee6'),('$staff_no', '$mentee7')";
                        }
                    }
                    else {
                        $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1'),('$staff_no', '$mentee2'),('$staff_no', '$mentee3'),
                                        ('$staff_no', '$mentee4'),('$staff_no', '$mentee5'),('$staff_no', '$mentee6')";
                    }
                }
                else {
                    $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1'),('$staff_no', '$mentee2'),('$staff_no', '$mentee3'),
                                        ('$staff_no', '$mentee4'),('$staff_no', '$mentee5')";
                }
            }
            else {
                $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1'),('$staff_no', '$mentee2'),('$staff_no', '$mentee3'),('$staff_no', '$mentee4')";
            }

        }
        else {
            $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1'),('$staff_no', '$mentee2'),('$staff_no', '$mentee3')";
        }
    }
    else {
        $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1'),('$staff_no', '$mentee2')";
    }
}
else {
    $query_upload3 = "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1')";
}

if (mysqli_query($conn, $query_upload3) == TRUE) {
    echo "<strong>New record created successfully</strong><br>";
    echo "<a href=form_mentor.php>Add New Record</a>";
}
if($mentor1!=NULL | |$mentor1!=“”){
如果($mentor2!=NULL | |$mentor2!=“”){
如果($mentor3!=NULL | |$mentor3!=“”){
如果($mentor4!=NULL | |$mentor4!=“”){
如果($mentor5!=NULL | |$mentor5!=“”){
如果($mentor6!=NULL | |$mentor6!=“”){
如果($MENTORT7!=NULL | |$MENTORT7!=“”){
如果($MENTORT8!=NULL | |$MENTORT8!=“”){
如果($MENTORT9!=NULL | |$MENTORT9!=“”){
如果($mentor10!=NULL | |$mentor10!=“”){
$query_upload3=“插入导师学生(教职员工编号,学生编号)值(“$staff_编号”,“$mentee1”),(“$staff_编号”,“$mentee2”),(“$staff_编号”,“$mentee3”),
("职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号"""8"),,
("员工编号","员工编号","员工编号","员工编号","员工编号","员工编号",";
}
}
否则{
$query_upload3=“插入导师学生(教职员工编号,学生编号)值(“$staff_编号”,“$mentee1”),(“$staff_编号”,“$mentee2”),(“$staff_编号”,“$mentee3”),
("职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号"""8"),,
(“$staff_no”,“$mentee9”)”;
}
}
否则{
$query_upload3=“插入导师学生(教职员工编号,学生编号)值(“$staff_编号”,“$mentee1”),(“$staff_编号”,“$mentee2”),(“$staff_编号”,“$mentee3”),
""职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号"","职员编号"和"职员编号;
}
}
否则{
$query_upload3=“插入导师学生(教职员工编号,学生编号)值(“$staff_编号”,“$mentee1”),(“$staff_编号”,“$mentee2”),(“$staff_编号”,“$mentee3”),
("职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号""和"职员;
}
}
否则{
$query_upload3=“插入导师学生(教职员工编号,学生编号)值(“$staff_编号”,“$mentee1”),(“$staff_编号”,“$mentee2”),(“$staff_编号”,“$mentee3”),
("职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号","职员编号;
}
}
否则{
$query_upload3=“插入导师学生(教职员工编号,学生编号)值(“$staff_编号”,“$mentee1”),(“$staff_编号”,“$mentee2”),(“$staff_编号”,“$mentee3”),
("员工编号","员工编号","员工编号","员工编号","员工编号","员工编号",";
}
}
否则{
$query_upload3=“插入导师学生(教职员工编号,学生编号)值('$staff_编号','$mentee1'),('$staff_编号','$mentee2'),('$staff_编号','$mentee3'),('$staff_编号','$mentee4');
}
}
否则{
$query_upload3=“插入导师学生(教职员工编号,学生编号)值(“$staff_编号”,“$mentee1”),(“$staff_编号”,“$mentee2”),(“$staff_编号”,“$mentee3”);
}
}
否则{
$query_upload3=“插入导师学生(教职员工编号,学生编号)值('$staff_编号','$mentee1'),('$staff_编号','$mentee2')”;
}
}
否则{
$query_upload3=“插入导师_学生(教职员工编号,学生编号)值(“$staff_编号”,“$mentee1”)”;
}
if(mysqli\u查询($conn,$query\u upload3)=TRUE){
echo“已成功创建新记录”
”; 回声“; }

让我们看看我是否理解正确。您试图根据发送的值插入一个值,但它们彼此不依赖?如果是这样,我会这样做:

$arr=array();
$str=”“;
foreach($\发布为$key=>$value){
如果(isset($value)&&strlen($value)>0){
$arr[]=“(“$staff_no.”,“$key.”);
}
}
$str=内爆(',',$arr);
$query\u upload=“插入导师学生(教职员工编号、学生编号)值”$str;
echo$query\u上传;
在这里,我根据用户发送的值动态创建一个数组,然后用逗号连接它们,这样我就可以有一个像您这样的查询


试试看,你会告诉我们。

你不想在这样一个条件中做一个条件。。。 如果我明白你想做什么,你可以这样说: (这不是最好的方法,但请尝试):

$tjek_如果_ins=false;
如果($1!=“”){
mysqli_query($conn,“插入导师_学生(教职员工编号,学生编号)值(“$staff_编号”,“$mentee1”)”);
$tjek_如果_ins=true;
}
如果($2!=“”){
mysqli_query($conn,“插入导师学生(教职员工编号、学生编号)值('$staf
$tjek_if_ins = false;

if ($mentor1 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee1')");
$tjek_if_ins = true;
}
if ($mentor2 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee2')");
$tjek_if_ins = true;
}
if ($mentor3 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee3')");
$tjek_if_ins = true;
}
if ($mentor4 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee4')");
$tjek_if_ins = true;
}
if ($mentor5 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee5')");
$tjek_if_ins = true;
}
if ($mentor6 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee6')");
$tjek_if_ins = true;
}
if ($mentor7 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee7')");
$tjek_if_ins = true;
}
if ($mentor8 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee8')");
$tjek_if_ins = true;
}
if ($mentor9 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee9')");
$tjek_if_ins = true;
}
if ($mentor10 != "") {
mysqli_query($conn, "INSERT INTO mentor_student (staff_no, student_no) VALUES ('$staff_no', '$mentee10')");
$tjek_if_ins = true;
}

if ($tjek_if_ins == true) {
echo "<strong>New record created successfully</strong><br>";
echo "<a href=form_mentor.php>Add New Record</a>";
}
else {
echo "Nothing was created...";
}
<tr>
    <td colspan="2">
    Mentees :
    <?php for ($i = 1; $i <= 10; $i++) { ?>
        <br />
        <?php echo $i; ?>. Student No: <input name="mentee[]" type="text">
    <?php } ?>
    </td>
</tr>

<?php
if (isset($_POST['mentee'])) {
    $values = array();
    foreach ($_POST['mentee'] as $mentee) {
        $student_no = mysqli_real_escape_string($mentee);
        $values[] = "('$staff_no', '$student_no')";
    }

    if ($values) {
        $query = "INSERT INTO mentor_student (staff_no, student_no)
                  VALUES " . implode(',',$values);
        if (mysqli_query($conn, $query) == TRUE) {
            echo "<strong>New record created successfully</strong><br>";
            echo "<a href='form_mentor.php'>Add New Record</a>";
        }
    }
}
?>