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

rslzwgfq  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(280)
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");
}

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

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

按submit按钮时,数组的所有这些值都插入到表中

$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,依此类推。

cedebl8k

cedebl8k1#

$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_group0 以及 name1, 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());
        }
     }

}

对于insert函数,也可以这样做

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

相关问题