Insert multiple rows into database based on form data entry using PHP & mySQL
-
I am looking for source code in PHP that does the following: 1. Reads the values from one table in a mySQL database (DB: kw, tablename: kw_emps, fields: emp_no, con, last_name, first_name, loc) 2. Format the results into an HTML table/form that adds two additional columns using editable text boxes. Date & Hours 3. On submit the HTML form should insert into a seperate table (kw_time) the following values: emp_no, date(from the text box) and Hours (also from the text box) for all of the rows, not just one at a time. This is part of a simple time entry system that will allow managers to enter time for employees in the field. I need to capture the Date & the Hours based on employee number into the database. My problem is updating multiple rows of a mySQL DB & PHP using one form at the same time.
-
Answer:
Greetings cajler-ga, I have done this sort of multi-insert time entry system for a client before, so I was able to conjure the logic up with help from my memory (and from the PHP/MySQL documentation!). With some error checking and formatting clean-up, it should plug into your application easily. Your 3 requirements in the question are noted in comments within the script below. I tend to pop in and out of PHP code a bit, but hopefully the indentation will remain when I post this and you can follow along. This was tested on an Apache web server running PHP 4 and MySQL. If you have any questions at all, please don't hesitate to ask for clarification. I do hope this helps! -- mother-ga Additional Resources: PHP Documentation http://www.php.net <?php /* Connect and selecting database - modify for your own server */ mysql_connect ("YOURHOST", "USERNAME", "PASSWORD"); mysql_select_db ("kw"); /* If time entries submitted */ if ($function=="insert") { /* Printing HTML : Start entry added table for verification */ ?> <B>Entries added to kw_time: </B><BR> <table> <tr> <td>Employee No</td> <td>Date</td> <td>Hours</td> </tr> <?php /* 3. On submit the HTML form should insert into a seperate table (kw_time) the following values: emp_no, date(from the text box) and Hours (also from the text box) for all of the rows, not just one at a time. */ /* Loop through the entries followed by counter "i" */ for ($i=0; $i<$num_rows; $i++) { /* Assign dynamic variable to arrays, and then assign each key */ $emp_info = "emp_".$i; list($emp_no, $date, $hours) = $$emp_info; /* Print and run insert queries for each entry array */ $i_query = "INSERT INTO kw_time (emp_no, date, hours) VALUES ($emp_no, \"$date\", $hours)"; $i_result = mysql_query ($i_query); /* Printing HTML : Show added entries */ ?> <tr> <td><?php echo $emp_no ?></td> <td><?php echo $date ?></td> <td><?php echo $hours ?></td> </tr> <?php } ?> </table> <?php } /* 1. Reads the values from one table in a mySQL database (DB: kw, tablename: kw_emps, fields: emp_no, con, last_name, first_name, loc) /* /* Performing SQL query */ $query = "SELECT * FROM kw_emps"; $result = mysql_query ($query); /* Print and populate employee date and hours table */ /* Printing HTML : Start table and form */ ?> <form action="<?php echo $SCRIPT_NAME ?>?function=insert" method="POST"> <table> <tr> <td>Employee No</td> <td>Con</td> <td>Last Name</td> <td>First Name</td> <td>Loc</td> <td>Date</td> <td>Hours</td> </tr> <?php /* 2. Format the results into an HTML table/form that adds two additional columns using editable text boxes. Date & Hours (Keep date in MySQL format: YYYY-MM-DD) */ for ($count = 0; $row = mysql_fetch_array($result); $count++) { ?> <tr> <input type="hidden" name="emp_<?php echo $count ?>[]" value="<?php echo $row[0] ?>"> <td><?php echo $row[0] ?></td> <td><?php echo $row[1] ?></td> <td><?php echo $row[2] ?></td> <td><?php echo $row[3] ?></td> <td><?php echo $row[4] ?></td> <td><input type="text" name="emp_<?php echo $count ?>[]" size=11 value="<?php echo date('Y-m-d') ?>" maxlength=10></td> <td><input type="text" name="emp_<?php echo $count ?>[]" size=4 value="0" maxlength=4></td> </tr> <?php } ?> <input type="hidden" name="num_rows" value="<?php echo $count ?>"> </table> <input type="submit" value=" Add Entries "> </form>
cajler-ga at Google Answers Visit the source
Related Q & A:
- Where We Find Data Entry Projects?Best solution by Yahoo! Answers
- How To Build Business Directory Using Php Mysql?Best solution by Stack Overflow
- How To Earn Money By Data Entry Without Investment?Best solution by justclickin.com
- How to move up/down multiple rows?Best solution by Super User
- how to insert multiple rows from MySQL with checkbox?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.