1

我可以在数据库中插入除电子邮件字段之外的所有字段。我收到以下错误“未定义的索引:第 11 行 C:\wamp\www\ins_db.php 中的 em”数据类型的电子邮件如下:

email varchar(20)  not null,

表格代码:

<form action = "ins_db.php" method = "post">
<div class="reg"><br>
<br>All fields (*) are mandatory
<br>
User Id *<input type = "text" name = "id"><br><br>
Password *<input type = "password" name = "pwd"><br><br>
Confirm Password *<input type = "password" name = "cpwd"><br><br>
First Name *<input type = "text" name = "fname"><br><br>
Last Name *<input type = "text" name = "lname"><br><br>
Contact *<input type = "number" name = "contact"><br><br>
E-mail *<input type = "text" name ="em"><br><br>
Date of Birth *<input type = "date" name = "dob"><br><br>
<span>Qualification *
<select name = "qualification">
  <option value="B.Sc">B.Sc</option>
  <option value="M.Sc">M.Sc</option>
  <option value="Ph.d">Ph.d</option>
  <option value="B.Tech">B.Tech</option>
  <option value="B.Tech">B.Tech</option>
</select></span><br><br>
Experience *<input type = "number" name = "exp"><br><br>
Gender *<input type = "text" name = "gender"><br><br>
Address *<textarea cols="40" rows="5" name = "address">
Enter your address
</textarea><br><br>
Country *<input type = "text" name = "country"><br><br>
Religion *<input type = "text" name = "religion"><br><br>
Hint question *<input type = "text" name = "qhint"><br><br>
Hint answer *<input type = "text" name = "ahint"><br><br>
<?php
  $timezone = "Asia/Kolkata";
  date_default_timezone_set($timezone);
  $today = date("Y-m-d");
?>
ID created On *<input type = "date" name = "createon" value="<?php echo $today; ?>"><br><br>
<input type = "submit" value = "Submit">
</div>
</form>

以下是接收表单数据的完整 php 代码

<?php
$con = mysqli_connect('localhost','root','','jobhunt');
if(!$con)
{
echo "Can not connect to DB";
}
if(isset($_POST['id']))
echo "suceecess";
if(isset($_POST['em'])){
$em = $_POST['em'];
}
else 
echo "empty";
if(isset($_POST))
{
$query = "INSERT INTO seeker(sid,fname,lname,contact,email,dob,gender,address,religion,experience,qualification,created)
VALUES
('$_POST[id]','$_POST[fname]','$_POST[lname]','$_POST[contact]','$_POST[em]','$_POST[dob]','$_POST[gender]','$_POST[address]','$_POST[religion]','$_POST[exp]','$_POST[qualification]','$_POST[createon]')";
}
if (!mysqli_query($con,$query))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>
4

3 回答 3

0

你错过了->=

E-mail *<input type = "text" name "em"><br><br>
                                 ^ HEre

应该是这样的:

E-mail *<input type = "text" name = "em"><br><br>

但是您必须检查您的em变量是否已设置。

if(isset($_POST['em'])){
$em = $_POST['em'];
}
于 2013-09-22T13:10:12.693 回答
0

测试每个单独的 POST 变量中的值。用这个替换你的 PHP 代码:

<?php

    $con = mysqli_connect('localhost','root','','jobhunt');

    if(!$con){
        echo "Can not connect to DB";
        exit;
    }

  $keys = array('id', 'fname', 'lname', 'contact', 'em', 'dob', 'gender', 'address', 'religion', 'exp', 'qualification', 'createon');
  $query = "INSERT INTO seeker(sid,fname,lname,contact,email,dob,gender,address,religion,experience,qualification,created) VALUES (";

  foreach($keys as $key){

    if(isset($_POST[$key])){

      $query .= "'" . $_POST[$key] . "'";

    }else{

      $query .= "''";

    }

    if($key === end($keys)){

      $query = ");";

    }else{

      $query .= ",";

    }
  }

  if (!mysqli_query($con,$query)){
    die('Error: ' . mysqli_error($con));
  }else{
    echo "success";
  }

  mysqli_close($con);

?>

附带说明一下,将 POST 变量直接插入数据库是一个严重的安全风险。

于 2013-09-22T13:10:42.010 回答
0

您错过了 HTML 代码中的等号。

<input type = "text" name "em">

应该

<input type = "text" name = "em">
于 2013-09-22T13:12:05.560 回答