Explode/foreach 无法将复选框中的多个值记录到数据库中的多行中

Explode/foreach not working to record multiple values from checkbox into multiple rows in database

我目前正在开发一个在线青年导师门户网站,该门户网站需要导师和学员注册。 导师注册表单有一个复选框区域,他们可以在其中单击他们希望指导的类别。他们可以单击任意多个。我的问题如下:

1) 尽管将其记为一个数组,并在使用 $_POST 时使用内爆,在插入数据库时​​使用爆炸和 foreach,但我似乎只在复选框中选择的第一个选项被记录在数据库中。请帮我解决这个问题。

2) 对于这个特定的脚本我有 3 个 table; Mentor、Category 和 Logged Category 用于记录数据以用于规范化目的。除了问题编号 (1) 之外,导师 table 似乎每次注册都会获得 3 个重复的行条目。可能是什么问题?

以下是表格的摘录:

<form id="form1" name="form1" action="registration.php" method=POST>

<label for="name">Full name:</label>
<input type="text" id="name" name="name">
          
<label for="mail">Email:</label>
<input type="email" id="mail" name="email">
          
<label for="password">Password:</label>
<input type="password" id="password" name="pass">
          
<label>Gender:</label>
<input type="radio" id="m" value="m" name="gender"><label for="male" class="light">Male</label><br>
<input type="radio" id="f" value="f" name="gender"><label for="female" class="light">Female</label>

<label for="bio">Biography:</label>
<textarea id="bio" name="bio"></textarea>
        
<label>Category to Mentor in:</label><br>
<input type="checkbox" id="ICT" value="1" name="catID[]">         
<label class="light" for="ICT">ICT</label><br>

<input type="checkbox" id="Music" value="2" name="catID[]">
<label class="light" for="Music">Music</label><br>

<input type="checkbox" id="Politics" value="3" name="catID[]">
<label class="light" for="Politics">Politics</label><br>

<input type="checkbox" id="Entrepreneurship" value="4" name="catID[]">
<label class="light" for="Entrepreneurship">Entrepreneurship</label><br>

<input type="checkbox" id="Sports" value="5" name="catID[]">
<label class="light" for="Sports">Sports</label><br>

<input type="checkbox" id="Religious" value="6" name="catID[]">
<label class="light" for="Religious">Religious</label><br>

<input type="checkbox" id="Agriculture" value="7" name="catID[]">
<label class="light" for="Agriculture">Agriculture</label><br>

<input type="checkbox" id="Finance" value="8" name="catID[]">
<label class="light" for="Finance">Finance & Banking</label><br>

<input type="checkbox" id="Leadership" value="9" name="catID[]">
<label class="light" for="Leadership">Leadership</label><br>

<input type="checkbox" id="Science" value="10" name="catID[]">
<label class="light" for="Science">Science</label><br>

<input type="checkbox" id="Fashion" value="11" name="catID[]">
<label class="light" for="Fashion">Fashion/Beauty</label><br>

<input type="checkbox" id="Medical" value="12" name="catID[]">
<label class="light" for="Medical">Medical</label><br>
          

<input type="submit" name="submit" class="submit" value="Sign Up">
</form>

这是我的registration.php

<?php

$a = $_POST['name'];
$b = $_POST['email'];
$c = $_POST['pass'];
$d = $_POST['gender'];
$e = $_POST['bio'];
$fimp= implode(', ', $_POST['catID']);
if($a && $b && $c && $d && $e && $fimp) {

    if ( filter_var(($_POST["email"]), FILTER_VALIDATE_EMAIL)  == TRUE) {
        $con=mysqli_connect('localhost','root','');
        if(!$con) {
            die('Error in connection'.mysqli_error());
        }else {
            mysqli_select_db($con,'ymp');

            $query = mysqli_query($con, "select * from mentor where email = '".$b."'");
            if (!$query) {
                die ("Failed to query database" . mysqli_error($con));
            }
            if(mysqli_num_rows($query) > 0) {
                echo "<div ><h3>Email has already been used</h3><br/>Click <a href='mentorsignup.php'>here</a> to try again</div>";
            }else{
                $sql = ("INSERT INTO mentor VALUES(DEFAULT,'$a','$b','$c','$d','$e')");
                $result = mysqli_query($con, $sql);

                // insertion to logged category table
                $fs = explode(', ', $fimp);
                foreach ($fs as $f ){
                    mysqli_query($con, "INSERT INTO logged_cat 
                                        (mentorID, catID) 
                                VALUES (LAST_INSERT_ID(), '$f')");
                }
                $result = mysqli_query($con, $sql);

                if(!mysqli_query($con, $sql)){
                    die('Could not register'.mysqli_error($con));
                }else{
                    echo "<div class='form'><center><h3>You have successfully registered!</h3><br/>Click here to <a href='loginmentor.php'>login</a></center></div>";
                }
            }
        }
    }else {
        echo "<div class='form'><center><h3>Invalid email address</h3><br/>Click <a href='mentorsignup.php'>here</a> to try again</center></div>";
    }                   
}else{
    echo "<div class='form'><center><h3>Enter values in all fields</h3><br/>Click <a href='mentorsignup.php'>here</a> to try again</center></div>";
}
?>

请原谅我的经验不足,我是新手。提前谢谢你

这应该可以解决问题。

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>..............

    $f = implode(', ', $_POST['catID']);

    if($a && $b && $c && $d && $e && $f)
..........

    mysqli_query($con, "INSERT INTO mentor VALUES(DEFAULT,'$a','$b','$c','$d','$e')");
          
    $mentorID = mysqli_insert_id($con);
    $fs = explode(', ', $f);     
    foreach ($fs as $f )
    {
    mysqli_query($con, "INSERT INTO logged_cat (mentorID, catID) VALUES ('$mentorID', $f)");
    }