Hi All,
I have created the following php file (createproject.php) to insert data via a form into a database about a student project (data such as project ID, student ID, role of lecturer marking the project, project name and description). Once the form is filled out the user is taken to the studentqueue.php were there is atble of students that the lecturer is supervising.
Here is the file: createproject.php
<?php
include "conn.inc.php";
?>
<html>
<head>
<title>Create Project</title>
</head>
<body>
<?php
if (isset($_POST['submit']) && $_POST['submit'] == "Create Project") {
if ($_POST['lecturer_role'] != " " &&
$_POST['SK_ID'] != " " &&
$_POST['project_name'] != " " &&
$_POST['project_description'] != " ") {
$query = "SELECT lecturer_role, SK_ID, LK_ID FROM student_project
WHERE LK_ID = ' " . $_POST['username'] . " '
AND SK_ID = ' " . $_POST['SK_ID'] . " '
AND lecturer_role = ' " . $_POST['lecturer_role'] . " '";
$result = mysql_query($query)
or die(mysql_error());
if (mysql_num_rows($result) !=0) {
?>
<font color="#FF0000"><b>You have already created this project and you are a
<?php echo $_POST['lecturer_role']; ?> on it. Please create another project.</b></font>
<br>
<br>
<p>
<form action = "createproject.php" method="post">
<table border="0" cellpadding="15">
<tr>
<td>Select Lecturer Role:<br></td>
<td>
<input type="radio" name="role" value="Supervisor">Supervisor<br>
<input type="radio" name="role" value="Second Marker">Second Marker<br>
<input type="radio" name="role" value="Third Marker">Third Marker<br>
<input type="radio" name="role" value="Fourth Marker">Fourth Marker<br>
</td>
</tr>
<br>
<tr>
<td>Select Student:</td>
<td>
<?php
//Select student ID and name from dropdown menu
$sql = "SELECT SK_ID, name FROM student";
$result = mysql_query($sql, $conn) or die ($sql . '<br />' . mysql_error);
if (!$result) {
echo 'Query failed';
exit;
}
if (mysql_num_rows($result)>0) {
echo '<select name="students">';
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['SK_ID'] . '">' . $row['SK_ID'] . ', ' . $row['name'] . '</option>';
}
echo '</select>';
} else {
echo 'No students created yet';
}
?>
</tr>
<br>
<br>
<tr>
<td>Project Name:</td>
<td><input type="text" name="project_name" size="61"
value="<?php echo $_POST['project_name']; ?>">
</td>
</tr>
<br>
<br>
<tr>
<td>Project Description:</td>
<td><input type="text" name="project_desc" size="61"
value="<?php echo $_POST['project_desc']; ?>">
</td>
</tr>
<br>
<br>
</table>
<input type="submit" name="submit" value="Create Project">
<input type="reset" value="Clear">
</form>
</p>
<?php
} else {
$query = "INSERT INTO student_project (project_ID, lecturer_role, SK_ID, " .
"project_name, project_desc) " .
"VALUES (NULL, ' " . $_POST['lecturer_role'] . " ', " .
" ' " . $_POST['SK_ID'] . " ', ' " . $_POST['project_name'] . " ', " .
" ' " . $_POST['project_desc'] . " '); ";
$result = mysql_query($query)
or die(mysql_error());
?>
<p>
Thank you, project (<?php echo $_POST['project_name']; ?>) entered into database.<br>
<?php
header("Refresh: 5; URL=index.php");
echo "Returning to student queue ";
echo "(If your browser does not support this " .
"<a href=\"index.php\">Click here</a>)";
die();
}
} else {
?>
<font color="#FF0000"><b>The following information is required to create a project</b></font>
<br>
<br>
<p>
<form action = "createproject.php" method="post">
<table border="0" cellpadding="15">
<tr>
<td>Select Lecturer Role:<br></td>
<td>
<input type="radio" name="role" value="Supervisor">Supervisor<br>
<input type="radio" name="role" value="Second Marker">Second Marker<br>
<input type="radio" name="role" value="Third Marker">Third Marker<br>
<input type="radio" name="role" value="Fourth Marker">Fourth Marker<br>
</td>
</tr>
<br>
<tr>
<td>Select Student:</td>
<td>
<?php
//Select student ID and name from dropdown menu
$sql = "SELECT SK_ID, name FROM student";
$result = mysql_query($sql, $conn) or die ($sql . '<br />' . mysql_error);
if (!$result) {
echo 'Query failed';
exit;
}
if (mysql_num_rows($result)>0) {
echo '<select name="students">';
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['SK_ID'] . '">' . $row['SK_ID'] . ', ' . $row['name'] . '</option>';
}
echo '</select>';
} else {
echo 'No students created yet';
}
?>
</tr>
<br>
<br>
<tr>
<td>Project Name:</td>
<td><input type="text" name="project_name" size="61"
value="<?php echo $_POST['project_name']; ?>">
</td>
</tr>
<br>
<br>
<tr>
<td>Project Description:</td>
<td><input type="text" name="project_desc" size="61"
value="<?php echo $_POST['project_desc']; ?>">
</td>
</tr>
<br>
<br>
</table>
<input type="submit" name="submit" value="Create Project">
<input type="reset" value="Clear">
</form>
</p>
<?php
}
} else {
?>
<p>
Welcome to the 'Create Student Project' page<br>
The following information is required to create a project
<form action="createproject.php" method="post">
<table border="0" cellpadding="15">
<tr>
<td>Select Lecturer Role:<br></td>
<td>
<input type="radio" name="role" value="Supervisor">Supervisor<br>
<input type="radio" name="role" value="Second Marker">Second Marker<br>
<input type="radio" name="role" value="Third Marker">Third Marker<br>
<input type="radio" name="role" value="Fourth Marker">Fourth Marker<br>
</td>
</tr>
<br>
<tr>
<td>Select Student:</td>
<td>
<?php
//Select student ID and name from dropdown menu
$sql = "SELECT SK_ID, name FROM student";
$result = mysql_query($sql, $conn) or die ($sql . '<br />' . mysql_error);
if (!$result) {
echo 'Query failed';
exit;
}
if (mysql_num_rows($result)>0) {
echo '<select name="students">';
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['SK_ID'] . '">' . $row['SK_ID'] . ', ' . $row['name'] . '</option>';
}
echo '</select>';
} else {
echo 'No students created yet';
}
?>
</tr>
<br>
<br>
<tr>
<td>Project Name:</td>
<td><input type="text" name="project_name" size="61">
</td>
</tr>
<br>
<br>
<tr>
<td>Project Description:</td>
<td><input type="text" name="project_desc" size="61">
</td>
</tr>
<br>
<br>
</table>
<input type="submit" name="submit" value="Create Project">
<input type="reset" value="Clear">
</form>
</p>
<?php
}
?>
</body>
</html>
An overview of the studentproject database table is attached in jpeg format.
The questions I have regarding this code:
The lecturer role (added thru radio buttons) and the student ID (added via a drop down box) are not inserted into the database properly? How can I get these to insert properly?
I want to record the lecturer ID (I have already setup a session for login purposes) when they write in the project details. How can I do this as an input that doesn't show up in the form and record it in the database?
After running the file I got this error: Warning: Cannot modify header information - headers already sent by (output started at C:\WAMP5\www\Web_Dev\FYP2\createproject.php:9) in C:\WAMP5\www\Web_Dev\FYP2\createproject.php on line 100?
I am still a novice at PHP so any help appreciated.
Regards.