我的 sql 查询有什么问题?它不存储数据
What is wrong with my sql query? It doesnt store the data
请帮助我 sql 查询。我无法将 checkbox 和 date 的值存储在我的 database 中。有解决方案吗?如果没有的话。我应该改变什么?对于 sql 和 php,我只是一个初学者。 提前致谢。
<form method = "POST"><strong>
Set the date:
</strong>
<input type = "date" name = "set_date"></br></br>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>First name</th>
<th>Last name</th>
<th>Subject</th>
<th>Section</th>
<th>Gender</th>
<th>Status</th>
</tr>
<?php
$con = mysql_connect("localhost", "root", "avtt123");
mysql_select_db("arvintarrega",$con);
$query = mysql_query("SELECT * FROM student");
echo "<form action = attendance.php method = POST>";
while($student=mysql_fetch_array($query))
{
echo "<tr>";
echo "<td><center>".$student['fname']."</center></td>";
echo "<td><center>".$student['lname']."</center></td>";
echo "<td><center>".$student['subject']."</center></td>";;
echo "<td><center>".$student['section']."</center></td>";
echo "<td><center>".$student['gender']."</center></td>";
echo '<td><center><input type = "checkbox" name = "status" value = "Present">Present</input>';
echo '<input type = "checkbox" name = "status" value = "Absent">Absent</input>';
echo '<input type = "checkbox" name = "status" value = "Excused">Excused</center></input></td>';
}
echo "</form>";
echo "</table>";
if(isset($_POST['save']))
{
if(empty($_POST['set_date']) && empty($_POST['status']))
{
if(isset($_POST['set_date']) && ($_REQUEST['status']=="Present"))
{
$sql = "INSERT INTO attendance(date, status) VALUES('$_POST[set_date]', '$_REQUEST[status]')";
echo '<span style="color:green;"><strong>Attendance Complete!<strong></span>';
}
else if(isset($_POST['set_date']) && ($_REQUEST['status']=="Absent"))
{
$sql = "INSERT INTO attendance(date, status) VALUES('$_POST[set_date]', '$_REQUEST[status]')";
echo '<span style="color:green;"><strong>Attendance Complete!<strong></span>';
}
else if(isset($_POST['set_date']) && ($_REQUEST['status']=="Excused"))
{
$sql = "INSERT INTO attendance(date, status) VALUES('$_POST[set_date]', '$_REQUEST[status]')";
echo '<span style="color:green;"><strong>Attendance Complete!<strong></span>';
}
mysql_query($sql, $con);
}
else
echo '<span style="color:red;"><strong>All fields are required</strong></span>';
}
mysql_close();
?>
</br></br>
<input type = "submit" name = "save" value = "Submit">
</form>
</center>
</body>
</html>
您现在有一个 if 语句来检查您的变量是否为空,因此以后的值永远不会为真。
删除这个:
if(empty($_POST['set_date']) && empty($_POST['status']))
{
...
}
但当然要留下代码
请帮助我 sql 查询。我无法将 checkbox 和 date 的值存储在我的 database 中。有解决方案吗?如果没有的话。我应该改变什么?对于 sql 和 php,我只是一个初学者。 提前致谢。
<form method = "POST"><strong>
Set the date:
</strong>
<input type = "date" name = "set_date"></br></br>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>First name</th>
<th>Last name</th>
<th>Subject</th>
<th>Section</th>
<th>Gender</th>
<th>Status</th>
</tr>
<?php
$con = mysql_connect("localhost", "root", "avtt123");
mysql_select_db("arvintarrega",$con);
$query = mysql_query("SELECT * FROM student");
echo "<form action = attendance.php method = POST>";
while($student=mysql_fetch_array($query))
{
echo "<tr>";
echo "<td><center>".$student['fname']."</center></td>";
echo "<td><center>".$student['lname']."</center></td>";
echo "<td><center>".$student['subject']."</center></td>";;
echo "<td><center>".$student['section']."</center></td>";
echo "<td><center>".$student['gender']."</center></td>";
echo '<td><center><input type = "checkbox" name = "status" value = "Present">Present</input>';
echo '<input type = "checkbox" name = "status" value = "Absent">Absent</input>';
echo '<input type = "checkbox" name = "status" value = "Excused">Excused</center></input></td>';
}
echo "</form>";
echo "</table>";
if(isset($_POST['save']))
{
if(empty($_POST['set_date']) && empty($_POST['status']))
{
if(isset($_POST['set_date']) && ($_REQUEST['status']=="Present"))
{
$sql = "INSERT INTO attendance(date, status) VALUES('$_POST[set_date]', '$_REQUEST[status]')";
echo '<span style="color:green;"><strong>Attendance Complete!<strong></span>';
}
else if(isset($_POST['set_date']) && ($_REQUEST['status']=="Absent"))
{
$sql = "INSERT INTO attendance(date, status) VALUES('$_POST[set_date]', '$_REQUEST[status]')";
echo '<span style="color:green;"><strong>Attendance Complete!<strong></span>';
}
else if(isset($_POST['set_date']) && ($_REQUEST['status']=="Excused"))
{
$sql = "INSERT INTO attendance(date, status) VALUES('$_POST[set_date]', '$_REQUEST[status]')";
echo '<span style="color:green;"><strong>Attendance Complete!<strong></span>';
}
mysql_query($sql, $con);
}
else
echo '<span style="color:red;"><strong>All fields are required</strong></span>';
}
mysql_close();
?>
</br></br>
<input type = "submit" name = "save" value = "Submit">
</form>
</center>
</body>
</html>
您现在有一个 if 语句来检查您的变量是否为空,因此以后的值永远不会为真。 删除这个:
if(empty($_POST['set_date']) && empty($_POST['status']))
{
...
}
但当然要留下代码