将垂直数组值水平存储到mysql表中的不同列

时间:2018-09-27 10:48:22

标签: javascript php mysql

function insert()
{

	var detail = [];




      for (var i = 0 ; i<= arrayA.length ; i++)
	 {
	   
		detail.push(arrayA[i]);
		
	 }
	

	// });
	

$.ajax({  
    url:'insert.php',  
    method:"POST",  
    data:{  details: detail,},  
      
    success:function(data){  
    //alert(html);
    
    }  
   });  


}

var arrayA = [];
function addvalues()

	
	$('[name=data]').each(function() {
     
	arrayA.push($(this).val());

	});
	
	
	alert("record enter");
}

我已将值从引导程序形式推送到数组中。表单条目是连续的,就像用户填写表单,然后按添加,然后在确认将值添加到数组中后,用户再次输入具有不同值的表单并按添加。在该用户按下提交后,将值插入表中。现在表格就像

    <input class="form-control col-md-7 col-xs-12" name ="data" type="text"  value="" id="name-input" required >
<input class="form-control col-md-7 col-xs-12" name ="data" type="number"  value="" id="father-input" required>   
<input class="form-control col-md-7 col-xs-12" name ="data" type="number"  value="" id="mother-input" required>
<input class="form-control col-md-7 col-xs-12" name ="data" type="number"  value="" id="age-input" required >
<input class="form-control col-md-7 col-xs-12" name ="data" type="number"  value="" id="blood-input" required >
<button type="button" id="add" onclick = addvalues();>Add</button>

并在按下提交按钮时将数组的所有这些值插入表

 $sql1="INSERT INTO tble1 (name, father_name ,Mother_name,  age, blood_group)VALUES ";
 for($i=0;$i<count($values1);$i++) {
$sql1 .= " ('".$values1[$i]."', '".$values1[$i]."', '".$values1[$i]."','".$values1[$i]."','".$values1[$i]."'),";
 }
 $sql1_trimmed = rtrim($sql1,',');

但是,此查询在所有字段中输入的值为0。我想在第二个字段的第一个字段中输入值0,依此类推。

1 个答案:

答案 0 :(得分:0)

$sql1="INSERT INTO tble1 (name, father_name ,Mother_name,  age, blood_group)VALUES ('";
for($i=0;$i<count($values1);$i++) {
   if($i == count($values1)-1){
        $sql1 .= $values1[$i]."')";
        return 0;
   }
   $sql1 .= $values1[$i]."', '";
}

但是您不应该以这种方式插入,因为这很危险,它被称为“ SQL注入”

您必须这样做

$req = $bdd->prepare('INSERT INTO tble1(name, father_name ,Mother_name,  age, blood_group) VALUES(:name, :father_name , :Mother_name,  :age, :blood_group)');
$req->execute(array(
    'name' => $values1[0],
    'father_name' => $values1[1],
    'Mother_name' => $values1[2],
    'age' => $values1[3],
    'blood_group' => $values1[4]
    ));

如果您有这样的数组

$values=[name,...., name, father_name, Mother_name, age, blood_group];

然后就这样

$values=array_chunk($values, 5);

for($i=0;$i<count($values);$i++) {
    $values1 = $values[$i];
$req = $bdd->prepare('INSERT INTO tble1(name, father_name ,Mother_name,  age, blood_group) VALUES(:name, :father_name , :Mother_name,  :age, :blood_group)');
$req->execute(array(
    'name' => $values1[0],
    'father_name' => $values1[1],
    'Mother_name' => $values1[2],
    'age' => $values1[3],
    'blood_group' => $values1[4]
    ));
}

示例

您从表单中获得了这样的数组

$values=[name0, father_name0, Mother_name0, age0, blood_group0, name1, father_name1, Mother_name1, age1, blood_group1];

它包含两组数据name0, father_name0, Mother_name0, age0, blood_group0name1, father_name1, Mother_name1, age1, blood_group1 它可能包含n组数据,但所有数据都有5个值,这就是为什么我使用了

$values=array_chunk($values, 5);

它将数组拆分为n个数组(在本示例中为2个数组)

做完$values=array_chunk($values, 5);之后回到我们的例子 值将等于

$values=[[name0, father_name0, Mother_name0, age0, blood_group0], [name1, father_name1, Mother_name1, age1, blood_group1]];

并使用for循环,我们将像这样循环遍历这些子数组

for($i=0;$i<count($values);$i++) {
   $values1 = $values[$i];
   ....
}

对于JS代码,您要多次添加相同的值,请改为

var arrayA = [];
function addvalues()

    var len = arrayA.length;
    for(let i=len; i<$('[name=data]').length; i++){
        if($('[name=data]')[i].val() || $('[name=data]')[i].val()==0){//if you aren't sure that these values can be null or not. Delete this line if you think that they can't be null.
           arrayA.push($('[name=data]')[i].val());
        }
     }

}

对于插入功能,也要这样做

    function insert(){
       $.ajax({  
             url:'insert.php',  
             method:"POST",  
             data:{  details: arrayA},  
             success:function(data){  
                             //alert(html);
                     }  
       });  
    }