dimanche 24 avril 2016

Database Connection Successful but will not insert data, yet it can retrieve it

I have searched for answers regarding this but to no avail so far. First off I am developing a Quiz Application, and yes I have to use php and MySQL I don't have a choice. The user enters his/her name into the text box on the login page and the username is stored in a database of names.

Then the database retrieves questions and answers from the database. I know it has something to do with the form action attribute because when I leave it blank it works fine, but otherwise it does not store values. So really I am wondering how I can pass info to a database and at the same time bring the user to another page of my choosing (without opening a new tab).

Any help would be greatly appreciated and I will try to format my code as well as I can. I am new to this so I understand it will be messy and ugly, sorry in advance.

<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();
  }
?>




Aucun commentaire:

Enregistrer un commentaire