gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:47:45 26 4
gpt4 key购买 nike

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

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

2) 对于这个特定的脚本,我有 3 个表; Mentor、Category 和 Logged Category 来记录数据以实现标准化。除了问题编号 (1) 之外,导师表似乎每次注册都会获得 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>

这是我的注册.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 class="snippet-code-html lang-html prettyprint-override"><code>..............

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

if($a && $b && $c && $d && $e && $f)</code></pre>
</div>
</div>


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

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)");
}</code></pre>
</div>
</div>

关于php - Explode/foreach 无法将复选框中的多个值记录到数据库中的多行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49669740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com