0

I want to make a student registration form. I get no error messages, but the data is not entered into the database.

INDEX is for INDEX NUMBER USERNAME is for USER EMAIL is for EMAIL

I had trouble with index variable it said that it is undefined, I am not sure if I defined the variable properly.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- TemplateBeginEditable name="doctitle" -->
<title>Untitled Document</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>

<body>

<?php
    session_start();
    $_SESSION["message"] = "";


    $mysqli = new MySQLi("localhost","root","","accounts");

    if ($_SERVER['REQUEST_METHOD'] == "POST"){
        $username = $mysqli->real_escape_string($_POST["username"]);
        $email = $mysqli->real_escape_string($_POST["email"]);

        $index = ($_POST["email"]);



        $_SESSION["username"] = $username;
        $_SESSION["email"] = $email;



        $sql = "INSERT INTO users (index, email, name) "
    . "VALUES ('$index', '$email', '$username')";   

        //check if mysql query is successful
if ($mysqli->query($sql) === true)
{
    $_SESSION[ 'message' ] = "Registration succesful! Added $username to the database!";
    //redirect the user to welcome.php
    header( "location: welcome.php" );
}
        else{
            $_SESSION["message"] = "user could not be added to the database";
        }



    }
    else{
            $_SESSION["message"] = "could not initiate session";
        }





    ?>



<link href="//db.onlinewebfonts.com/c/a4e256ed67403c6ad5d43937ed48a77b?family=Core+Sans+N+W01+35+Light" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="form.css" type="text/css">
<div class="body-content">
  <div class="module">
    <h1>Create an account</h1>
    <form class="form" action="form.php" method="post" enctype="multipart/form-data" autocomplete="off">
      <div class="alert alert-error"></div>

      <input type="text" placeholder="User Name" name="username" required />
<input type="email" placeholder="Email" name="email" required />
      <br>

      <input type="text" placeholder="Index Number" name="index" required /><br>

      <input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
    </form>
  </div>
</div>



</body>
</html>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 4
    Possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](https://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – Sean Dec 12 '18 at 03:04
  • 1
    `index` is a MySQL reserved word - https://dev.mysql.com/doc/refman/5.5/en/keywords.html#keywords-5-5-detailed-I. In order to use it as a column name, you must escape it - `INSERT INTO users (\`index\`, email, ...` – Sean Dec 12 '18 at 03:06
  • Than.k you very much. It works now . – prem6667777 Dec 12 '18 at 03:14
  • Welcome to StackOverflow. Please don't use ALL CAPS in your questions in future. – Graham Dec 12 '18 at 05:11
  • @Graham: Please try to use exception or error handling so it will easy to find out received error. I.e Just simply add or die(mysqli_error($db)); at the end of your query, this will print the mysqli error. mysqli_query($db, $inserQuery) or die(mysqli_error($db)); Thank you – Dipti Dec 12 '18 at 05:38
  • @Dipti I just edited the question from the "Help & Improvement" queue, it's not me you need to tag. – Graham Dec 12 '18 at 06:17

1 Answers1

0

Change this line :

$sql = "INSERT INTO users (index, email, name) "
. "VALUES ('$index', '$email', '$username')";    

To this :

$sql = 'INSERT INTO users (index, email, name) '
. 'VALUES ("$index", "$email", "$username")';   

Hope to solve your problem.

Farhad Lavaei
  • 452
  • 4
  • 7