multiple inserts from form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bugfreek1
Forum Newbie
Posts: 1
Joined: Tue Sep 05, 2006 3:47 pm

multiple inserts from form

Post by bugfreek1 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello all,

Getting stuck on a semi-complex form in an application to track volunteers for my employer. In this portion of the script, the administrator is assigning X volunteers to a timeslot for a given job function. That means that I have to get three values into the database multiple times, with the final number not known.

I've been approaching this by setting each value as an array (lately going at it with a two-dimensional array) then trying to loop over the results with a SQL statement to insert. While there are many outstanding tutorials on how to handle multiple select fields, none that I've found deals with inserting multiple fields of information multiple times. Any ideas out there?

The values in question are sid, pid, and qty. Assign() will pass the values to Processassign() for processing.

My form code (which seems to be working well):

Code: Select all

function assign()
{

-- excerpted code here --
echo'
<div class="leftmain">
<div class="headings">Shift Details</div>
<b>Position:</b><br>
'.$p['position_title'].'<br>
<br>
<b>Position Description:</b><br>
'.$p['position_desc'].'<br>

</div>
<div class="rightmain">
<div class="headings">Available Shifts</div>
<br>
<table class="rstyle">
<form name="editevent" method="GET" action=" ' . $_SERVER['SCRIPT_NAME'] . ' ">
<input type="hidden" name="mode" value="processassign" />
<input type="hidden" name="$arr[sid][]" value="'.$_GET['position_id'].'">
<input type="hidden" name="name" value="'.$_GET['name'].'">
<input type="hidden" name="$arr[pid][]" value="'.$_GET['position_id'].'">
';

$cur_date = "";
$shift = new pgsql(DBHOST,DB,DBUSER,DBPASS);
$shift->connect();
$sql="SELECT * FROM eventshifts WHERE event_id = '".$_GET['event_id']."' ORDER BY shift_starttime ASC;";
$shift->query($sql) or die("Could not execute query: ".$sql." - ".pg_last_error());



if ($shift->numRows() >=1){
//loop through results
for($i = 0; $i < $shift->numRows(); $i++){
$st = $shift->fetchObject($i);

$startdate = date("M j Y", strtotime($st->shift_starttime));
$starttime = date("g:i a", strtotime($st->shift_starttime));
$endtime = date("g:i a", strtotime($st->shift_endtime));

if ($cur_date != $startdate)
{
echo"
<tr><th colspan=2><b>$startdate</b></th>
<th>Qty.</th>
</tr>";
// now set the cur_* variables
$cur_date = $startdate;
}
// print the row data
echo'<tr>
<td align=right><b>'.$starttime.' - </b></td>
<td><b>'.$endtime.'</b></td>
<td><input type="text" name="$arr[qty][]" size="5"> </td>
</tr>';
}
} else {
echo'<tr><td colspan=2>No Shifts Created Yet.</td></tr>';
}
echo'
<tr>
<td colspan=2><input type="submit" value="Assign Shifts">
</tr>
</table>
</form>
</div>';
}

function processassign()
{
// Insert data from table above.

}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

bugfreek1,

U can insert multiple rows of data in one SQL Statement

Code: Select all

INSERT INTO table (column1,column2,column3) VALUES(row1a,row1b,row1c), (row2a,row2b,row2c,)....etc
you just need to have php do a complex build of the statement...its not that hard
Post Reply