如何在php中使用代码插入动态表单字段的值

时间:2016-04-15 07:38:20

标签: php mysql forms

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

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

4 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您的问题是您分别传递了每个值。我强烈建议您更改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}')";
    }
}
?>

为每个领域提供或进行一些调整或特殊处理。这是非常通用的代码,仅为您提供指导。

希望这有帮助

答案 2 :(得分:0)

<?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);}}

答案 3 :(得分:0)

<?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>";
}

}

}  

?>