未定义的字段正在插入数据库

时间:2016-09-29 09:51:46

标签: javascript php jquery html mysql

enter image description here

从我添加2个技能的图像中,一个额外的feild正在添加到数据库中:id_job是173。

enter image description here

这里我的html表单和Here jquery附加代码:

var skillcount=0;
$(".addSkills").click(function(){
	$('#jobSkills tr:last').after('<tr><td class="col-xs-4"><input placeholder="Enter or Type a letter to search skills" class="wp-form-control searchskill" count="'+skillcount+'" id="skill_'+skillcount+'_title" name="skill['+skillcount+'][title]" type="text" autocomplete="off"></td><td class="col-xs-4"><input placeholder="Should be 10 - 100" class="wp-form-control" count="'+skillcount+'" id="skill_'+skillcount+'_weightage" name="skill['+skillcount+'][weightage]" type="text" autocomplete="off"></td><td class="col-xs-4"><select class="wp-form-control" name="skill['+skillcount+'][type]"><option value="0">Select Test Type</option><option value="1">Practice Test</option><option value="2">Qualifying</option></select></td ><td class="col-xs-4"><input  class="selectGdSkill" type="checkbox" count="'+skillcount+'" id="skill['+skillcount+'][gdskill]" name="skill['+skillcount+'][gdskill]"></td> <td class="col-xs-4"> <input class="selectPiSkill" type="checkbox" count="'+skillcount+'" id="skill['+skillcount+'][piskill]" name="skill['+skillcount+'][piskill]"></td><td class="col-xs-2"><span class="removeSkill" id="'+skillcount+'" ><a style="color:red">Remove</a></span></td></tr>');
	skillcount++;
	//console.log(skillcount);
});
$("#jobSkills").on('click','.removeSkill',function(){
	console.log($(this).parent());
	$(this).parent().parent().remove();
<div class="col-lg-12">
	<h4>Skills Required</h4>
	<div class="col-md-10">
		<table id="jobSkills" class="col-lg-10" style="border:1px;">
			<tbody>
				<tr><td  class="col-xs-4"> Skill </td><td class="col-xs-4"> Weightage </td><td class="col-xs-4"> Test Type </td><td class="col-xs-4"> Assign to GD-Skill</td> <td class="col-xs-4">Assign to PI-Skill</td> </tr>
			</tbody>
		</table>
	</div>
	<div class="col-md-2">
		<span class="text-center btn btn-danger addSkills">+ Add Skills</span>
	</div>
</div>

我的控制器代码:

foreach($_POST["skill"] as $k=>$key)
{
    $conn->query("INSERT INTO r_job_skill (id_job,title,weightage,type,gdskill,piskill) values ('".$jobId."','".$key["title"]."','".$key["weightage"]."','".$key["type"]."','".$gdskill."','".$piskill."')");
}

1 个答案:

答案 0 :(得分:1)

在评论/回答中添加以下引用文字

“首先,如果你想从其他表中添加id_job,那么你可以获取隐藏的输入并设置值并将其发布在Controller Code中。建议:如果它与其他表无关,那么我建议保留id_job字段使用主键'自动增加'并在插入查询中传递NULL。

在插入数据库之前,还检查值是否为空,因此可以防止在数据库中保存空白记录。 “