试图写入一个mysql数据库,但它一直是空集

时间:2017-12-08 14:48:32

标签: php mysql

对于学校项目,我正在尝试写一个名为enrolment的表格,在这个表格中,学生编号和他们选择的课程在经过测试后添加,以确保学生姓名和编号存在于另一个数据库中。没有错误出现,但是当我检查我的数据库之后enrolment说它是一个空集。有没有人有建议?

<?php

require 'connect.php';

//making a variable from the user data
$name = $_POST["name"];
$number = $_POST["snumber"];
$course = $_POST["pcourse"];

//linking up the database
$link = mysqli_connect(HOST, USER, PASS, DB) or die (mysqli_connect_error());

// select all from table student which show student name and number
$squery = "SELECT * FROM student";
$sresult = mysqli_query($link, $squery);

$found = 0;

while ($srow = mysqli_fetch_array($sresult)) {

    // testing if the student name and number match the users data
    if ($name == $srow['family'] && $number == $srow['uid']) {

        $enrol = "INSERT INTO enrolment (uid course) VALUES('$number' '$course')";
        $found = 1;
        break;
    }
}
mysqli_close($link);

?>

<html>
    <body>
        <form action="index.php" method="post">
            <br>
            <input type = "submit" value="back" name="back">
        </form>
    </body>
</html>

index.php(表格)

<!DOCTYPE html>
<html>
  <body>
    <h1>Course Selection</h1><br>


    <form action="next.php" method="post">


              Name: <input type="text" name="name" placeholder="Name" required="required" maxlength="50">
              <br><br>

              Student Number: <input type="text" name= "snumber" required="required" maxlength="9">
              <br><br>

        <?php
        //form
      require 'connect.php';

       echo "Select a course: <select name = \"pcourse\">\n";

      $link = mysqli_connect(HOST, USER, PASS, DB) or die(mysqli_connect_error());

      $query = "SELECT * FROM course";
      $result = mysqli_query($link, $query);

      while ($row = mysqli_fetch_array($result)) {
        echo "<option> $row[code] $row[name] $row[maxenroll]</option><br>";
      }

      mysqli_free_result($results);

      mysqli_close ($link);

      echo " </select>\n";


      ?>

      <br><br>
      <input type = "submit" value="submit" name= "submit">

    </form>

    </body>
    </html>

2 个答案:

答案 0 :(得分:0)

您的插入代码只是一个字符串。您应该向mysql发送您的插入代码。试试这个

$enrol = "INSERT INTO enrolment (uid, course) VALUES($number, $course)";
$link->query($enrol);

答案 1 :(得分:-2)

我的猜测是,当从student表中检查结果集时,其中没有familyuid,这意味着 - 在表中。而不是立即插入,尝试显示数据库中的匹配记录 - 如果这实际上是您想要找到的。然后你可以检查数据库中实际存储的内容 - 你可以比较两者 其他的事情是 - 为什么不限制选择以确定该学生?
重建您的查询,例如:
$squery = "select * from student where family='".$name."' and uid='".$number."'"
然后,您可以检查选择了多少条记录,并在进行任何插入之前显示该数字。