Jump to content

This problem has been bothering me for days...


karatekid36

Recommended Posts

So I have recentley gotten into PHP and I am now starting to write my own code.  PHP so far has been a life saver and I am a huge fan of it.  My current problem is that I have one php page that is pulling the first and last name from a users table I have in a database and posting it on this page.  This page is an attendance roster for weekly meetings that we have.  Next to each name that is being posted are foru radio buttons; Present, Absent, Excused.  Getting all of the names to post and have these radio buttons next to the name is quite easy, but I want all of these radio buttons submitted into another table I have in the same database.  I can not get the submit button to send any data to this new table.  I am quite lost and would really like some help so I could get this to work.  I can def send out some code and a longer description of my issue upon request.  I just want to keep a record of who is attending meetings and I can not figure this out. 

 

<?php

 

$page_title = 'Meeting Role';

include ('includes/header.html');

 

// Page header.

echo '<h1 id="mainhead">Active Brothers</h1>';

 

 

 

require_once ('mysql_connect.php'); // Connect to the db.

 

$errors = array(); // Initialize error array.

 

    // Check for a first name.

if (empty($_POST['first_name'])) {

$errors[] = 'You forgot to enter your first name.';

} else {

$fn = escape_data($_POST['first_name']);

}

 

// Check for a last name.

if (empty($_POST['last_name'])) {

$errors[] = 'You forgot to enter your last name.';

} else {

$ln = escape_data($_POST['last_name']);

}

 

// Check for a attendance.

if (empty($_POST['attend'])) {

$errors[] = 'You forgot to enter the attendance for someone.';

} else {

$at = escape_data($_POST['attend']);

}

 

 

 

// Number of records to show per page:

$display = 100;

 

// Determine how many pages there are.

if (isset($_GET['np'])) { // Already been determined.

 

$num_pages = $_GET['np'];

 

} else { // Need to determine.

 

// Count the number of records

$query = "SELECT COUNT(*) FROM users ORDER BY last_name ASC";

$result = mysql_query ($query);

$row = mysql_fetch_array ($result, MYSQL_NUM);

$num_records = $row[0];

 

// Calculate the number of pages.

if ($num_records > $display) { // More than 1 page.

$num_pages = ceil ($num_records/$display);

} else {

$num_pages = 1;

}

 

} // End of np IF.

 

// Determine where in the database to start returning results.

if (isset($_GET['s'])) {

$start = $_GET['s'];

} else {

$start = 0;

}

 

// Make the query.

$query = "SELECT last_name, first_name FROM users ORDER BY last_name ASC LIMIT $start, $display" ;

$querytwo = "INSERT INTO attend  (last_name, first_name, attend) VALUES ($ln, $fn, $at)";

 

$result = mysql_query ($query); // Run the query.

 

 

// Table header.

echo '<table align="center" cellspacing="0" cellpadding="5">

<tr>

<td align="left"><b>Last Name</b></td>

<td align="left"><b>First Name</b></td>

<td align="left"><b>Attendance</b></td>

</tr>

';

 

// Fetch and print all the records.

$bg = '#eeeeee'; // Set the background color.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.

echo '<tr bgcolor="' . $bg . '">

 

<td align="left">' . $row['last_name'] . '</td>

<td align="left">' . $row['first_name'] . '</td>

<td align="left">

<form>

  <p>

    <label>

      <input type="radio" name="attend" id="attend" value="Present" />

      Present</label>

 

    <label>

      <input type="radio" name="attend" id="attend" value="Late" />

      Late</label>

 

    <label>

      <input type="radio" name="attend" id="attend" value="Excused" />

      Excused</label>

 

    <label>

      <input type="radio" name="attend" id="attend" value="Unexcused" />

      Unexcused</label>

 

  </p>

</form>

 

</td>

 

   

</tr>

';

}

 

echo '</table>';

 

mysql_free_result ($result); // Free up the resources.

 

mysql_close(); // Close the database connection.

 

// Make the links to other pages, if necessary.

if ($num_pages > 1) {

 

echo '<br /><p>';

// Determine what page the script is on.

$current_page = ($start/$display) + 1;

 

// If it's not the first page, make a Previous button.

if ($current_page != 1) {

echo '<a href="view_users.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';

}

 

// Make all the numbered pages.

for ($i = 1; $i <= $num_pages; $i++) {

if ($i != $current_page) {

echo '<a href="meeting_role.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';

} else {

echo $i . ' ';

}

}

 

// If it's not the last page, make a Next button.

if ($current_page != $num_pages) {

echo '<a href="meeting_role.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';

}

 

echo '</p>';

 

} // End of links section.

 

echo '<br /><p><input type="submit" name="submit" value="Submit" /></p>';

echo '<input type="hidden" name="submitted" value="TRUE" />';

 

include ('includes/footer.html'); // Include the HTML footer.

?>

<?php

$page_title = 'Meeting Role';
include ('includes/header.html');

// Page header.
echo '<h1 id="mainhead">Active Brothers</h1>';



   require_once ('mysql_connect.php'); // Connect to the db.
   
$errors = array(); // Initialize error array.

    // Check for a first name.
   if (empty($_POST['first_name'])) {
      $errors[] = 'You forgot to enter your first name.';
   } else {
      $fn = escape_data($_POST['first_name']);
   }
   
   // Check for a last name.
   if (empty($_POST['last_name'])) {
      $errors[] = 'You forgot to enter your last name.';
   } else {
      $ln = escape_data($_POST['last_name']);
   }
   
   // Check for a attendance.
   if (empty($_POST['attend'])) {
      $errors[] = 'You forgot to enter the attendance for someone.';
   } else {
      $at = escape_data($_POST['attend']);
   }



// Number of records to show per page:
$display = 100;

// Determine how many pages there are.
if (isset($_GET['no problem'])) { // Already been determined.

   $num_pages = $_GET['no problem'];

} else { // Need to determine.

    // Count the number of records
   $query = "SELECT COUNT(*) FROM users ORDER BY last_name ASC";
   $result = mysql_query ($query);
   $row = mysql_fetch_array ($result, MYSQL_NUM);
   $num_records = $row[0];

   // Calculate the number of pages.
   if ($num_records > $display) { // More than 1 page.
      $num_pages = ceil ($num_records/$display);
   } else {
      $num_pages = 1;
   }
   
} // End of no problem IF.

// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
   $start = $_GET['s'];
} else {
   $start = 0;
}
      
// Make the query.
$query = "SELECT last_name, first_name FROM users ORDER BY last_name ASC LIMIT $start, $display" ;
$querytwo = "INSERT INTO attend  (last_name, first_name, attend) VALUES ($ln, $fn, $at)";
                  
$result = mysql_query ($query); // Run the query.


// Table header.
echo '<table align="center" cellspacing="0" cellpadding="5">
<tr>
   <td align="left">Last Name</td>
   <td align="left">First Name</td>
   <td align="left">Attendance</td>
</tr>
';

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
   echo '<tr bgcolor="' . $bg . '">
   
      <td align="left">' . $row['last_name'] . '</td>
      <td align="left">' . $row['first_name'] . '</td>
      <td align="left">
   <form>
  <p>
    <label>
      <input type="radio" name="attend" id="attend" value="Present" />
      Present</label>
   
    <label>
      <input type="radio" name="attend" id="attend" value="Late" />
      Late</label>
   
    <label>
      <input type="radio" name="attend" id="attend" value="Excused" />
      Excused</label>
   
    <label>
      <input type="radio" name="attend" id="attend" value="Unexcused" />
      Unexcused</label>
   
  </p>
      </form>
      
      </td>
   
     
   </tr>
   ';
}

echo '</table>';

mysql_free_result ($result); // Free up the resources.   

mysql_close(); // Close the database connection.

// Make the links to other pages, if necessary.
if ($num_pages > 1) {
   
   echo '
<p>';
   // Determine what page the script is on.   
   $current_page = ($start/$display) + 1;
   
   // If it's not the first page, make a Previous button.
   if ($current_page != 1) {
      echo '<a href="view_users.php?s=' . ($start - $display) . '&no problem=' . $num_pages . '">Previous[/url] ';
   }
   
   // Make all the numbered pages.
   for ($i = 1; $i <= $num_pages; $i++) {
      if ($i != $current_page) {
         echo '<a href="meeting_role.php?s=' . (($display * ($i - 1))) . '&no problem=' . $num_pages . '">' . $i . '[/url] ';
      } else {
         echo $i . ' ';
      }
   }
   
   // If it's not the last page, make a Next button.
   if ($current_page != $num_pages) {
      echo '<a href="meeting_role.php?s=' . ($start + $display) . '&no problem=' . $num_pages . '">Next[/url]';
   }
   
   echo '</p>';
   
} // End of links section.

   echo '
<p><input type="submit" name="submit" value="Submit" /></p>';
   echo '<input type="hidden" name="submitted" value="TRUE" />';
   
include ('includes/footer.html'); // Include the HTML footer.
?>

 

Had to post your code like that so I could see it better before I looked at it =)

Yesideez - yeah, I was posting that as you said something :) Plus I needed to look at it, hah.

 

karatekid36 - Do you have a unique ID said for each person? It would be nice for them to each have their own number by adding an auto-increment field in the database as the primary key. Then  the checkboxes could hold the values of their unique numbers so we know how to direct the query. I guess it would work with their last name, but it would be much easier if they had their own numbers.

Great, let me look at your code again and see if I can fix it for you. Using a Unique ID just gives each person in the database their own identity where it is easy to make a query to the database to update a specific field.

 

For the code I am posting, I am going to pretend your unique ID for each user is named simply "id".

 

Okay, there is no guarantee that this will work...but I gave it my best shot ^^

 

<?php

$page_title = 'Meeting Role';
include ('includes/header.html');

// Page header.
echo '<h1 id="mainhead">Active Brothers</h1>';



   require_once ('mysql_connect.php'); // Connect to the db.
   
   
if ($_POST['take_roll']){

   if ($_POST['attend']){

   $id = $_POST['attend'];	
$num = count($id);
  for($i = 0; $i < $num; $i++){

	$userID = $attend[$i];

mysql_query("UPDATE users SET attend = 'yes' WHERE id = '$userID'");
  }
}

   if ($_POST['late']){

   $id = $_POST['late'];	
$num = count($id);
  for($i = 0; $i < $num; $i++){

	$userID = $late[$i];

mysql_query("UPDATE users SET attend = 'late' WHERE id = '$userID'");
  }
}

   if ($_POST['excused']){

   $id = $_POST['excused'];	
$num = count($id);
  for($i = 0; $i < $num; $i++){

	$userID = $excused[$i];

mysql_query("UPDATE users SET attend = 'excused' WHERE id = '$userID'");
  }
}

if ($_POST['unexcused']){

   $id = $_POST['unexcused'];	
$num = count($id);
  for($i = 0; $i < $num; $i++){

	$userID = $unexcused[$i];

mysql_query("UPDATE users SET attend = 'unexcused' WHERE id = '$userID'");
  }
}



echo "Successfully took role";
exit;
}
   
$errors = array(); // Initialize error array.

    // Check for a first name.
   if (empty($_POST['first_name'])) {
      $errors[] = 'You forgot to enter your first name.';
   } else {
      $fn = escape_data($_POST['first_name']);
   }
   
   // Check for a last name.
   if (empty($_POST['last_name'])) {
      $errors[] = 'You forgot to enter your last name.';
   } else {
      $ln = escape_data($_POST['last_name']);
   }
   
   // Check for a attendance.
   if (empty($_POST['attend'])) {
      $errors[] = 'You forgot to enter the attendance for someone.';
   } else {
      $at = escape_data($_POST['attend']);
   }



// Number of records to show per page:
$display = 100;

// Determine how many pages there are.
if (isset($_GET['no problem'])) { // Already been determined.

   $num_pages = $_GET['no problem'];

} else { // Need to determine.

    // Count the number of records
   $query = "SELECT COUNT(*) FROM users ORDER BY last_name ASC";
   $result = mysql_query ($query);
   $row = mysql_fetch_array ($result, MYSQL_NUM);
   $num_records = $row[0];

   // Calculate the number of pages.
   if ($num_records > $display) { // More than 1 page.
      $num_pages = ceil ($num_records/$display);
   } else {
      $num_pages = 1;
   }
   
} // End of no problem IF.

// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
   $start = $_GET['s'];
} else {
   $start = 0;
}
      
// Make the query.
$query = "SELECT id, last_name, first_name FROM users ORDER BY last_name ASC LIMIT $start, $display" ;
$querytwo = "INSERT INTO attend  (last_name, first_name, attend) VALUES ($ln, $fn, $at)";
                  
$result = mysql_query ($query); // Run the query.


// Table header.
echo '<table align="center" cellspacing="0" cellpadding="5">
<tr>
   <td align="left">Last Name</td>
   <td align="left">First Name</td>
   <td align="left">Attendance</td>
</tr>
';

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
   echo '<tr bgcolor="' . $bg . '">
   
      <td align="left">' . $row['last_name'] . '</td>
      <td align="left">' . $row['first_name'] . '</td>
      <td align="left">
   <form>
  <p>
    <label>
      <input type="radio" name="attend[]" id="attend" value="'.{$row['id']}.'" />
      Present</label>
   
    <label>
      <input type="radio" name="late[]" id="attend" value="'.{$row['id']}.'" />
      Late</label>
   
    <label>
      <input type="radio" name="excused[]" id="attend" value="'.{$row['id']}.'" />
      Excused</label>
   
    <label>
      <input type="radio" name="unexcused[]" id="attend" value="'.{$row['id']}.'" />
      Unexcused</label>
   
  </p>
      
      </td>
   
     
   </tr>
   ';
}

echo '<input type = "submit" name = "take_roll" value = "Take Roll">
      </form>';

echo '</table>';

mysql_free_result ($result); // Free up the resources.   

mysql_close(); // Close the database connection.

// Make the links to other pages, if necessary.
if ($num_pages > 1) {
   
   echo '
<p>';
   // Determine what page the script is on.   
   $current_page = ($start/$display) + 1;
   
   // If it's not the first page, make a Previous button.
   if ($current_page != 1) {
      echo '<a href="view_users.php?s=' . ($start - $display) . '&no problem=' . $num_pages . '">Previous[/url] ';
   }
   
   // Make all the numbered pages.
   for ($i = 1; $i <= $num_pages; $i++) {
      if ($i != $current_page) {
         echo '<a href="meeting_role.php?s=' . (($display * ($i - 1))) . '&no problem=' . $num_pages . '">' . $i . '[/url] ';
      } else {
         echo $i . ' ';
      }
   }
   
   // If it's not the last page, make a Next button.
   if ($current_page != $num_pages) {
      echo '<a href="meeting_role.php?s=' . ($start + $display) . '&no problem=' . $num_pages . '">Next[/url]';
   }
   
   echo '</p>';
   
} // End of links section.

   echo '
<p><input type="submit" name="submit" value="Submit" /></p>';
   echo '<input type="hidden" name="submitted" value="TRUE" />';
   
include ('includes/footer.html'); // Include the HTML footer.
?>

 

 

so I looked over the code today and I am not sure that the solution provided is exactly what I had in mind, but also my inexperience may be misleading me. I have a users table with a lot of information about each user, which in this case is fraternity brothers.  I will be adding the info in the beginning, but later as the brotherhood grows they will have to add the info themselves and there will be a page where they can update the info.  The goal of this project is to have no myphpadmin / mysql interaction for the basic users.  I want to set up the users table and then have a separate attend table where the user_id, last_name, first_name, and then finally attendance is added.  Each week we have two meetings where everyone's attendance is needed.  I want to add the meeting date to the attend table each time we have a meeting and then check off each person's attendance record for the newly added date.  I know that I can alter a table once it is made, but I do not want to continually have to go into myphpadmin and add the date of the meeting to the table.  I would like to have a seperate php page where I can add meeting dates to the attend table.  I have a background in Access, but I recently switched to mysql and php.  I do not how to link the two tables in mysql so that all of the user info that I need in the users table goes to the attend table.  I hope this makes my situation a little clearer, but if not please let me know.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.