我有一个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>
用户可以输入所有或部分受训者并提交。
现在这是我的PHP代码的一部分: (对不起长而凌乱的代码) PHP应该检查发布的$ mentee1-10是否为空或根据它们进行查询,但是我尝试了代码,它似乎只发送$ 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>";
}
答案 0 :(得分:1)
如果我不完全忽略你的观点,那么这样的事情怎么样。你可以通过一些循环来节省很多代码重复:
<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>";
}
}
}
?>
答案 1 :(得分:0)
让我们看看我是否理解正确。您是否尝试根据发送的值插入值,但它们并不相互依赖?如果是这样的话,我会做这样的事情:
$arr = array();
$str = "";
foreach($_POST as $key => $value) {
if(isset($value) && strlen($value) > 0) {
$arr[] = "('$staff_no', ".$key.")";
}
}
$str = implode(',', $arr);
$query_upload = "INSERT INTO mentor_student (staff_no, student_no) VALUES " . $str;
echo $query_upload;
在这里,我根据用户发送的值动态创建一个数组,然后用逗号加入它们,这样我就可以像你的一样进行查询。
试试吧,你会告诉我们。
答案 2 :(得分:0)
你不想在某种情况下做某种情况...... 如果我明白你想做什么,你想要这样的东西: (不是最好的方法,但尝试):
@IBOutlet weak var mapView: MKMapView!
@IBAction func zoomToUserCurrentLocation(sender: AnyObject) {
if self.mapView != nil {
self.mapView.setRegion(MKCoordinateRegionMake(
self.mapView.userLocation.coordinate,
MKCoordinateSpanMake(0.1, 0.1)
), animated: true)
}
}