gpt4 book ai didi

php - 发布动态输入并插入 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 22:11:03 25 4
gpt4 key购买 nike

这是我的 Schedule.php 页面,是用户选择具体日期的示例。

enter image description here

用户点击添加后,将显示确认页面。但不幸的是我遇到了这些错误。

enter image description here

在 mySQL 数据库中,假定属于“1700 - 1800”的值出现在“1400 - 1600”行上。

enter image description here

这是我的 Schedule.php 页面的代码,

            <form name="Create New schedule"  class="form-horizontal" method="post" action="handleSchedule.php">
<div class="form-group">
<div class="col-sm-4">
<label for="AcadInst">Academic Institution</label>

<select class="form-control" id="AcadInst" type="text" class="form-control" placeholder="Institution Name" name="academicInstitution">
<option>Institution Name</option>
<option>Singapore Polytechnic (SP)</option>
<option>Ngee Ann Polytechnic (NP)</option>
<option>Temasek Polytechnic (TP)</option>
<option>Republic Polytechnic (RP)</option>
<option>Nanyang Polytechnic (NYP)</option>
<option>Others(Please specify)</option>
</select>
</div>
<div class="col-sm-4">
<label>Level of Teaching</label>
<input list="LvTeaching" type="text" class="form-control" placeholder="Teaching Stage" name="levelofteaching">
<datalist id="LvTeaching">
<option value="Undergraduate Teaching">
<option value="Postgraduate Teaching">
<option value="Continuing Education">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Type of Teaching</label>
<input list="TyTeaching" type="text" class="form-control" placeholder="Mode of Teaching" name="typeofteaching">
<datalist id="TyTeaching">
<option value="Clinical Teaching">
<option value="Academic Teaching">
<option value="Talk">
<option value="Others (Please specify)">
</datalist>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<label for="startdate">Start Date</label>
<input type="date" class="form-control" placeholder="Select Date" name="startdate">
</div>
<div class="col-sm-4">
<label for="enddate">End Date</label>
<input type="date" class="form-control" placeholder="Select Date" name="enddate" id="noEndDate">
<input onclick="document.getElementById('noEndDate').disabled = true;" type="checkbox" name="type">No end date
</div>
</div>

<div class="form-group">
<div class="panel-body table-responsive">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Start Time</th>
<th>End Time</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th><input type="button" class="btn btn-warning" value="Add Timeslot" onClick="addRow('dataTable')" /></th>
</tr>
</thead>
<tbody id="dataTable">
<tr>
<td><input type="text" class="form-control" name="startTime[0]"></td>
<td><input type="text" class="form-control" name="endTime[0]"></td>
<td><input type="checkbox" name="Monday[0]" value="1"></td>
<td><input type="checkbox" name="Tuesday[0]" value="1"></td>
<td><input type="checkbox" name="Wednesday[0]" value="1"></td>
<td><input type="checkbox" name="Thursday[0]" value="1"></td>
<td><input type="checkbox" name="Friday[0]" value="1"></td>
<td><input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" /></td>
</tr>
</tbody>
</table>
<br/>

</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<input type="submit" value="Add" class="btn btn-success">
</div>
</div>
</form>

我的handleSchedule.php代码,

<?php

$con = getDbConnect();

$academicInstitution = $_POST['academicInstitution'];
$levelofteaching = $_POST['levelofteaching'];
$typeofteaching = $_POST['typeofteaching'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];

if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
if ($_POST['startTime']) {
foreach ($_POST["startTime"] as $key => $value) {

$endTime = $_POST["endTime"][$key];
$monday = $_POST["Monday"][$key];
$tuesday = $_POST["Tuesday"][$key];
$wednesday = $_POST["Wednesday"][$key];
$thursday = $_POST["Thursday"][$key];
$friday = $_POST["Friday"][$key];


$sql = "INSERT INTO timetableschedule (academicInstitution, lvteaching, tyteaching, startdate, endate, startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday) " .
"VALUES ('$academicInstitution', '$levelofteaching', '$typeofteaching', '$startdate', '$enddate', '$value', '$endTime', '$monday', '$tuesday', '$wednesday', '$thursday', '$friday')";
mysqli_query($con, $sql);
}
}

echo "1 record added";
mysqli_close($con);
}
?>

我怀疑未选中的复选框导致了问题。有人有办法解决吗?

最佳答案

未选中的复选框不会提交到服务器。所以 $_POST['Monday'] 只是所有选中框的数组。这些参数的索引将与文本框不匹配。

解决方案是在名称中放置显式索引:

    <tr>
<td><input type="text" class="form-control" name="startTime[0]"></td>
<td><input type="text" class="form-control" name="endTime[0]"></td>
<td><input type="checkbox" name="Monday[0]" value="1"></td>
<td><input type="checkbox" name="Tuesday[0]" value="1"></td>
<td><input type="checkbox" name="Wednesday[0]" value="1"></td>
<td><input type="checkbox" name="Thursday[0]" value="1"></td>
<td><input type="checkbox" name="Friday[0]" value="1"></td>
<td><input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" /></td>
</tr>

添加新行时,需要增加索引。

在PHP中,需要检查该参数是否设置。

$monday = isset($_POST["Monday"][$key]) ? 1 : 0;

关于php - 发布动态输入并插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665679/

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