简体   繁体   English

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

[英]vertical array value store into mysql table different columns horizontally

 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"); } 

I have pushed values from bootstrap form into array. 我已经将值从引导形式推入数组。 Form entries are continuous like user fill the form and press add then after confirmation that values is added into array user again enter that form with different values and press add. 表单条目是连续的,就像用户填写表单,然后按添加,然后在确认将值添加到数组中后,用户再次输入具有不同值的表单并按添加。 after that user press submit to insert values into table. 在该用户按下提交后,将值插入表中。 now form is like 现在形式就像

  <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> 

and on pressing submit button all these values of an array insert into table 然后按提交按钮将数组的所有这些值插入表中

  $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,','); 

But, this query enter value at 0 in all field. 但是,此查询在所有字段中输入的值为0。 I want to enter value 0 in first field 1 in second and so on. 我想在第二个字段的第一个字段中输入值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]."', '";
}

But you shouldn't do insert in this way because it's dangerous it's called "sql injection" 但是您不应该以这种方式进行插入,因为这很危险,称为“ SQL注入”

You have to do it like this 你必须这样

$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]
    ));

If you have an array like this 如果你有这样的数组

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

then just do like this 然后就这样

$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]
    ));
}

Example

You got an array like this from your form 您从表单中得到了这样的数组

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

it contain two sets of data name0, father_name0, Mother_name0, age0, blood_group0 and name1, father_name1, Mother_name1, age1, blood_group1 it may contain n set of data but all of them have 5 values thats why I used 它包含两组数据name0, father_name0, Mother_name0, age0, blood_group0name1, father_name1, Mother_name1, age1, blood_group1它可能包含n组数据,但它们都有5个值,这就是为什么我使用

$values=array_chunk($values, 5);

it will split the array into n (in this example it will be 2 array) array 它将数组拆分为n个数组(在本示例中为2个数组)

let's return to our example after doing $values=array_chunk($values, 5); 让我们在执行$values=array_chunk($values, 5);之后返回示例$values=array_chunk($values, 5); values will be equal to 值将等于

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

and using for loop we will loop over these sub arrays like this 并使用for循环,我们将像这样循环遍历这些子数组

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

For the JS code you are adding the same values multiple time do this instead 对于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());
        }
     }

}

Also for the insert function do just this 同样对于插入功能做到这一点

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM