Jump to content

Recommended Posts

Right now these are my tables..

 

h3cwHlH.png

 

i have create a page that list all the users and there is an edit button..

My question is when i click the edit button, how to view the name of the user and a select box option where the options are my course and subject that been stored?

Then, when i fill the course and subject from the select box, it will stored to user_subject and user_course table..

<?php
session_start();
require_once('function.php'); //Set this to what ever page you include that holds all your functions so that we can use the checkUserStatus()
checkUserStatus('admin');
?>
<html>
	<head>
		<title> View Users </title>
	</head>

<body>
<h2 align='right'><a href='admin.php'>Home</a></h2>
<center><h2>Users Information</h2><center>
<table width='800' align='center' border='5'>
	<tr bgcolor='yellow'>
		<th>No.</th>
		<th>Full Name</th>
		<th>Matrix No.</th>
		<th>Username</th>
		<th>Password</th>
		<th>User Type</th>
		<th>Edit User</th>
		<th>Delete User</th>
	</tr>
	
<?php
mysql_connect("localhost","root","");
mysql_select_db("class_attendance");

	$query = "select * from user";
	
	$run = mysql_query($query);

while ($row=mysql_fetch_array($run)){

	$id = $row[0];
	$full_name = $row[1];
	$matrix_no = $row[2];
	$username = $row[3];
	$pass = $row[4];
	$type = $row[5];

?>
	<tr align='center'>
	<td><?php echo $id; ?></td>
	<td><?php echo $full_name; ?></td>
	<td><?php echo $matrix_no; ?></td>
	<td><?php echo $username; ?></td>
	<td><?php echo $pass; ?></td>
	<td><?php echo $type; ?></td>
	<td><a href='edit.php?del=<?php echo $id;?>'>Edit</a></td>
	<td><a href='delete.php?del=<?php echo $id;?>'>Delete</a></td>
	</tr>
<?php } ?>
	
</table>
</body>
</html>

Ok i tried to sketch the coding a little bit.. and this is what i got so far..

<?php
session_start();
require_once('function.php'); //Set this to what ever page you include that holds all your functions so that we can use the checkUserStatus()
checkUserStatus('admin');
?>
<html>
	<head>
		<title> Student/Lecturer Course Form </title>
	</head>
	
<body>
<h2 align='right'><a href='admin.php'>Home</a></h2>
<form method='post' action='edit.php'>
	<table width='400' border='5' align='center'>
	<tr>
		<td align='center' colspan='5'><h1>Student/Lecturer Course Form</h1></td>
	</tr>
	
	<tr>
	<td align='center'>
	<select name="course_id" id="course_id">
	<option value="0 selected="selected">Select course</option>
	<option value=""></option>
	<option value=""></option>
	</select></td>
	</tr>
	
	<tr>
		<td colspan='5' align='center'><input type='submit' name='submit' value='Submit' /></td>
	</tr>
	
	</table>
</form>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("class_attendance"); 

	if(isset($_POST['submit'])){
	
	$course_id= $_POST['course_id'];
	
	if($course_id==''){
	echo "<script>alert('The Course Name is empty!')</script>";
	exit();
	}
	
	$check_name= "select * from user_course where course_id='$course_id'";
	
	$run = mysql_query($check_name);
	
	if(mysql_num_rows($run)>0){
	
	echo "<script>alert('The Course Name $course_id already been filled! Please fill another one.')</script>";
	exit();
	}
	
	$query = "insert into user_course (course_id) values ('$course_id')";
	if(mysql_query($query)){
	
	echo"<script>alert('Registration successful!')</script>";
	
	}
	
}

?>

As you can see, i dont know how to view the clicked user_id to the edit.php and fill the dropdown options with courses that have been added in the course table..

ok i updated my view_users.php and my edit.php..

 

view_users.php

<?php
session_start();
require_once('function.php'); //Set this to what ever page you include that holds all your functions so that we can use the checkUserStatus()
checkUserStatus('admin');
?>
<html>
	<head>
		<title> View Users </title>
	</head>

<body>
<h2 align='right'><a href='admin.php'>Home</a></h2>
<center><h2>Users Information</h2><center>
<table width='800' align='center' border='5'>
	<tr bgcolor='yellow'>
		<th>No.</th>
		<th>Full Name</th>
		<th>Matrix No.</th>
		<th>Username</th>
		<th>Password</th>
		<th>User Type</th>
		<th>Edit User</th>
		<th>Delete User</th>
	</tr>
	
<?php
mysql_connect("localhost","root","");
mysql_select_db("class_attendance");

	$query = "select * from user";
	
	$run = mysql_query($query);

while ($row=mysql_fetch_array($run)){

	$id = $row[0];
	$full_name = $row[1];
	$matrix_no = $row[2];
	$username = $row[3];
	$pass = $row[4];
	$type = $row[5];

?>
	<tr align='center'>
	<td><?php echo $id; ?></td>
	<td><?php echo $full_name; ?></td>
	<td><?php echo $matrix_no; ?></td>
	<td><?php echo $username; ?></td>
	<td><?php echo $pass; ?></td>
	<td><?php echo $type; ?></td>
	<a href="edit.php?user_id=<?= $id ?>">Edit</a>
	<td><a href='delete.php?del=<?php echo $id;?>'>Delete</a></td>
	</tr>
<?php } ?>
	
</table>
</body>
</html>

edit.php

<?php
session_start();
require_once('function.php'); //Set this to what ever page you include that holds all your functions so that we can use the checkUserStatus()
checkUserStatus('admin');
?>
<html>
	<head>
		<title> Student/Lecturer Course Form </title>
	</head>
	
<body>
<h2 align='right'><a href='admin.php'>Home</a></h2>
<?php
mysql_connect("localhost","root","");
mysql_select_db("class_attendance"); 

$courses = $subjects = array();

$sql = "SELECT * FROM course";
$res = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($res))
{
   $courses[] = $row;
}

$sql = "SELECT * FROM subject";
$res = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($res))
{
   $subjects[] = $row;
}

?>

<form method='post' action='edit.php'>
	<table width='400' border='5' align='center'>
	<tr>
		<td align='center' colspan='5'><h1>Student/Lecturer Course Form</h1></td>
	</tr>
	
	<tr>
   <td>
       <select name="course">
           <?php foreach($courses as $course) : ?>
               <option value="<?= $course['course_id'] ?>"> <?= $course['name'] ?> </option>
           <?php endforeach ?>
       </select>
   </td>
</tr>

<tr>
   <td>
       <select name="subject">
           <?php foreach($subjects as $subject) : ?>
               <option value="<?= $subject['subject_id'] ?>"> <?= $subject['name'] ?> </option>
           <?php endforeach ?>
       </select>
   </td>
</tr>

<input type="hidden" name="user_id" value="<?= $_GET['user_id'] ?>">

	
	<tr>
		<td colspan='5' align='center'><input type='submit' name='submit' value='Submit' /></td>
	</tr>
	
	</table>
</form>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("class_attendance"); 

if (isset($_POST['submit']))
{
   print_r( $_POST ); // check all submitted data & make sure we get course_id, subject_id and user_id
}

?>

and this is my error..

 

latest_zpsbe99c27f.png

here's a list of things about your last posted code (and the image of your tables) -

 

1) you should store the full name as 2 or 3 separate parts, first name, last name, middle name/initial so that you can for example display/select students by the first letter of their last name.

 

2) there's no good reason to retrieve and display the password field from the user table. the password field is only used when logging in or if you have a change password feature.

 

3) the image of your user table doesn't match the columns you are referencing (yes it matters if what you post is actual information.)

 

4) you need to switch to using the associative index/column names in your code. if your table should get reorganized, your current code will break. using the associative index/column names will eliminate this possibility. also, more people will be likely to help you with your code because your code will be somewhat 'self documenting'.

 

5) if the purpose of this code is to edit (pick/modify) which course(s) a student is in, shouldn't you just retrieve the user's who are students?

 

6) students take courses. courses belong to a subject type. when you add a user_id/course_id to your user_course table, that's the only place you need to store that information.

 

7) the only good reason i can see for the user_subject table is if you want to store which teachers (their user_id) are available to teach the subjects (via a subject_id.) you would use this information when picking/assigning teacher(s) to the course(s) they will teach.

 

8) your course table needs more information for each course (class.) it needs a course_id, subject_id (i don't see any particular need for your course_subject table), room_no, period_no, school_term_id. the school_term_id would relate the course to it's school year and start/end date to handle things like full year courses, semester long courses and which semester in the school year they are...

 

9) as to your question, you know which courses are under which subject from the relationship in your tables, they are not two separate selections. you would list the available courses, sorted by subject, then perhaps by the period number so that you could assign/edit the courses any one student is taking.

Edited by mac_gyver

EDIT:

 

about no 3, the user table i show you is the old one.. i do updated the table to match my coding in the view_users.php

about no 5 and no 7, yes, i would like the teacher to be assigned with his/her own courses and subjects so that when he/she want to take attendance, he/she need to click the course and subject 1st..

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.