我目前正在建立一个在线青年辅导门户网站,要求导师和受指导者注册。 导师注册表单有一个复选框区域,他们点击他们希望指导的类别。他们可以根据需要点击。我的问题如下:
1)尽管注意到它是一个数组并且在使用$ _POST时使用implode并且在插入数据库时爆炸和foreach,我似乎只在数据库中记录的复选框中选择了第一个选项。请帮助我解决这个问题。
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>
&#13;
这是我的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>";
}
?>
请原谅我的经验不足,我是新手。提前谢谢
答案 0 :(得分:0)
这应该可以解决问题。
..............
$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)");
}