[英]Data insertion issues with HTML form and PHP to mySQL
I have seen few posts in SO related to the same scenario as mine but did not find a proper resolution. 我在SO中看到的帖子很少与我的相同,但没有找到合适的解决方案。 So am posting question with my problem stuff.
所以我发布问题的问题。
I have an HTML form 我有一个HTML表单
<form method="post" id="myForm">
<label for="e_name">Name</label>
<input name="e_name" id="emp_name" value="" type="text" data-theme="a">
<label for="date">Date</label>
<input name="date" id="emp_dob" value="" data-theme="a">
<label for="gender">Gender</label>
<select name="gender" id="emp_gender" data-role="slider" data-theme="a" data-inline="true">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<label for="address">Address</label>
<textarea name="address" id="emp_address" value="" type="text" data-theme="a"></textarea><br><br>
<input type="button" id="insert" value="Submit">
</form>
<div id="someElement"></div>
And I have the following to perform my form elements submission to a PHP page- 我有以下内容来执行我的表单元素提交到PHP页面 -
$(document).ready(function(){
$("#insert").click(function(e) {
e.preventDefault();
alert("am in the Insert function now");
$.ajax({
cache: false,
type: 'POST',
url: 'insert.php',
data: $("#myForm").serialize(),
success: function(d) {
$("#someElement").html(d);
}
});
});
});
Here is my PHP - 这是我的PHP -
<?php
$con=mysqli_connect("localhost","root","root","employee");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name =" ";
$dob =" ";
$gender =" ";
$address =" ";
if(isset($_POST['emp_name'])){ $name = $_POST['emp_name']; }
if(isset($_POST['emp_dob'])){ $dob = $_POST['emp_dob']; }
if(isset($_POST['emp_gender'])){ $gender = $_POST['emp_gender']; }
if(isset($_POST['emp_address'])){ $address = $_POST['emp_address']; }
echo $name;
echo $dob;
echo $gender;
echo $address;
$sql="INSERT INTO emp_details (emp_name, emp_gender, emp_address) VALUES ('$name', '$gender', '$address')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Now what happens is, when I enter some values in my form and click on Submit, action performs well and a new row inserts in the database. 现在发生的事情是,当我在表单中输入一些值并单击“提交”时,操作执行良好,并在数据库中插入新行。 But all the rows are empty.
但所有行都是空的。 They're just empty, not even "NULL".
它们只是空的,甚至不是“空”。 I tried to echo my field values in the PHP but there is no output there.
我试图在PHP中回显我的字段值,但那里没有输出。 My "1 Record Added" has come-up well, but no form values appear here.
我的“1记录已添加”很好,但此处没有显示任何表单值。
Kindly help me sorting this out. 请帮我整理一下。
Thanks in advance. 提前致谢。
$_POST[]
references to the name
attribute of html-tag
, not id
. $_POST[]
引用html-tag
的name
属性,而不是id
。
For example, $_POST['emp_name']
should be $_POST['e_name']
例如,
$_POST['emp_name']
应为$_POST['e_name']
Furthermore, don't encapsulate your variables with single quotes: 此外,不要用单引号封装变量:
"INSERT INTO emp_details (emp_name, emp_gender, emp_address) VALUES ('$name', '$gender', '$address')";
Do this instead: 改为:
"INSERT INTO emp_details (emp_name, emp_gender, emp_address) VALUES ('" . $name . "', '" . $gender . "', '" . $address . "')";
Or use bind_param()
from mysqli
ofcourse! 或者使用来自
mysqli
ofcourse的bind_param()
!
Make the id
and name
of your input elements same 使输入元素的
id
和name
相同
<form method="post" id="myForm">
<label for="e_name">Name</label>
<input name="emp_name" id="emp_name" value="" type="text" data-theme="a">
<label for="date">Date</label>
<input name="emp_dob" id="emp_dob" value="" data-theme="a">
<label for="gender">Gender</label>
<select name="emp_gender" id="emp_gender" data-role="slider" data-theme="a" data-inline="true">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<label for="address">Address</label>
<textarea name="emp_address" id="emp_address" value="" type="text" data-theme="a"></textarea><br><br>
<input type="button" id="insert" value="Submit">
</form>
Otherwise change your $_POST
array keys.Because you will get the keys of $_POST
array will be the name of input elements.But i recommend you to mak ethe name and id same 否则更改你的
$_POST
数组键。因为你将得到$_POST
数组的键将是输入元素的名称。但我建议你把名称和id相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.