数据库连接成功但不会插入数据,但它可以检索它

时间:2016-04-24 19:23:06

标签: php mysql web mysqli

我已经搜索了关于此的答案,但到目前为止无济于事。首先,我正在开发一个测验应用程序,是的,我必须使用PHP和MySQL我没有选择。用户在登录页面的文本框中输入他/她的姓名,用户名存储在名称数据库中。

然后数据库从数据库中检索问题和答案。我知道它与表单操作属性有关,因为当我把它留空时它工作正常,但是否则它不存储值。所以我真的想知道如何将信息传递给数据库,同时将用户带到我选择的另一个页面(无需打开新选项卡)。

我们将非常感谢任何帮助,我会尝试尽可能地格式化我的代码。我是新手,所以我明白这将是凌乱和丑陋,提前抱歉。

<DOCTYPE html>
<html>
  <head>
    <title>Entrance Page</title>
  </head>
  <body>
  <h1>Welcome to our Quiz App V1 Alpha</h1>
<p>Please select which quiz you would like to take, you will take all 10
  questions and at the end you can see how you stack up against everyone
  else. Please enter your name below.</p><br />

  <!-- This form will take in an entered username and put it into a database,
  if it is already in the database then they will be asked to take a new test,
  but they cannot retake an old one -->


<form action="HytecCampQuestionPage.php" method="POST">
  <p>Username:
  <input type="text" name="name" id="name">
  <input type="submit" value="Submit">

  <?php

  include 'HytecFunctions.php';
  if((isset($_POST["name"]))  ) {
  $name = $_POST["name"];
  addName($name);
  }
  else {
    echo "It didn't work";
  }
  ?>
</form>
</body>
</html>



    <DOCTYPE html>
<html>
<head>
<title>Question Page</title>
  </head>
  <body>
    <?php
    include 'HytecFunctions.php';
    $questionNumber = 1;
    while ($questionNumber <= 10) {
      showQuestion($questionNumber);
      $questionNumber++;
    }
    ?>
  </body>
</html>


    <?php

function connectDB() {
  $servername = "localhost";
  $username = "root";
  $password = "C1e2r3n4y5f629#";
  $dbname = "QuizApp";

  //Create a connection object and return it to the caller

  $conn = new mysqli($servername, $username,
  $password, $dbname);

  if ($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
  }

  return $conn;
  }

      function addName($name) {
        $conn = connectDB();

    //Insert into the QuizApp table the element with the passed information

$sql = $conn->prepare("INSERT INTO Names (Name, Score)
VALUES(?, 0)");

// Puts values into above ?s

$sql->bind_param("s", $name);
$sql->execute();

$sql->close();
$conn->close();
  }

  function showQuestion($number) {
$conn = connectDB();

//Select everything from the Question table and print it to the screen within <radio tags>

$sql = $conn->prepare("SELECT id, question, a, b, c, d, answer FROM Question WHERE id=$number");

$sql->execute();
$sql->bind_result($id, $question, $a, $b, $c, $d, $answer);

while($sql->fetch()) {
  echo "<h1>Question $id </h1>";
  echo "<p>$question</p>";
  echo "<form>
    <input type=\"radio\" name=\"answer\" value=\"a\">$a<br>
    <input type=\"radio\" name=\"answer\" value=\"b\">$b<br>
    <input type=\"radio\" name=\"answer\" value=\"c\">$c<br>
    <input type=\"radio\" name=\"answer\" value=\"d\">$d<br>
    <input type=\"submit\" name=\"Submit\">
  </form>";
  echo "<a href=\"http://localhost/Html/QuizApp/HytecCampProjectLogin.php\">Login Page </a>";
}
$sql->close();
$conn->close();
  }
?>

0 个答案:

没有答案