Reputation: 85
i know this type of question is too easy for people here but im a beginner in PHP/SQL and so for personal learning reasons i am making a system of override papers for classes that are full in terms of capacity. It works by the student filling the override form, its submitted into an override database with the professor name. When the professor logs in, he sees the override forms requested into his class. After the professor accepts the form, it goes to the head of department for him to accept also. Now, the question is here: How do i make it when the professor clicks "Accept", it would write the same data with one extra row (called comments for comments that the professor has for the head of department) into the heads database and remove it from the current override database so that it isnt there anymore since the professor has accepted the override.
i tried doing it as a form and it works, it submits to the Head's database but whenever i try to add the comments part or any new column into the code, it stops writing to the database. Also, if there are like multiple overrides in the database showing up for the professor, and one is accepted/denied, it would write all of them overrides into the database instead of the one accepted/denied. It would be great to guide me where i did my mistake in the code. Please ignore the style of my code since i am a php beginner and its messy for now.
Here is what i was able to make, this is the whole class code:
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$user = $_SESSION['username'];
?>
<!DOCTYPE html>
<html>
<?php
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$usertype = $mysqli->query("SELECT usertype FROM student WHERE username = '$user'")->fetch_object()->usertype;
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$name = $mysqli->query("SELECT name FROM student WHERE username = '$user'")->fetch_object()->name;
?>
<?php if (login_check($mysqli) == true) : ?>
<p>Welcome <?php echo htmlentities($name); ?>!</p>
<?php if ($usertype == 0) { ?>
<form method="post" action="dboverride.php" name="OverrideForm" id="OverrideForm" autocomplete="off">
<fieldset>
<legend><b>Personal Details</b></legend>
<div>
<label for="name" accesskey="N">First Name</label>
<?php
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$name = $mysqli->query("SELECT name FROM student WHERE username = '$user'")->fetch_object()->name;
print_r($name);
?>
</div>
<br>
<div>
<label for="mname" accesskey="M">Mid. Name</label>
<?php
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$mname = $mysqli->query("SELECT mname FROM student WHERE username = '$user'")->fetch_object()->mname;
print_r($mname);
?>
</div>
<br>
<div>
<label for="fname" accesskey="F">Last Name</label>
<?php
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$fname = $mysqli->query("SELECT fname FROM student WHERE username = '$user'")->fetch_object()->fname;
print_r($fname);
?>
</div>
<br>
<div>
<label for="sid" accesskey="i">Student ID</label>
<?php
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$sid = $mysqli->query("SELECT sid FROM student WHERE username = '$user'")->fetch_object()->sid;
print_r($sid);
?>
</div>
<br>
<div>
<label for="email" accesskey="E">Email</label>
<?php
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$email = $mysqli->query("SELECT email FROM student WHERE username = '$user'")->fetch_object()->email;
print_r($email);
?>
</div>
<br>
<div>
<label for="phone" accesskey="p">Phone No.</label>
<?php
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$phone = $mysqli->query("SELECT phone FROM student WHERE username = '$user'")->fetch_object()->phone;
print_r($phone);
?>
</div>
<br>
<div>
<label for="sc" accesskey="s">Scolarship</label>
<?php
$mysqli = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$Scolarship = $mysqli->query("SELECT Scolarship FROM student WHERE username = '$user'")->fetch_object()->Scolarship;
print_r($Scolarship);
?>
</div>
</fieldset>
<br >
<fieldset>
<legend><b>Subject Details</b></legend>
<div>
<label for="class" accesskey="c">Subject</label>
<input name="class" type="text" id="class" size="50" required />
</div>
<br>
<div>
<label for="section" accesskey="o">Section</label>
<input name="section" type="number" id="section" min="1" max="9" required />
</div>
<br>
<div>
<label for="semester" accesskey="S">Semester</label>
<select name="semester" id="semester" required="required">
<option value="F15">Fall 2015</option>
<option value="S15">Summer 2015</option>
<option value="SP16">Spring 2016</option>
</select>
</div>
</fieldset>
<br >
<fieldset>
<legend><b>Agreement</b></legend>
<form action="#" onSubmit="if(document.getElementById('agree').checked) { return true; } else { alert('Please indicate that you have agreed on the terms'); return false; }">
<input type="checkbox" name="checkbox" value="check" id="agree" required /> By checking this box, i am fully responsible for the data entered above. I am also providing my official online signature to be liable in however the site may use it for.
<br> <i><font size="2"> * Date and time are recorded for various reasons.</font></i>
<br><br>
<label for="date" accesskey="c">   Current Date & Time : </label>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<script type="text/javascript">
document.write ('<span id="date-time">', new Date().toLocaleString(), '<\/span>')
if (document.getElementById) onload = function () {
setInterval ("document.getElementById ('date-time').firstChild.data = new Date().toLocaleString()", 50)
}
</script>
</fieldset>
<br>
<input type="submit" class="submit" id="submit" value="Submit" /> <input type="reset" value="Reset"><br>
</form>
</form>
<?php
} else if ($usertype == 1) {
$server = "localhost";
$user = "aukwizcq_user";
$pass = "r=Sc!~ZcFoBK";
$db = "aukwizcq_overrides";
$db2 = "aukwizcq_headforms";
$db3 = "aukwizcq_deanforms";
$user1 = $_SESSION['username'];
$mysqli = new Mysqli($server, $user, $pass, $db) or mysqli_error($mysqli);
$QUERY = new Mysqli($server, $user, $pass, $db2) or mysqli_error($mysqli);
$QUERY2 = new Mysqli($server, $user, $pass, $db3) or mysqli_error($mysqli);
$overrides = $mysqli->query("SELECT * FROM Overrides WHERE professor = '$user1'");
$num_rows = mysqli_num_rows($overrides);
?>
<?php
echo " Overrides today: " . $num_rows;
?>
<?php
while($row = mysqli_fetch_array($overrides)) { ?>
<fieldset> <?php
echo "First Name: " . $row['name'] . "<br />";
echo "<br />Mid. Name: " . $row['mname'] . "<br />";
echo "<br />Fam. Name: " . $row['fname'] . "<br />";
echo "<br />Student ID: " . $row['sid'] . "<br />";
echo "<br />Scolarship: " . $row['sc'] . "<br />";
echo "<br />Phone No: " . $row['phone'] . "<br />";
echo "<br />Email: " . $row['email'] . "<br />";
echo "<br />Class: " . $row['class'] . "<br />";
echo "<br />Section: " . $row['section'] . "<br />";
echo "<br />Semester: " . $row['semester'] . "<br />";
$name = $row['name'];
$mname = $row['mname'];
$fname = $row['fname'];
$sid = $row['sid'];
$sc = $row['sc'];
$phone = $row['phone'];
$email = $row['email'];
$class = $row['class'];
$section = $row['section'];
$semester = $row['semester'];
?>
<br />
<div>
<form method="post" action="dbheads.php" name="HeadWritingForm" id="HeadWritingForm" autocomplete="off">
<label for="comments" accesskey="c">Notes & Comments:</label><br />
<input type="textarea" name="comments" id="comments" cols="35" rows="10">
</textarea><br>
</div>
<br>
<input type="submit" class="submit" id="submit" value="Accept" /><br><br>
<?php if(Accept) { $prepare = $QUERY->prepare("INSERT INTO `headforms`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`class`,`section`,`semester`,`comments`) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
$prepare->bind_param("sssssssssss", $name, $mname, $fname, $sid, $email, $phone, $sc, $class, $section, $semester, $_POST['comments']); $prepare->execute(); } ?>
</form>
<form method="post" action="dbheads2.php" name="deny" id="deny" autocomplete="off">
<input type="submit" class="submit" id="submit" value="Deny" /><br><br>
<?php if(Deny) { $prepare = $QUERY2->prepare("INSERT INTO `deanforms`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`class`,`section`,`semester`) VALUES (?,?,?,?,?,?,?,?,?,?)");
$prepare->bind_param("ssssssssss", $name, $mname, $fname, $sid, $email, $phone, $sc, $class, $section, $semester); $prepare->execute(); } ?>
</form>
</fieldset>
<br>
<?php }
?>
<br />
<?php
} else if ($usertype == 2) {
$server1 = "localhost";
$user1 = "aukwizcq_user";
$pass1 = "r=Sc!~ZcFoBK";
$db1 = "aukwizcq_headforms";
$user2 = $_SESSION['username'];
$mysqli = new Mysqli($server1, $user1, $pass1, $db1) or mysqli_error($mysqli);
$headforms = $mysqli->query("SELECT * FROM headforms WHERE professor = '$user2'");
$num_rows2 = mysqli_num_rows($headforms);
echo "Overrides today: " . $num_rows2;
while($row2 = mysqli_fetch_array($headforms)) {
echo "<br /><br />First Name: " . $row2['name'] . "<br />";
echo "<br />Middle Name: " . $row2['mname'] . "<br />";
echo "<br />Family Name: " . $row2['fname'] . "<br />";
echo "<br />Student ID: " . $row2['sid'] . "<br />";
echo "<br />Scolarship: " . $row2['sc'] . "<br />";
echo "<br />Phone No: " . $row2['phone'] . "<br />";
echo "<br />Email: " . $row2['email'] . "<br />";
echo "<br />Class: " . $row2['class'] . "<br />";
echo "<br />Section: " . $row2['section'] . "<br />";
echo "<br />Semester: " . $row2['semester'] . "<br />";
}
?>
<?php
} else if ($usertype == 3) {
$server2 = "localhost";
$user2 = "aukwizcq_user";
$pass2 = "r=Sc!~ZcFoBK";
$db2 = "aukwizcq_registrarforms";
$user3 = $_SESSION['username'];
$mysqli = new Mysqli($server2, $user2, $pass2, $db2) or mysqli_error($mysqli);
$registrarforms = $mysqli->query("SELECT * FROM registrarforms WHERE professor = '$user3'");
$num_rows3 = mysqli_num_rows($registrarforms);
echo "Overrides today: " . $num_rows3;
while($row3 = mysqli_fetch_array($registrarforms)) {
echo "<br /><br />First Name: " . $row3['name'] . "<br />";
echo "<br />Middle Name: " . $row3['mname'] . "<br />";
echo "<br />Family Name: " . $row3['fname'] . "<br />";
echo "<br />Student ID: " . $row3['sid'] . "<br />";
echo "<br />Scolarship: " . $row3['sc'] . "<br />";
echo "<br />Phone No: " . $row3['phone'] . "<br />";
echo "<br />Email: " . $row3['email'] . "<br />";
echo "<br />Class: " . $row3['class'] . "<br />";
echo "<br />Section: " . $row3['section'] . "<br />";
echo "<br />Semester: " . $row3['semester'] . "<br />";
}
?>
<?php
} else if ($usertype == 4) {
$server3 = "localhost";
$user3 = "aukwizcq_user";
$pass3 = "r=Sc!~ZcFoBK";
$db3 = "aukwizcq_deanforms";
$user4 = $_SESSION['username'];
$mysqli = new Mysqli($server3, $user3, $pass3, $db3) or mysqli_error($mysqli);
$deanforms = $mysqli->query("SELECT * FROM deanforms WHERE professor = '$user4'");
$num_rows4 = mysqli_num_rows($deanforms);
echo "Overrides today: " . $num_rows4;
while($row4 = mysqli_fetch_array($deanforms)) {
echo "<br /><br />First Name: " . $row4['name'] . "<br />";
echo "<br />Middle Name: " . $row4['mname'] . "<br />";
echo "<br />Family Name: " . $row4['fname'] . "<br />";
echo "<br />Student ID: " . $row4['sid'] . "<br />";
echo "<br />Scolarship: " . $row4['sc'] . "<br />";
echo "<br />Phone No: " . $row4['phone'] . "<br />";
echo "<br />Email: " . $row4['email'] . "<br />";
echo "<br />Class: " . $row4['class'] . "<br />";
echo "<br />Section: " . $row4['section'] . "<br />";
echo "<br />Semester: " . $row4['semester'] . "<br />";
}
}
?>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
</p>
<?php endif; ?>
</html>
Don't mind the whole code above, the part i am doing the work in is here:
<?php
} else if ($usertype == 1) {
$server = "localhost";
$user = "";
$pass = "";
$db = "";
$db2 = "";
$db3 = "";
$user1 = $_SESSION['username'];
$mysqli = new Mysqli($server, $user, $pass, $db) or mysqli_error($mysqli);
$QUERY = new Mysqli($server, $user, $pass, $db2) or mysqli_error($mysqli);
$QUERY2 = new Mysqli($server, $user, $pass, $db3) or mysqli_error($mysqli);
$overrides = $mysqli->query("SELECT * FROM Overrides WHERE professor = '$user1'");
$num_rows = mysqli_num_rows($overrides);
?>
<?php
echo " Overrides today: " . $num_rows;
?>
<?php
while($row = mysqli_fetch_array($overrides)) { ?>
<fieldset> <?php
echo "First Name: " . $row['name'] . "<br />";
echo "<br />Mid. Name: " . $row['mname'] . "<br />";
echo "<br />Fam. Name: " . $row['fname'] . "<br />";
echo "<br />Student ID: " . $row['sid'] . "<br />";
echo "<br />Scolarship: " . $row['sc'] . "<br />";
echo "<br />Phone No: " . $row['phone'] . "<br />";
echo "<br />Email: " . $row['email'] . "<br />";
echo "<br />Class: " . $row['class'] . "<br />";
echo "<br />Section: " . $row['section'] . "<br />";
echo "<br />Semester: " . $row['semester'] . "<br />";
$name = $row['name'];
$mname = $row['mname'];
$fname = $row['fname'];
$sid = $row['sid'];
$sc = $row['sc'];
$phone = $row['phone'];
$email = $row['email'];
$class = $row['class'];
$section = $row['section'];
$semester = $row['semester'];
?>
<br />
<div>
<form method="post" action="dbheads.php" name="HeadWritingForm" id="HeadWritingForm" autocomplete="off">
<label for="comments" accesskey="c">Notes & Comments:</label><br />
<input type="textarea" name="comments" id="comments" cols="35" rows="10">
</textarea><br>
</div>
<br>
<input type="submit" class="submit" id="submit" value="Accept" /><br><br>
<?php if(Accept) { $prepare = $QUERY->prepare("INSERT INTO `headforms`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`class`,`section`,`semester`,`comments`) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
$prepare->bind_param("sssssssssss", $name, $mname, $fname, $sid, $email, $phone, $sc, $class, $section, $semester, $_POST['comments']); $prepare->execute(); } ?>
</form>
<form method="post" action="dbheads2.php" name="deny" id="deny" autocomplete="off">
<input type="submit" class="submit" id="submit" value="Deny" /><br><br>
<?php if(Deny) { $prepare = $QUERY2->prepare("INSERT INTO `deanforms`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`class`,`section`,`semester`) VALUES (?,?,?,?,?,?,?,?,?,?)");
$prepare->bind_param("ssssssssss", $name, $mname, $fname, $sid, $email, $phone, $sc, $class, $section, $semester); $prepare->execute(); } ?>
</form>
</fieldset>
<br>
<?php }
?>
<br />
dboverride.php:
<?php
$mysql_host = "localhost";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$mysqli = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysqli_error());
$prepare = $mysqli->prepare("INSERT INTO `Overrides`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`class`,`section`,`semester`) VALUES (?,?,?,?,?,?,?,?,?,?)");
$prepare->bind_param("ssssssssss", $_POST['name'], $_POST['mname'], $_POST['fname'], $_POST['sid'], $_POST['email'], $_POST['phone'], $_POST['sc'], $_POST['class'], $_POST['section'], $_POST['semester']);
$prepare->execute();
if ($prepare) {
echo 'Your form has been submitted';
} else {
echo 'Sorry';
}
?>
Upvotes: 0
Views: 48
Reputation: 6621
why instead of adding a whole new row with the same content, you do not create a column "approved_prof" or just "approved", and if it is on the first stage, the default value would be 0, when the professor approves it, you will have to just update the value from 0 to 1, and then check any records that match "approved"="1", to be show to the Head of Dept, so he approves it, at which point you can update it to =2 .... you save alot of mysql resources this way. and checks/updates are much easier
Upvotes: 1