Page 1 of 1

changing mysql value from loop

Posted: Sat Apr 07, 2007 6:50 pm
by thunderbox
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.

Here is my code:

Code: Select all

$data = mysql_query("SELECT * FROM training_sessions");

while($info = mysql_fetch_array( $data ))
{
Print $info['user'];
echo " Set Up This Session! <br><br>";
Print "<b>Date:</b> ".$info['sessiondate'] . " <br>";
Print "<b>Time:</b> ".$info['sessiontime'] . "<br>";
Print "<b>Location:</b> ".$info['location'] . " <br>";
Print "<b>Meet Location:</b> ".$info['meetlocation'] . "<br>";
Print "<b>Details:</b> ".$info['details'] . " <br>";
Print "<b>Training Level:</b> ".$info['level'] . "<br>";
Print "<b>Travel Details:</b> ".$info['traveldetails'] . " <br>";
Print "<b>Users Attending:</b> ".$info['usersattending'] . " <br>";
print "<form method='post' action='$server'><input type='submit' id='inner' value='Inner!'></form>";
Print "<hr>";
if(isset($_POST['inner'])) {
	$username = $_SESSION['username'];
	$sessionid = $info['sessionID'];
	$inner = mysql_query("UPDATE `training_sessions` SET `usersattending` = $username', ' WHERE `training_sessions`.`sessionID` = $sessionid");
	if(!$inner) {
		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();
	}
	
}
}
?>
the script i wrote there at the end of the loops does nothing, it dosnt give me any confirmation or any error messages, it just does nothing

note the variable $server = $_SERVER['PHP_SELF']

Posted: Sun Apr 08, 2007 1:20 am
by Benjamin
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.

Code: Select all

<?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
}

Posted: Sun Apr 08, 2007 1:37 am
by thunderbox
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?

Posted: Sun Apr 08, 2007 1:42 am
by Benjamin
One solution would be to have a table with fields identifying the event, and the user_id of an attendee who will be present.

Then you can INSERT the user_id for BOB, along with the event_id for Bobs Wedding into that table.

To list the attendees, you would then select user_id from event_attendees where event_id = x;

Posted: Sun Apr 08, 2007 7:14 pm
by thunderbox

Code: Select all

$query1 = mysql_query("SELECT * FROM inner");
            
            $sessionid = $info['sessionID'];
            $userid = $_SESSION['username'];
            
            $inner = mysql_query("INSERT INTO inner (sessionID, userID) VALUES ('$sessionid','$userid')");
im not sure if that would work... is there a better way to do it, i don't think getting the variable $sessionid like that would work