Js,Ajax,Json-如何不插入“;数组“;从自动完成到数据库
Js, Ajax, Json - How not to insert "Array" into db from autocomplete
im正在构建一个小脚本,该脚本将根据第一个输入字段的自动完成自动填充几个输入字段。
我的脚本正在运行,但当我点击提交按钮时,一切似乎都很好,但当我们查看数据库以查看插入了什么时,所有字段都插入了"数组",而不是应该插入的内容。在自动完成上,我可以看到字段是带有正确信息的字段,但我猜php不明白它是什么,而是插入了"Array"。
有什么办法吗?
我的表格:
<form action="job_post.php" method="post">
<div class="form-group has-success col-md-3">
<label class="control-label" for="inputSuccess1">First Name: </label>
<input type='text' id='firstname' name='firstname[]'/>
</div>
<div class="form-group has-success col-md-3">
<label class="control-label" for="inputSuccess1">Last Name: </label>
<input type='text' id='lastname' name='lastname[]'/>
</div>
<div class="form-group has-success col-md-3">
<label class="control-label" for="inputSuccess1">Age: </label>
<input type='text' id='age' name='age[]'/>
</div>
<div class="form-group has-success col-md-3">
<label class="control-label" for="inputSuccess1">Company Name: </label>
<input type='text' id='CoName' name='CoName[]'/>
</div>
<input type="submit">
</form>
我的job_post.php
<?php
require_once 'db_connect.php';
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$age=$_POST['age'];
$CoName=$_POST['CoName'];
$qry=mysql_query("INSERT INTO jobs (FirstName, LastName, Age, CoName)
VALUES ('$firstname', '$lastname', '$age', '$CoName')", $con);
if(!$qry)
{
mysql_close($con);
die("Query Failed: ". mysql_error());
}
else
{
mysql_close($con);
header("Location:index.php");
}
mysqli_close($con);
?>
我的Js:
$('#firstname').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
data: {
name_startsWith: request.term,
type: 'firstname',
row_num : 1
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[0],
value: code[0],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 2,
select: function( event, ui ) {
var names = ui.item.data.split("|");
console.log(names[1], names[2], names[3]);
$('#lastname').val(names[1]);
$('#age').val(names[2]);
$('#CoName').val(names[3]);
}
});
我的Ajax:
<?php
/*
Site : http:www.smarttutorials.net
Author :muni
*/
require_once 'db_connect.php';
if($_GET['type'] == 'firstname'){
$row_num = $_GET['row_num'];
$result = mysql_query("SELECT FirstName, LastName, Age, CoName FROM jobs where FirstName LIKE '".strtoupper($_GET['name_startsWith'])."%'");
$data = array();
while ($row = mysql_fetch_array($result)) {
$name = $row['FirstName'].'|'.$row['LastName'].'|'.$row['Age'].'|'.$row['CoName'].'|'.$row_num;
array_push($data, $name);
}
echo json_encode($data);
}
?>
它确实提取了数据,但我希望通过提交按钮将其插入数据库。
非常感谢您的帮助。
更新信息
然后将变量放置在您现在拥有的查询中。
页面重做:
<?php
require_once 'db_connect.php';
$firstname = $_POST["firstname"][0];
$CoName = $_POST["CoName"][0];
/*
* Last Name and Age are not in the array, I removed them from the query
* however you need to place them in the array if you wish to add them
*/
$qry=mysql_query("INSERT INTO jobs (FirstName, CoName)
VALUES ('$firstname', '$CoName')", $con);
if(!$qry) {
mysql_close($con);
die("Query Failed: ". mysql_error());
} else {
mysql_close($con);
header("Location: index.php");
}
?>
再次更新,误读数组。
请确保您获得的是单个记录,因为在sql中使用限制为1(这不是一个好主意)。否则循环通过返回数组进行多次插入。在插入之前,请使用print_r($_POST);
并查看要插入的内容。
相关文章:
- 在不使用循环的情况下,从一个数据库字符串值向javascript数组添加多个对象
- 如何使用JQuery将值数组插入到数据库中
- 在不重构Mongo数据库的情况下,更新嵌入数组中的具有给定ID的对象
- 获取从数据库到json_encode的数组
- 在Ember中显示使用数据库中的JSON的数组
- 无法从从sql数据库读取的javascript函数中获取返回数组
- 将 php 数据库结果传递给 javascript 数组
- 如何将javascript yml字符串移动到数据库to ruby数组
- 通过node-js脚本将文档数组插入mongodb中的数据库时出现问题
- PHP 数组未向数据库插入多行
- 如何通过php从数据库创建javascript数组
- 如何将复选框数组中的数据保存到数据库中
- 将数组的顺序插入到数据库中
- 用于获取数组中的数据库值的 php 脚本,以便在下拉列表中查看
- 蒙戈数据库.查询子文档中的数组
- 如何通过Firebase数据库中的AngularJS在点击时获取数组元素的唯一ID
- Js 数组检测:从 php 数组中找到的值数(从数据库中抓取)
- 来自数据库数组的简单 Javascript 幻灯片
- 将 mySQL 数据库信息放入 JavaScript 数组中
- 使用 ajax 使用 jquery 数组更新 mysql 数据库