使用提交功能将数据从数据库写入另一个数据库

时间:2015-05-01 09:45:46

标签: php html sql

我知道这类问题对于这里的人来说太容易了,但我是PHP / SQL的初学者,因此出于个人学习的原因,我正在为容量方面的课程制作覆盖文件系统。它由学生填写覆盖表单,其提交到具有教授姓名的覆盖数据库。当教授登录时,他会看到要求进入班级的覆盖表格。在教授接受表格之后,它也会被部门负责人接受。现在,问题在于:当教授点击"接受"时,我会如何制作它,它会用一个额外的行写出相同的数据(称为教授对部门主管的评论评论)进入head数据库并将其从当前覆盖数据库中删除,以便它不再存在,因为教授已接受覆盖。

我尝试将其作为一个表单并且它可以工作,它提交到Head的数据库但是每当我尝试将注释部分或任何新列添加到代码中时,它就会停止写入数据库。此外,如果数据库中有多个覆盖显示为教授,并且一个被接受/拒绝,它会将所有覆盖写入数据库而不是接受/拒绝的覆盖。如果我在代码中犯了错误,那将会很棒。请忽略我的代码的样式,因为我是一个php初学者,现在它很乱。

这是我能够做的,这是全班代码:

    <?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>&nbsp;&nbsp;&nbsp;&nbsp;
<?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>&nbsp;&nbsp;&nbsp;&nbsp;
<?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>&nbsp;&nbsp;&nbsp;&nbsp;
<?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>&nbsp;&nbsp;&nbsp;&nbsp;
<?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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?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>&nbsp;&nbsp;&nbsp;&nbsp;
<?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>&nbsp;&nbsp;&nbsp;&nbsp;
<?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>&nbsp;&nbsp;
            <input name="class" type="text" id="class" size="50" required />
        </div>
<br>    
        <div>
            <label for="section" accesskey="o">Section</label>&nbsp;&nbsp;
            <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 />&nbsp;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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* Date and time are recorded for various reasons.</font></i>

<br><br>
            <label for="date" accesskey="c">&nbsp;&nbsp&nbsp;&nbsp;&nbsp;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" />&nbsp;&nbsp;&nbsp;<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 "&nbsp;Overrides today: " . $num_rows; 
?>

    <?php
    while($row = mysqli_fetch_array($overrides)) { ?>
    <fieldset>  <?php
         echo "First Name:&nbsp;&nbsp; " . $row['name'] . "<br />";
         echo "<br />Mid. Name:&nbsp;&nbsp; " . $row['mname'] . "<br />";
         echo "<br />Fam. Name:&nbsp;&nbsp; " . $row['fname'] . "<br />";
         echo "<br />Student ID:&nbsp;&nbsp;&nbsp;&nbsp;" . $row['sid'] . "<br />";
         echo "<br />Scolarship:&nbsp;&nbsp;&nbsp;&nbsp; " . $row['sc'] . "<br />";
         echo "<br />Phone No:&nbsp;&nbsp;&nbsp;&nbsp; " . $row['phone'] . "<br />";
         echo "<br />Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['email'] . "<br />";
         echo "<br />Class:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['class'] . "<br />";
         echo "<br />Section:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['section'] . "<br />";
         echo "<br />Semester:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $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>

不要介意上面的整个代码,我正在做的工作就在这里:

<?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 "&nbsp;Overrides today: " . $num_rows; 
?>

    <?php
    while($row = mysqli_fetch_array($overrides)) { ?>
    <fieldset>  <?php
         echo "First Name:&nbsp;&nbsp; " . $row['name'] . "<br />";
         echo "<br />Mid. Name:&nbsp;&nbsp; " . $row['mname'] . "<br />";
         echo "<br />Fam. Name:&nbsp;&nbsp; " . $row['fname'] . "<br />";
         echo "<br />Student ID:&nbsp;&nbsp;&nbsp;&nbsp;" . $row['sid'] . "<br />";
         echo "<br />Scolarship:&nbsp;&nbsp;&nbsp;&nbsp; " . $row['sc'] . "<br />";
         echo "<br />Phone No:&nbsp;&nbsp;&nbsp;&nbsp; " . $row['phone'] . "<br />";
         echo "<br />Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['email'] . "<br />";
         echo "<br />Class:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['class'] . "<br />";
         echo "<br />Section:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['section'] . "<br />";
         echo "<br />Semester:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $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';
}
?>

1 个答案:

答案 0 :(得分:1)

为什么不添加具有相同内容的整个新行,而不是创建列“approved_prof”或只是“已批准”,如果它在第一个阶段,则默认值为0,当教授批准它,你将只需要将值从0更新为1,然后检查任何匹配“approved”=“1”的记录,以显示给部门负责人,所以他批准它,此时你可以将它更新为= 2 ....你以这种方式保存了很多mysql资源。和检查/更新更容易