multiple form insert query mysql

is there anyone know how to format this array in a loop so that it will insert in a database in a single query but in a loop way using php.

I'm making a multiple form by the way.

here's the array format of the field and this is my form looks like multiple form insert query mysql

Array ( [sup_payroll_type] => Array ( [0] => 1 [1] => 2 [2] => 1 ) [sup_month] => Array ( [0] => January [1] => January [2] => May ) [sup_year] => Array ( [0] => 2015 [1] => 2015 [2] => 2015 ) [sup_late_days] => Array ( [0] => 1 [1] => 1 [2] => 2 ) [sup_absent_days] => Array ( [0] => 2 [1] => 1 [2] => 0 ) [sup_id] => Array ( [0] => 1 [1] => 5 [2] => 6 ) [sup_emp_id] => Array ( [0] => 24 [1] => 24 [2] => 24 ) [save_edit_satt] => Array ( [0] => SAVE ) )

any help would be appreciated.

-------------Problems Reply------------

You can do with using a loop only

$insertQuery = 'INSERT INTO attendence (sup_payroll_type, sup_month, sup_year,
sup_late_days, sup_absent_days, sup_id, sup_emp_id,
save_edit_satt) VALUES';
foreach($attendenceArray['sup_payroll_type'] AS $key => $value){
$insertQuery .= '("'.$value.'","'.$attendenceArray['sup_month'][$key].'",
"'.$attendenceArray['sup_year'][$key].'","'.$attendenceArray['sup_late_days'][$key].'",
"'.$attendenceArray['sup_absent_days'][$key].'","'.$attendenceArray['sup_id'][$key].'",
"'.$attendenceArray['sup_emp_id'][$key].'","'.$attendenceArray['save_edit_satt'][$key].'"),';
}

echo $insertQuery; // Query for insert

Please try below loop:-

$insertQuery = 'INSERT INTO attendence (sup_payroll_type, sup_month, sup_year,
sup_late_days, sup_absent_days, sup_id, sup_emp_id,
save_edit_satt) VALUES';
foreach($attendenceArray AS $key => $value){
$insertQuery .= '("'.$value['sup_payroll_type'].'","'.$value['sup_month'].'",
"'.$value['sup_year'].'","'.$value['sup_late_days'].'",
"'.$value['sup_absent_days'].'","'.$value['sup_id'].'",
"'.$value['sup_emp_id'].'","'.$value['save_edit_satt'].'"),';
}
$insertQuery = rtrim($insertQuery,',');

$insertQuery contains ur insert query. $attendenceArray contains ur example array.

If ur using mysqli then used mysqli_query($mysqli,$insertQuery) to execute ur query. $mysqli is connection object.

i figured it out.

$ctr = 0;
foreach($_POST['sup_payroll_type'] as $row){

$upd_value = array();
$upd_value['sup_payroll_type'] = $_POST['sup_payroll_type'][$ctr];
$upd_value['sup_month'] = $_POST['sup_month'][$ctr];
$upd_value['sup_year'] = $_POST['sup_year'][$ctr];
$upd_value['sup_late_days'] = $_POST['sup_late_days'][$ctr];
$upd_value['sup_absent_days'] = $_POST['sup_absent_days'][$ctr];
$upd_value['sup_emp_id'] = $_POST['sup_emp_id'][$ctr];

$upd_result = $db_conn->mysql_update($upd_value,'tbl_attendance_support','sup_id='.$_POST['sup_id'][$ctr],'db_norkis');

$ctr++;
}

BTW thanks Pankaj K and Shijin for sharing your idea =)

Category:php Views:11 Time:2018-09-08
Tags: php mysql

Related post

  • Insert Multiple Form select to Mysql 2011-12-12

    Need help with PHP Mysql code to insert multiple select data into database. With the current code i only get "Arry" in the ccaid field in the MySQL database, it should contain the Md5 code generated by first code. Here is the code for Multiple Select

  • Multiple records inserting into MySql from PHP form 2012-03-10

    I think I've got a tiny mistake in my code so when I try to add two records to MySQL database it adds them both, but at the moment its only adding the second row that should be inputted. SO for example I have two RefTitle fields, two RefSurname field

  • PHP Form Inserting to MySQL NULL Values Allowed 2011-08-12

    I have a form which submits to a php file which inserts data to a table in MySQL there a some fields in this form which may not be filled in and if this is the case the record doesn't get inserted even though I have set the field in MySQL to accept n

  • Assign Common ID to multiple row insert from MySQL array 2011-10-02

    I have a PHP script that inserts rows into a table based on selected rows from a MySQL array. the code to insert the rows into the new table is: $sql="insert into loaddetails (CaseNo,GrossMass,CaseStatus,Customer) select `case no`,`gross mass`,`case

  • Multiple Record Insert in mysql using Json from an android app 2014-04-09

    i want to insert multiple record in MySQL database using Json from My Android app if anyone have idea to insert multiple record in MySQL than tell me. Thanks. That's My Code: String TempDial = ""; Cursor cursord = class1.GetDial(); if (cursord.moveTo

  • Multiple form with php mysql and jquery using tabs 2011-05-27

    am newbie to jquery..I have a three steps registration form in which the user have to finish the first form before get the other form till the final step in registration. Is there a way i could accomplish this using jquery tabs? am using codeiginter

  • insert query mysql and python 2012-03-06

    def get(infilename): fd_in = open(infilename, "r") try: con = mdb.connect (host=MY_HOST, user=MY_USER, passwd=MY_PASS, db=MY_DB) cur = con.cursor() insertQuery = "LOAD DATA LOCAL INFILE" + infilename + "INTO TABLE vlan_area (vlan_id, area)" In python

  • Inserting multiple values from multiple forms at once in PHP and MySQL 2012-04-09

    I am creating a system for my dissertation for a school to use. There's one aspect I can't get working though. I want to be able to set attendance for multiple people all at once. There's an image here that will show you what the form looks like: All

  • Inserting data from Multiple forms into multiple tables with MySQL 2013-09-17

    I have multiple forms that I am trying to insert data from into MySql. Each form has a different table associated with that information. I am using an insert.php file to insert the data. The first form works great. However when I add any other variab

  • Efficient Insert Query Against Multiple Tables in MySQL 2009-09-16

    I was wondering if there is a more efficient way of doing an insert in MySQL against multiple tables than a separate insert query for each record in each table. I was thinking of doing something like this: INSERT INTO table1 (t1c1, t1c2, t1c3), table

  • PHP Multiple Dropdown Box Form Submit To MySQL (Part 2) 2010-02-08

    This is a continuation of the discussion at http://stackoverflow.com/questions/944158/php-multiple-dropdown-box-form-submit-to-mysql which ended with the words: "Once you have the variables, it is trivial to create new rows." No doubt that's generall

  • Inserting Multiple Rows with PHP & MySQL 2011-11-22

    I'm building a website where people can place orders and this is the first time I've had to insert multiple rows at a time and I'm lost. I know that I need a FOR loop to perform this, but I'm lost as to how to construct the loop. I'm using PHP, MySQL

  • mysql insert query variable issues 2012-04-06

    Here is my query // Put it all into the database mysql_query("INSERT INTO orders ( item_id, order_id, contact_first_name, contact_last_name, contact_email, contact_phone, business_name, business_type, business_website, business_address, business_city

  • Looking for a Function that insert "Any" form values to mysql 2010-04-05

    I am Looking for a Function that insert "Any" form values to mysql , update or delete with php. (or can we do this with one function?) (not OOP) I have found a function here: example: get all fields of form and ınsert into database. Thanks in advance

  • Recover Generated Ids from Multiple Insert Query 2010-07-29

    I want to execute a query of the form: INSERT INTO table(field1, field2) SELECT field1, field2 FROM table WHERE id IN ( 4, 8, 15, 16, 23, 42 ) Where id is the auto_increment'ing primary key of table. After executing this statement, I want to know wha

  • MySQL's AUTO_INCREMENT behavior in a multiple row insert 2011-08-01

    I think the answer to my question is obvious but since I could not find any documentation to support it, I thought it's worth asking. At least for the record. As we all know AUTO_INCREMENT fields are incremented each time an INSERT statement is execu

  • multiple SQL insertion & form validation In php 2011-12-03

    I am trying to do multiple product add with a form using php and mysql , and i am confusing on the concept of doing these, My expecting output is , provide a form with at least ten rows of multiple field to be fill and do the validation among these ,

  • Concise way to check variable presence in MySQL INSERT QUERY using PHP 2009-08-26

    I am writing a PHP script to add items to a shopping basket. My shopping basket table has fields for the userid, product id, session id, notes and a few others. Some of the fields can be blank. For example: if someone isn't signed in, then I will sto

  • PHP Multiple Dropdown Box Form Submit To MySQL with OR 2009-11-26

    This question is similar to http://stackoverflow.com/questions/944158/php-multiple-dropdown-box-form-submit-to-mysql, but with a twist. Let's assume we have an HTML multi-select, which gets submitted to a PHP backend. How do you elegantly create a my

  • mysqli bind_params for an insert query 2010-02-09

    I'm trying to use call_user_func_array and mysqli_stmt::bind_param like so: # A. prepare an insert query statement $this->_stmt = $db_link->prepare('INSERT INTO foo (col1, col2) VALUES (?,?)'); # B. bind a placeholder array to the statement $bo

  • MySQLi PHP: Check if SQL INSERT query was fully successful using MySQLi 2010-05-20

    I have this big function that gets a lot of different data and insert it into multiple tables.. Not all the data is always available so not all the SQL INSERT queries are successful. I need to check which SQL INSERT query was fully successful and whi

  • Is there a way that i can read from mysql binarylog file with php code and insert query statement in DB? 2010-06-06

    I want insert query statement in DB for LOG. Is there a way that i can read from mysql binarylog file with php code and insert query statement in DB? --------------Solutions------------- Probably you can (it's a file, it's content, you can alter it),

  • exporting table records as insert in mysql using mysql query browser? 2010-09-29

    how to export table records as insert in mysql using mysql query browser? is there any other tool that can do it? --------------Solutions------------- The old school way is to use string concatenation: SELECT CONCAT('INSERT INTO YOUR_TABLE (col1, col

  • MySQL LAST_INSERT_ID() used with multiple records INSERT statement 2011-01-09

    If I insert multiple records with a loop that executes a single record insert, the last insert id returned is, as expected, the last one. But if I do a multiple records insert statement: INSERT INTO people (name,age) VALUES ('William',25), ('Bart',15

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.226 (s). 11 q(s)