未定义的索引:

时间:2015-07-28 12:13:10

标签: php html forms mysqli

我收到此错误: 注意:未定义索引:性别\ form.php在第26行。请帮助..如何解决此错误..?

如何从表格员工性别男性或女性获取并插入MySQL数据库

**my html code**

<form action="form.php" method="post">
<table border="1">
  <tr>
    <td align="center">Form Input Employees Data</td>
  </tr>
  <tr>
    <td>
      <table>
        <form method="post" action="form.php">
        <tr>
          <td>Name</td>
          <td><input name="employe_name" type="text" id="employe_name" size="20">
          </td>
        </tr>
        <tr>
          <td>Address</td>
          <td><input name="employe_add" type="text" id="employe_add" size="40">
          </td>
        </tr>
        <tr>
          <td height="23">Gender</td>
		  <td>Male<input type="radio" name="gender"value="m" />
		  Female<input type="radio" name="gender"value="f"/>
		  </td></tr>
		  
		  <td>
          <td align="right"><input type="submit" 
          name="submit" value="Submit"> add</td>
        </tr>
        </table>
      </td>
    </tr>
</table></form>

和 我的PHP代码是......

<?php

//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','root','','employe');

//Output any connection error
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}else{
    echo"connection good";
}
if(isset($_POST['gender']))
{

    $gender=$_POST['gender'];
    echo("your are select" .$gender);


}
//get values from form
$employe_name=$_POST['employe_name'];
$employe_add=$_POST['employe_add'];
$gender=$_POST['gender'];


//insert data into mysql



$sql = "INSERT INTO employe_detail(employe_name,employe_add,gender) VALUES('$employe_name','$employe_add','$gender')";

print '<h3>Insert a record</h3>';



if($sql)
{
    print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />'; 
}else{
    die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}

$mysqli->close();
?>

请帮帮我............

1 个答案:

答案 0 :(得分:0)

更改您的代码
if(isset($_POST['gender']))
{

    $gender=$_POST['gender'];
    echo("your are select" .$gender);


}
//get values from form
$employe_name=$_POST['employe_name'];
$employe_add=$_POST['employe_add'];
$gender=$_POST['gender'];

$gender = "";
if(isset($_POST['gender']))
{

    $gender=$_POST['gender'];
    echo("your are select" .$gender);


}
//get values from form
$employe_name=$_POST['employe_name'];
$employe_add=$_POST['employe_add'];

将分配性别或将其留空