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!
hey i have a loops that writes down 'Training Sessions'. i need a button that when a user presses it it adds their name to the 'usersattending' field. There has to be one of these buttons for each of the Training Sessions.
Well I wanted to help you out, but your code needed a bit of work so I took a few minutes and refactored it a bit. I hope this gives you some insight. It's not going to work out of the box as it needs to be customized to your specs and it's untested as well. I haven't checked it for any errors.
<?php
/**
There are several problems with the original code..
1. Each form requires a unique name.
2. There was no field named inner being posted.
3. The form submission processing code should not be
mixed in with the display while loop.
Looking at your original code is was hard for me to
determine exactly what you were trying to do, but
I'll rewrite it and hopefully give you a good head
start..
**/
// process any post submissions
if(!empty($_POST['process']))
{
switch ($_POST['process'])
{
case 'new_inner':
// you will need to add your own validation code here..
// is this already set someplace??
$username = $_SESSION['username'];
// are you sure you want an update query?
// that would mean there can't be multiple
// attendees wouldn't it?
$inner = mysql_query("UPDATE `training_sessions` "
."SET `usersattending`=" . mysql_real_escape_string($username) . "', ' "
."WHERE `sessionID`='" . mysql_real_escape_string($_POST['UNIQUE_RECORD_ID_NAME_HERE']) . "' "
."LIMIT 1");
if($inner === false)
{
echo 'Error adding you to the Inner List!';
echo mysql_error();
} else {
echo 'You have now been added to the Inner list!';
echo mysql_error();
}
break;
}
}
// query the db..
// mysql_query returns a resource ID
// the * should be replaced with only the fields you require,
// unless you either know what your doing or require virtually all of them
// generally queries without a where clause are a bad thing, but I'm
// assuming you want to pull all the records.
// field names should be encapsulated in backticks ie `
$rid = mysql_query("SELECT * FROM `training_sessions`");
// loop through all records..
while ($info = mysql_fetch_array($rid))
{
// indent 4 spaces for readability, use either print, or echo, but not both..
// I replaced all your print's with a table..
?>
<form name="FORM_<?php echo $info['UNIQUE_RECORD_ID_HERE']; ?>" action="" method="post">
<table cellspacing="0">
<tr>
<th colspan="2"><?php echo $info['user']; ?></th>
</tr>
<tr>
<td class="label">Date:</td>
<td><?php echo $info['sessiondate']; ?></td>
</tr>
<tr>
<td class="label">Time:</td>
<td><?php echo $info['sessiontime']; ?></td>
</tr>
<tr>
<td class="label">Location:</td>
<td><?php echo $info['location']; ?></td>
</tr>
<tr>
<td class="label">Meet Location:</td>
<td><?php echo $info['meetlocation']; ?></td>
</tr>
<tr>
<td class="label">Details:</td>
<td><?php echo $info['details']; ?></td>
</tr>
<tr>
<td class="label">Training Level:</td>
<td><?php echo $info['level']; ?></td>
</tr>
<tr>
<td class="label">Travel Details:</td>
<td><?php echo $info['traveldetails']; ?></td>
</tr>
<tr>
<td class="label">Users Attending:</td>
<td><?php echo $info['usersattending']; ?></td>
</tr>
<tr>
<td class="submit" colspan="2">
<input type="hidden" name="UNIQUE_RECORD_ID_NAME_HERE" value="<?php echo $info['UNIQUE_RECORD_ID_HERE']; ?>" />
<input type="hidden" name="process" value="new_inner" />
<input type="submit" value="Inner!" />
</td>
</tr>
</table>
</form>
<hr />
<?php
}
concerning the UPDATE sql command... i was not aware that it could not simply add another user to the list.. how can i make it so it adds another user to the list?