选择两个选项显示错误

时间:2015-03-15 13:35:45

标签: php html mysql

我在php中有一个表单选择3个选项,当我选择全部三个没有显示错误时,但是当我选择两个选项时它显示错误但选择保存在数据库中,并显示保存的记录,但也显示通知:未定义的索引:第14行的C:\ wamp \ www \ uni1 \ admin \ courses.php中的hb3 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>

请解决我的问题

4 个答案:

答案 0 :(得分:2)

有错误,因为当您只选择两个选项时,第三个选项为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']:'';

答案 1 :(得分:1)

如果不想注意“未定义的索引”,您应该这样做

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

答案 2 :(得分:0)

当checkox未选中时,$ _POST不包含给定键,因此您无法分配其值。每当您使用复选框时,请指定给定的帖子值,如下所示:

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

然后在未定义值的情况下,您将分配一个空字符串,您将看不到这些通知。

答案 3 :(得分:0)

使用隐藏字段

<input type="hidden" id="hb1_" name="hb1"  value="-1">
<input type="checkbox" id="hb1" name="hb1" value="online" />

<input type="hidden" id="hb2_" name="hb2"  value="-1">
<input type="checkbox" id="hb2" name="hb2" value="private" />

<input type="hidden" id="hb3_" name="hb3"  value="-1">
<input type="checkbox" id="hb3" name="hb3" value="regular" />