【问题标题】:How to insert value of dynamic form fields in database iusing code in php如何使用php中的代码在数据库中插入动态表单字段的值
【发布时间】:2016-08-07 01:35:32
【问题描述】:

我在尝试使用动态表单字段将数据库值存储在数据库内的相应行中时遇到问题。我在这里提供了我的代码和输出的屏幕截图。

This is my script in adding dynamic form fields in which the form is located in a separate file named load_work_experience_form.php

This is the html code for the form I have appended in the my script to add a dynamic form fields

This is the look of my dynamic form fields

This happens to be the wrong output inside the database in which data values do not store in their proper record. I am attempting to insert 2 records of work experience but it seems that it has created 4 records.

The source code for adding into the database is supplied below. Kindly help me in fixing this problem. Thanks. More power:

<!--ADD WORK EXPERIENCE TO DATABASE -->
  <?php
    require'../admin/php/db_connection.php';
    if(isset($_POST['update_profile']))
    {

      if (isset($_POST['employer'])) {
        foreach ( $_POST['employer'] as $value ) {
        $values = mysql_real_escape_string($value);
        $query = mysql_query("INSERT INTO tbl_work_exp (employer) VALUES ('$values')");

        }}

        if (isset($_POST['job_position'])) {
        foreach ( $_POST['job_position'] as $value ) {
        $values = mysql_real_escape_string($value);
        $query = mysql_query("INSERT INTO tbl_work_exp (job_position) VALUES ('$values')");

        }}
//some more codes here for Work From and To. This website does not accept alot of codes. But the codes here are just like the ones at the top.

    }
  ?>
<!--ADD WORK EXPERIENCE TO DATABASE -->

【问题讨论】:

    标签: php mysql forms


    【解决方案1】:

    如果您每次都拥有具有动态行的相同字段:

    <!--ADD WORK EXPERIENCE TO DATABASE -->
      <?php
        require'../admin/php/db_connection.php';
        if(isset($_POST['update_profile']))
        {
            if (isset($_POST['employer'])) {
                for ($i = 0, $nCount = count($_POST['employer']); $i < $nCount; $i++) {
                    $employer = mysql_real_escape_string($_POST['employer'][$i]);
                    $job_position = mysql_real_escape_string($_POST['job_position'][$i]);
                    $query = mysql_query('INSERT INTO tbl_work_exp (´employer´, ´job_position´) VALUES (´' . $employer . '´, ´' . $job_position . '´)');
                }
            }
        }
      ?>
    <!--ADD WORK EXPERIENCE TO DATABASE -->
    

    您还必须在其中添加其他字段...

    您的逻辑问题是您为每个字段执行一次插入,但您只需要为一行插入一次。

    并且请不要在php中使用mysql库,最好使用mysqli

    【讨论】:

      【解决方案2】:

      您的问题是您分别传递每个值。我强烈建议更改您的 HTML 标记以对每个值进行分组,但这不是这里的目标。至于您当前的问题,这是一个可以帮助您解决问题的快速解决方案。从您当前的代码中提取:

      <?php
      if (isset($_POST['update_profile'])) {
          $profileFields = array('employer', 'job_position');
          $profiles = array();
      
          foreach ($profileFields as $field)
          {
              if (isset($_POST[$field])) {
                  foreach ($_POST[$field] as $key => $value) {
                      if (!isset($profiles[$key])) {
                          $profiles[$key] = array();
                      }
                      $profiles[$key][$field] = mysql_real_escape_string($value);
                  }
              }
          }
      
          foreach ($profiles as $profile) {
              $tableCols = implode(",", array_keys($profile));
              $profileValues = implode("','", array_values($profile));
              $insertQuery = "INSERT INTO tbl_work_exp ({$tableCols}) VALUES ('{$profileValues}')";
          }
      }
      ?>
      

      对每个字段进行一些调整或特殊处理。这是非常通用的代码,只是为了给你一个指导。

      希望对你有帮助

      【讨论】:

        【解决方案3】:
        <?php
        if(isset($_POST['Button_name'])){
        for($i=0; $i<count($_POST['employer_name']); $i++){
        $query="INSERT INTO table_name(employer_name,employer_age) VALUES ('".$_POST['employer_name']."','".$_POST['employer_age']."')";
        $result=mysql_query($query);}}
        

        【讨论】:

        • 感谢您提供此代码 sn-p,它可能会提供一些即时帮助。一个正确的解释would greatly improve 其教育价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有类似但不相同的问题的读者更有用。请edit您的答案添加解释,并说明适用的限制和假设。
        【解决方案4】:
        <?php 
        $count_post_value = $_POST['first_name'] //this is value of text box //
        
        
        for($i=0; $i<$count_post_value; $i++) 
        { 
        if(trim($_POST["first_name"][$i] && $_POST["last_name"] && $_POST["remarks"]!= '')) 
        {   
        
        $sql = mysql_query("INSERT INTO Table_name(first_name,last_name) VALUES('".$_POST["first_name"][$i]."','".$_POST["last_name"][$i]."')"); 
        if($sql) 
        {
        echo "<script>alert('Inserted Successfully');</script>";
        }
        
        
        else 
        {
        echo "<script>alert('ERROR');</script>";
        }
        
        }
        
        }  
        
        ?>
        

        【讨论】:

          猜你喜欢
          • 2018-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-14
          • 2021-01-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多