Search code examples
phppdorepeater

Insert jQuery repeater row in PHP database


I have 1 row having 5 form fields. User can add/remove rows. Its repeatable row. Now i want to store these fields into database with PDO php.

For normal values i am using this code but i am confused for repeater field.

$data = array(
    'bill_no' => trim($_REQUEST['bill_no']),
    'from_name' => trim($_REQUEST['from_name']),
    'to_name' => trim($_REQUEST['to_name']),
    'date' => trim($_REQUEST['date_bill']),
    'mr_or_ms' => trim($_REQUEST['mr_or_ms']),
);

if($crud->InsertData("bill",$data)) {
    header("Location: add-bill.php");
}

Insert Function:

public function InsertData($table,$fields) {
    $field = array_keys($fields);

    $single_field = implode(",", $field);
    $val = implode("','", $fields);

    try {
        $query = $this->db->prepare("INSERT INTO ".$table."(".$single_field.") VALUES('".$val."')");

        $query->execute();
        return true;
    } catch(PDOException $e) {
        echo "unable to insert data";
    }
}

Please help me to insert fields. Thanks


Solution

  • I did it with this method.

    $total=count($_POST['description']);
    for($i=0; $i<$total; $i++){
        $data1 = array(
            'bill_no' => trim($_POST['bill_no']),
            'description' => trim($_POST['description'][$i]),
            'nos' => trim($_POST['nos'][$i]),
            'nos_day' => trim($_POST['nos_day'][$i]),
            'pay' => trim($_POST['pay'][$i]),
            'weekly_off' => trim($_POST['weekly'][$i]),
            'hra' => trim($_POST['hra'][$i]),
            'rs' => trim($_POST['rs'][$i]),
            'ps' => trim($_POST['ps'][$i]),
    
        );
        $crud->InsertData("bill_details",$data1);
    }