如何使用PHP / WordPress中的外键将重复字段保存到数据库? [英] How to save repetitive fields to database with a foreign key in PHP / WordPress?

查看:139
本文介绍了如何使用PHP / WordPress中的外键将重复字段保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以我的形式重复/重复以下字段:

 < input type =textname =BrotherAdmissionNumber []/> 
< input type =textname =BrotherName []/>
< input type =textname =BrotherGrade []/>
< input type =textname =BrotherClassTr []/>

外键来自:



$ unique_id_generated - 这是在表单提交过程中生成的并保存在一个名为admission的表中



我想要做的是,



我想把这个插入到一个有一个兄弟细节的行中。



所以让我们假设外键是ZKHYT6

  FK入学否名称等级Tr 
--- --- ------------ ----- ------- ------------
ZKHYT6 7721 John 10 Mrs. Johnathan
ZKHYT6 8975 Bobby 09 Mr. Parkinson
ZKHYT6 6585 Smith 11 Mrs. Camp

我想将此插入到WordPress中名为 brothers 的自定义表格中。
如何使用表格' ''从外部键设置此表$ unique_id_generated

  $ wpdb-> insert(
'brothers ',
array(
'applicationID'=> $ unique_id_generated,
'BrotherAdmissionNumber'=> $ BrotherAdmissionNumber,
'BrotherName'=> $ BrotherName,
'BrotherGrade'=> $ BrotherGrade,
'BrotherClassTr'=> $ BrotherClassTr

);

上述操作将向表' brothers / p>

如何用外键插入重复的字段?

解决方案


更好地在所有字段中使用是必需的,否则数据将保存为空白。或者,您必须至少使用一个字段,其中一个字段不能设置为空。在这个字段数组的基础上,我们可以根据你的需要完成任务。例如,我根据需要设置 BrotherAdmissionNumber 字段。




 < input type =textname =BrotherAdmissionNumber []required =True/> 
< input type =textname =BrotherName []/>
< input type =textname =BrotherGrade []/>
< input type =textname =BrotherClassTr []/>




提交表单后 -




 <?php 

$ BrotherAdmissionNumber = $ _POST ['BrotherAdmissionNumber'];
$ BrotherName = $ _POST ['BrotherName'];
$ BrotherGrade = $ _POST ['BrotherGrade'];
$ BrotherClassTr = $ _POST ['BrotherClassTr'];

foreach($ brotherAdmissionNumber as $ key => $ v){
$ data = array(
'applicationID'=> $ unique_id_generated,
'BrotherAdmissionNumber '=> $ v [$ key],
'BrotherName'=> $ BrotherName [$ key],
'BrotherGrade'=> $ BrotherGrade [$ key],
' BrotherClassTr'=> $ BrotherClassTr [$ key]
);
}
?>




现在只需传递 Array $ data 到您的查询以插入兄弟表。正在尝试 -




 <?php $ wpdb-& ',$ data); ?> 


The following fields are repetitive / repeated in my form:

<input type="text" name="BrotherAdmissionNumber[]" />
<input type="text" name="BrotherName[]" />
<input type="text" name="BrotherGrade[]" />
<input type="text" name="BrotherClassTr[]" />

the foreign key comes from this:

$unique_id_generated - this is generated during the form submission and saved in a table called 'admission'

What I am trying to do this is,

I want to insert this into db like one row with one brother details.

so let's say if the foreign key is ZKHYT6 then the rows should be like this for a person who has three brothers.

FK      admission No   Name    grade    Tr
------  ------------   -----   -------  ------------
ZKHYT6  7721           John     10      Mrs. Johnathan
ZKHYT6  8975           Bobby    09      Mr. Parkinson
ZKHYT6  6585           Smith    11      Mrs. Camp

And I want to insert this to a custom table called 'brothers' in WordPress. How to set up this table with a foreign key $unique_id_generated from the table 'admission'

I can do this like below only if these are normal fields

$wpdb->insert( 
    'brothers', 
    array(
        'applicationID' => $unique_id_generated, 
        'BrotherAdmissionNumber' => $BrotherAdmissionNumber, 
        'BrotherName' => $BrotherName, 
        'BrotherGrade' => $BrotherGrade,
        'BrotherClassTr' => $BrotherClassTr 
    )
);

The above will insert a single row to the table 'brothers'

How can I insert the repeated fields with a foreign key?

解决方案

Better to use in all the fields are required, other wise the data will be saved as blank. Or, You have to use at least one field which one cannot be set as blank. On the basis of that field array we can do the task as you desire. Such as- I'm setting BrotherAdmissionNumber field as required.

<input type="text" name="BrotherAdmissionNumber[]" required="True"/>
<input type="text" name="BrotherName[]" />
<input type="text" name="BrotherGrade[]" />
<input type="text" name="BrotherClassTr[]" />

After submission of your form -

<?php 

  $BrotherAdmissionNumber = $_POST['BrotherAdmissionNumber'];
  $BrotherName = $_POST['BrotherName'];
  $BrotherGrade = $_POST['BrotherGrade'];
  $BrotherClassTr = $_POST['BrotherClassTr'];

  foreach($BrotherAdmissionNumber as $key => $v){
      $data = array(
         'applicationID' => $unique_id_generated, 
         'BrotherAdmissionNumber' => $v[$key], 
         'BrotherName' => $BrotherName[$key], 
         'BrotherGrade' => $BrotherGrade[$key],
         'BrotherClassTr' => $BrotherClassTr[$key]
      );
  }
?>

Now Just Pass the Array $data to your query to Insert into brothers table. As you were trying -

<?php $wpdb->insert( 'brothers',$data); ?>

这篇关于如何使用PHP / WordPress中的外键将重复字段保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆