【问题标题】:I've this form data to be sent to insert into database我有这个表单数据要发送到数据库中
【发布时间】:2013-09-22 13:06:15
【问题描述】:

我可以在数据库中插入除电子邮件字段之外的所有字段。我收到以下错误“未定义的索引:第 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);
?>

【问题讨论】:

标签: php mysql


【解决方案1】:

测试每个 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 变量直接插入数据库是一个严重的安全风险。

【讨论】:

  • 是的电子邮件字段为空。但形式有价值
  • 我在您当前的&lt;input&gt; 中没有看到电子邮件的值...我猜您是在发送之前自己插入的?
  • 我的意思是我已经在表单中输入了电子邮件 ID 以供处理。但它仍然是空的!
  • 是的,我测试过。它没有价值。但我不明白为什么当我输入一个值时
  • 而且您绝对确定您已将当前表单更改为 name="em" 而不是 name "em"
【解决方案2】:

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

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

应该是

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

【讨论】:

  • 谢谢!一开始是对的。我改了名字,错过了=符号。我编辑了它。但这不是问题。
  • 我按照您的建议进行了更正。但是还是有错误!我该如何解决?
【解决方案3】:

你错过了 -> =

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'];
}

【讨论】:

  • 我测试了您给定的代码并添加了 else 部分。问题是 $_POST['em'] 变量为空!我不知道为什么。
  • 我查过了!是的,问题是未设置 em 变量!但我已经在表单中输入了它的值
  • 请检查我的更新答案,我提到您在 HTML 中缺少等于 =11
  • 是的,我已经检查了所有内容并进行了更正。但是还是没有希望!我很害怕我必须完全放弃电子邮件字段才能继续前进!
  • 不!尝试var_dump($_POST['em']); 并检查它是否为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 2014-01-20
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 2017-10-16
  • 2021-08-21
相关资源
最近更新 更多