表单数据未插入数据库

时间:2015-04-28 17:34:22

标签: php html mysqli

这是我单击提交按钮时将数据写入另一个数据库的代码。有两个按钮,一个是accept,一个是deny,当单击accept时,它将写入名为headforms的数据库,当单击deny时,它会将数据写入名为deanforms的数据库。由于我的基本html / php / sql技能,请忽略我编写代码的方式,如果有人帮助我会很棒!

    <?php
} else if ($usertype == 1) { 
$server = "localhost";
$user = "";
$pass = "";
$db = "";
$user1 = $_SESSION['username'];
$mysqli  = new Mysqli($server, $user, $pass, $db) 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>
<label for="comments" accesskey="c">Notes & Comments:</label><br />
<textarea name="comments" cols="35" rows="10">
</textarea><br>
</div>
<br>
            <form method="post" action="dbheads.php" name="HeadWritingForm" id="HeadWritingForm" autocomplete="off">
    <input type="submit" class="submit" id="submit" value="Accept" /><br><br>
        </form>
                <form method="post" action="dbheads2.php" name="deny" id="deny" autocomplete="off">
    <input type="submit" class="submit" id="submit" value="Deny" /><br><br>
    </form>

    </fieldset>
    <br>
<?php    }
?>
<br />

我的dbheads.php

    <?php
$mysql_host     = "localhost";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$mysqli  = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysql_error());


$prepare = $mysqli->prepare("INSERT INTO `headforms`(`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();   
if ($prepare) {
echo 'accepted';
} else {
echo 'sorry';
}
?>

我的dbheads2.php

    <?php
$mysql_host     = "localhost";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$mysqli  = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysql_error());


$prepare = $mysqli->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();   
if ($prepare) {
echo 'denied';
} else {
echo 'Sorry';
}
?>

单击“接受”时,将在接受的页面上打印,单击“拒绝”时,将打印拒绝,但不会写入数据库。提前谢谢!

2 个答案:

答案 0 :(得分:1)

由于Prepared不为null,因此听起来您有数据库问题。数据库中的某些东西不允许你写入它。

对你来说非常方便的是看看数据库方面发生了什么。

MySQL有一个很棒的功能,它允许您查看已执行的查询并多次帮助过我。

SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';

查看表mysql.general_log

select * from  mysql.general_log;

完成后请务必设置general_log = 'OFF';,以便日志不会不断增长。

在打开日志之前,我通常会清除日志:

truncate mysql.general_log;

答案 1 :(得分:0)

您必须设置$ user =“”;作为root(默认)或你拥有的任何东西,并提供数据库名称,如$ db =“你的数据库名称”,这样php就可以知道数据应插入哪个数据库或者需要交互的数据库。