[英]How To Insert multiple values from a single form at once using PHP and MySQL
我試圖使用php在mysql中插入多個名稱及其對應的姓氏,但似乎出了點問題。 我嘗試了google和這個網站,但是我還沒有最好的答案來滿足我的要求。我也設法插入了它們,但是姓氏可能使用了錯誤的用戶名,然后我決定刪除該代碼,然后開始一個新的代碼。任何幫助我們將不勝感激。以下是我嘗試過的。
<form action ="dependant_Process_Trevor.php?memberid=<?php echo $_GET['memberid']; ?>" name="contactform" method="post" id ="---contactform" >
<input type="hidden" name ="memberid" value="<?php echo $_GET['memberid']; ?>" >
<div class="box-header">
</div>
<!-- Start Trevor -->
<div class="box box-danger">
<div class="box-header with-border">
<h3 class="box-title">Dependents / Children under the age of 18 years - maximum 6 children</h3>
</div>
<!-- First child -->
<div class="box-body">
<div class="row">
<div class="col-xs-3">
<input type="text" name ="name[]"class="form-control" placeholder="Name">
</div>
<div class="col-xs-4">
<input type="text" name ="surname[]" class="form-control" placeholder="Surname">
</div>
<div class="col-xs-5">
<label>parent_type</label>
<select name ="parent_type[]" ">
<option value ="" >Select Type</option>
<option name ="parent_type[]" value ="Son">Son</option>
<option name ="parent_type[]" value ="Daughter">Daughter</option>
<option name ="parent_type[]" value ="Brother">Brother</option>
<option name ="parent_type[]" value ="Grandchild">Grandchild</option>
<option name ="parent_type[]" value ="Nephew">Nephew</option>
<option name ="parent_type[]" value ="Niece">Niece</option>
</select>
</div>
</div>
</div><!-- /.box-body -->
<!-- End second child -->
<!-- Start second child -->
<div class="box-body">
<div class="row">
<div class="col-xs-3">
<input type="text" name ="name[]"class="form-control" placeholder="Name">
</div>
<div class="col-xs-4">
<input type="text" name ="surname[]" class="form-control" placeholder="Surname">
</div>
<div class="col-xs-5">
<label>parent_type</label>
<select name ="parent_type[]" ">
<option value ="" >Select Type</option>
<option name ="parent_type[]" value ="Son">Son</option>
<option name ="parent_type[]" value ="Daughter">Daughter</option>
<option name ="parent_type[]" value ="Brother">Brother</option>
<option name ="parent_type[]" value ="Grandchild">Grandchild</option>
<option name ="parent_type[]" value ="Nephew">Nephew</option>
<option name ="parent_type[]" value ="Niece">Niece</option>
</select>
</div>
</div>
</div><!-- /.box-body -->
<!-- End second child -->
<!-- Third child -->
<div class="box-body">
<div class="row">
<div class="col-xs-3">
<input type="text" name ="name[]"class="form-control" placeholder="Name">
</div>
<div class="col-xs-4">
<input type="text" name ="surname[]" class="form-control" placeholder="Surname">
</div>
<div class="col-xs-5">
<label>parent_type</label>
<select name ="parent_type[]" ">
<option value ="" >Select Type</option>
<option name ="parent_type[]" value ="Son">Son</option>
<option name ="parent_type[]" value ="Daughter">Daughter</option>
<option name ="parent_type[]" value ="Brother">Brother</option>
<option name ="parent_type[]" value ="Grandchild">Grandchild</option>
<option name ="parent_type[]" value ="Nephew">Nephew</option>
<option name ="parent_type[]" value ="Niece">Niece</option>
</select>
</div>
</div>
</div><!-- /.box-body -->
<!-- End third child -->
<!-- Fourth child -->
<div class="box-body">
<div class="row">
<div class="col-xs-3">
<input type="text" name ="name[]"class="form-control" placeholder="Name">
</div>
<div class="col-xs-4">
<input type="text" name ="surname[]" class="form-control" placeholder="Surname">
</div>
<div class="col-xs-5">
<label>parent_type</label>
<select name ="parent_type[]" ">
<option value ="" >Select Type</option>
<option name ="parent_type[]" value ="Son">Son</option>
<option name ="parent_type[]" value ="Daughter">Daughter</option>
<option name ="parent_type[]" value ="Brother">Brother</option>
<option name ="parent_type[]" value ="Grandchild">Grandchild</option>
<option name ="parent_type[]" value ="Nephew">Nephew</option>
<option name ="parent_type[]" value ="Niece">Niece</option>
</select>
</div>
</div>
</div><!-- /.box-body -->
<!-- End second child -->
<!-- Fifth child -->
<div class="box-body">
<div class="row">
<div class="col-xs-3">
<input type="text" name ="name[]"class="form-control" placeholder="Name">
</div>
<div class="col-xs-4">
<input type="text" name ="surname[]" class="form-control" placeholder="Surname">
</div>
<div class="col-xs-5">
<label>parent_type</label>
<select name ="parent_type[]" ">
<option value ="" >Select Type</option>
<option name ="parent_type[]" value ="Son">Son</option>
<option name ="parent_type[]" value ="Daughter">Daughter</option>
<option name ="parent_type[]" value ="Brother">Brother</option>
<option name ="parent_type[]" value ="Grandchild">Grandchild</option>
<option name ="parent_type[]" value ="Nephew">Nephew</option>
<option name ="parent_type[]" value ="Niece">Niece</option>
</select>
</div>
</div>
</div><!-- /.box-body -->
<!-- End fifth child -->
<!-- six child -->
<div class="box-body">
<div class="row">
<div class="col-xs-3">
<input type="text" name ="name[]"class="form-control" placeholder="Name">
</div>
<div class="col-xs-4">
<input type="text" name ="surname[]" class="form-control" placeholder="Surname">
</div>
<div class="col-xs-5">
<label>parent_type</label>
<select name ="parent_type[]" ">
<option value ="" >Select Type</option>
<option name ="parent_type[]" value ="Son">Son</option>
<option name ="parent_type[]" value ="Daughter">Daughter</option>
<option name ="parent_type[]" value ="Brother">Brother</option>
<option name ="parent_type[]" value ="Grandchild">Grandchild</option>
<option name ="parent_type[]" value ="Nephew">Nephew</option>
<option name ="parent_type[]" value ="Niece">Niece</option>
</select>
</div>
</div>
</div><!-- /.box-body -->
<!-- End six child -->
</div><!-- /.box -->
<!-- End Trevor -->
<div class="box-body">
<!-- Date dd/mm/yyyy -->
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">
</div>
</div><!-- /.input group -->
</div><!-- /.form group -->
<input type="submit" name ="submit" value ="Save Data" class="btn-primary">
</form>
下面是我的PHP
//$surname =(isset($_POST['surname']))? trim ($_POST['surname']):'';
$recieved_by =($_SESSION['username']);
$othername =(isset($_POST['othername']))? trim ($_POST['othername']):'';
$gender =(isset($_POST['gender']))? trim ($_POST['gender']):'';
//$parent_type =(isset($_POST['parent_type']))? trim ($_POST['parent_type']):'';
$id_passport =(isset($_POST['id_passport']))? trim ($_POST['id_passport']):'';
$tel =(isset($_POST['tel']))? trim ($_POST['tel']):'';
$work_contact =(isset($_POST['work_contact']))? trim ($_POST['work_contact']):'';
$address =(isset($_POST['address']))? trim ($_POST['address']):'';
$work_address =(isset($_POST['work_address']))? trim ($_POST['work_address']):'';
$comments =(isset($_POST['comments']))? trim ($_POST['comments']):'';
$cell =(isset($_POST['cell']))? trim ($_POST['cell']):'';
//cell convert to 27
if(isset($_POST['submit'])){
foreach($_POST['name'] as $name)
foreach ($_POST['surname'] as $surname)
foreach($_POST['parent_type'] as $parent_type )
$count = count($_POST['name']);
if(is_array($name) && is_array($surname) && is_array($parent_type)) {
for($i = 0; $i < $count; $i++) {
//$name = $_POST['name'][$i], $_POST['surname'][$i] , $_POST['parent_type'][$i];
//echo $name;
///
#########################################################
$query ='INSERT INTO dependants(
name, surname, othername, gender, id_passport,
cell, tel, work_contact, address, work_address, comments,
memberid,companyid, joined_date, usercode, recieved_by , deadalive
)
VALUES
(
:name, :surname, :othername, :gender, :id_passport,
:cell, :tel, :work_contact, :address, :work_address, :comments,
:memberid,:companyid, :joined_date, :usercode, :recieved_by ,:deadalive
)';
$insert=$con->prepare($query);
$insert->execute(array(
':name'=>$name,
':surname'=>$surname,
':othername'=>$othername,
':gender'=>$gender,
':id_passport'=>$id_passport,
':cell'=>$realcell,
':tel'=>$tel,
':work_contact'=>$work_contact,
':address'=>$address,
':work_address'=>$work_address,
':comments'=>$comments,
':memberid'=>$memberid,
':usercode'=>$usercode,
':joined_date'=>$joined_date,
':companyid'=>$companyid,
':recieved_by'=>$recieved_by,
':deadalive'=>$deadalive
));
}}}
if($query){
echo '<h1 style="color:green">Dependant Created</h1>';
}
我認為有一些問題,首先是大多數代碼是多余的...
foreach($_POST['name'] as $name)
foreach ($_POST['surname'] as $surname)
foreach($_POST['parent_type'] as $parent_type )
$count = count($_POST['name']);
由於foreach()
沒有{}
所有循環最終都將只執行最后一行,這將始終是相同的。 因此,刪除3個foreach()
循環即可
$count = count($_POST['name']);
然后為每個循環設置值,您幾乎已經擁有(我認為)並且需要...
$name = $_POST['name'][$i];
$surname = $_POST['surname'][$i];
最后,您進行測試以查看其是否有效
if($query){
是不正確的,您應該檢查所有
$insert->execute(array(...));
語句返回true
,這應表明它們已全部創建。
我最終在@Nigel Ren的幫助下得到了答案,很好,這里是答案。
if(isset($_POST['submit'])){
$surname = $_POST['surname'];
$name = $_POST['name'] ;
$parent_type = $_POST['parent_type'];
$count = count($_POST['name']);
if(is_array($name) && is_array($surname) && is_array($parent_type)) {
for($i = 0; $i < $count; $i++) {
var_dump($name[$i]);
$surname1 = $surname[$i];
$name1 =$name[$i];
$parent_type1 = $parent_type[$i];
///
#########################################################
$query ='INSERT INTO dependants(
name, surname, othername, gender, id_passport,
cell, tel, work_contact, address, work_address, comments,
memberid,companyid, joined_date, usercode, recieved_by , deadalive,parent_type
)
VALUES
(
:name, :surname, :othername, :gender, :id_passport,
:cell, :tel, :work_contact, :address, :work_address, :comments,
:memberid,:companyid, :joined_date, :usercode, :recieved_by ,:deadalive,:parent_type
)';
$insert=$con->prepare($query);
$insert->execute(array(
':name'=>$name1,
':surname'=>$surname1,
':othername'=>$othername,
':gender'=>$gender,
':id_passport'=>$id_passport,
':cell'=>$realcell,
':tel'=>$tel,
':parent_type'=>$parent_type1,
':work_contact'=>$work_contact,
':address'=>$address,
':work_address'=>$work_address,
':comments'=>$comments,
':memberid'=>$memberid,
':usercode'=>$usercode,
':joined_date'=>$joined_date,
':companyid'=>$companyid,
':recieved_by'=>$recieved_by,
':deadalive'=>$deadalive
));
}}}// all end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.