gpt4 book ai didi

php - 选择两个选项 显示错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:01:21 25 4
gpt4 key购买 nike

我在 php 中有一个表单来选择 3 个选项,当我选择所有三个时没有显示错误,但是当我选择两个选项时它显示错误但选项保存在数据库中,并显示记录已保存,但也显示通知: Undefined index: hb3 in C:\wamp\www\uni1\admin\courses.php 第14行2014 年 7 月 25 日

<?php 
include('auth.php');
include('conect.php'); $msg='';
if(isset($_POST['save']))
{
$category = $_POST['category'];
$cname = $_POST['cname'];
$code = $_POST['code'];
$type = $_POST['type'];
$dur = $_POST['dur'];
$fee = $_POST['fee'];
$hb1 = $_POST['hb1'];
$hb2 = $_POST['hb2'];
$hb3 = $_POST['hb3'];
$catid =mysql_insert_id();
$sql = mysql_query("select * from category where catid='$category'");// to get category code from category table
$rs = mysql_fetch_array($sql);
mysql_query("INSERT INTO courses1 (catid,cname, code, type, dur, fee, hb1, hb2, hb3)
VALUES ('$category','$cname','$code','$type','$dur','$fee','$hb1','$hb2','$hb3')");
$msg = '<h3>Record Saved</h3>';
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>courses</title>
<link rel="stylesheet" type="text/css" href="headerstyle.css" />
</head>

<body>
07/25/2014
<div class="fixx"><?php include('header.php');?></div>
<div class="tabl">
<table width="700" border="0">

<tr>
<td>
<form method="post" action="courses.php">
<table width="700" border="1" cellpadding="10">
<tr>
<td colspan="2"><h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ADD COURSES &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="courses_list.php"> EDIT</a> </h3></td>
</tr>
<tr>
<td colspan="2"><?php echo $msg;?></td>
</tr>
<tr>
<td width="174"><h4>Select Category </h4></td>
<td width="674"><label for="category"></label>
<select name="category" id="category">
<?php $qre = mysql_query("select * from category ORDER BY name");
while($res = mysql_fetch_array($qre))
{
echo '<option value="'.$res['catid'].'">'.$res['name'].'</option>';


}



?>


</select> </td>
</tr>
<tr>
<td width="174"><h4>Enter Course Name</h4></td>
<td width="674"><label for="cname"></label>
<input name="cname" type="text" id="cname" size="70" /></td>
</tr>
<tr>
<td><h4>Course Code</h4></td>
<td><input type="text" name="code" id="code" /></td>
</tr>
<tr>
<td><h4>Type</h4></td>
<td><label for="type"></label>
<select name="type" id="type">
<option value="Month"><h4>Month</h4></option>
<option value="Semester"><h4>Semester</h4></option>
<option value="Year"><h4>Year</h4></option>
</select></td>
</tr>
<tr>
<td><h4>Duration</h4></td>
<td><p>
<select name="dur" id="dur">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
</p></td>
</tr>
<tr>
<td><h4>Fee</h4></td>
<td><input type="text" name="fee" id="fee" /></td>
</tr>
<tr>
<td><h4>Mode</h4></td>
<td><h4>Online
<input type="checkbox" name="hb1" id="hb1" value="online" /></h4><br />
<h4>Private
<input type="checkbox" name="hb2" id="hb2" value="private" /></h4>
<br />
<h4>Regular
<input type="checkbox" name="hb3" id="hb3" value="regular" />
</h4> <br />
</label></td>
</tr>
<tr>

<td><input type="submit" name="save" id="save" value="Submit" /></td>
</tr></h2>
</table>
</form>
</td>
</tr>

</table>
</div>

</body>
</html>

请解决我的问题

最佳答案

有错误,因为当你只选择两个选项时,第三个选项有 null 意味着没有值

$hb3 has no value for choice

你应该检查是否设置了值,如果没有则分配一些默认值

$hb1 = isset($_POST['hb1'])?$_POST['hb1']:'';
$hb2 = isset($_POST['hb2'])?$_POST['hb2']:'';
$hb3 = isset($_POST['hb3'])?$_POST['hb3']:'';

关于php - 选择两个选项 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29061218/

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