php 输入框 调用数据库字段,PHP -获取数据库输入的字段输入数

I have a form which looks like this:

$numOfCols = 4;

$rowCount = 0;

$bootstrapColWidth = 12 / $numOfCols;

$i=1;

for ($x=1;$x<=8;$x++) {

".$i.". Data

Firstname:*

Lastname:*

";

Field A:*

";

Field B:*

$rowCount++;

$i++;

if($rowCount % $numOfCols == 0) {

}

}

So as you can see I have a for-loop which shows 8x times the same fields. It is up to the user if he enters only one person or up to eight persons.

The form will be submitted this way:

if ($savedata=="true") {

/* create db connection */

$link = mysqli_connect("localhost", "USER", "PASSWORD", "DATABASE");

// check connection

if($link === false){

$error_db_connection=1;

}

// insert entries into database

for ($run=1;$run<=8;$run++) {

$var_firstname="firstname_".$run;

$var_lastname="lastname_".$run;

$var_field_a="field_a_".$run;

$var_field_b="field_b_".$run;

$sql = "INSERT INTO TABLENAME (firstname, lastname, praefix_title, postfix_title) VALUES ('".$$var_firstname."', '".$$var_lastname."', '".$$var_field_a."', '".$$var_field_b."')";

if(mysqli_query($link, $sql)){

$success=1;

} else{

$error=1;

}

}

// close connection

mysqli_close($link);

}

I am using again a for-loop where the total number of loops is hardcoded right now (8). What I need to achive is, that the hardcoded total number of loops is dynamic. So if a user fills out 3x the fields (firstname, lastname, field_a, field_b) than only 3 records should be inserted into my database. If a user fills out all 8 fields, than 8 records should be inserted into my database.

Right now I always get 8 entries into my database, also if a user only fills out one entry. Can you help me how am I able to achive what I need?

Every form field has his own unique name and id because of the $i that I am counting. Maybe I need to send a hidden field with the total number of filled fields? If so, how do I do that? Or any other approach?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值