I can insert all fields in database except email field. I get the following error "Undefined index: em in C:\wamp\www\ins_db.php on line 11" data type of email is below:
我可以在数据库中插入除电子邮件字段之外的所我收到以下错误“未定义的索引:在第11行的C:\ wamp \ www \ ins_db.php中的em”电子邮件的数据类型如下:
email varchar(20) not null,
Form code:
<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>
Following is the full php code which receives form data
以下是接收表单数据的完整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);
?>
3 个解决方案
#1
0
Test for a value in each individual POST variable. Replace your PHP code with this:
测试每个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);
?>
As a side note, inserting straight from POST variables into a database is a serious security risk.
作为旁注,从POST变量直接插入数据库是一个严重的安全风险。
#2
0
You missed an equal sign in the HTML code.
您错过了HTML代码中的等号。
<input type = "text" name "em">
should be
<input type = "text" name = "em">
#3
0
You have mised -> =
你已经mised了 - > =
E-mail *<input type = "text" name "em"><br><br>
^ HEre
Should be like this:
应该是这样的:
E-mail *<input type = "text" name = "em"><br><br>
But you have to check either your em
variable is set or not.
但是你必须检查你的em变量是否已设置。
if(isset($_POST['em'])){
$em = $_POST['em'];
}
#1
0
Test for a value in each individual POST variable. Replace your PHP code with this:
测试每个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);
?>
As a side note, inserting straight from POST variables into a database is a serious security risk.
作为旁注,从POST变量直接插入数据库是一个严重的安全风险。
#2
0
You missed an equal sign in the HTML code.
您错过了HTML代码中的等号。
<input type = "text" name "em">
should be
<input type = "text" name = "em">
#3
0
You have mised -> =
你已经mised了 - > =
E-mail *<input type = "text" name "em"><br><br>
^ HEre
Should be like this:
应该是这样的:
E-mail *<input type = "text" name = "em"><br><br>
But you have to check either your em
variable is set or not.
但是你必须检查你的em变量是否已设置。
if(isset($_POST['em'])){
$em = $_POST['em'];
}