-2

I am working on a school project and can't get registration page done right. It just doesn't insert the data in table.

Here is a screenshot for database and table

I have added the HTML, and after going through all your answers I think I am using outdated videos and study material to learn (PHP, HTML, CSS, Website Security).

 <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Register</title>
  </head>
  <body>
    <div class="form">
      <h2>Registeration</h2>
      <form action="" method="post">
              Name<input type="text" name="name" placeholder="First & Last name" required><br><br>
              Username<input type="text" name="username" placeholder="Username"required><br><br>
              Password<input type="password" name="password" placeholder="Keep it Strong"required><br><br>
              Confirm Password<input type="password" name="confirm-pass" placeholder="Re-Enter Password"required><br><br>
              Email<input type="email" name="email" placeholder="Email"required><br><br>
              Phone No.<input type="text" name="phone-no" placeholder="Phone No"required><br><br>
              Gender<input type="text" name="gender" placeholder="Male or Female"required><br><br>
              State<input type="text" name="state" placeholder="State"required><br><br>
              <button type="submit" name="home">Home</button>
              <button type="submit" name="sub">Submit</button>
              <button type="submit" name="reset">Reset</button>
            </form>
    </div>
  </body>
</html>
<?php
$con=mysqli_connect('localhost','root','123456789','eedb');
if ($con)
{
    if (isset($_POST['sub']))
    {
        $n= $_POST['name'];
        $un=$_POST['username'];
        $p=$_POST['password'];
        $cp=$_POST['confirm-pass'];
        $e=$_POST['email'];
        $pn=$_POST['phone-no'];
        $g=$_POST['gender'];
        $s=$_POST['state'];
        mysqli_query($con,"SELECT * FROM `register`");
        $insert= mysqli_query($con,"INSERT INTO `register`(`name`, `username`, `password`, `confirm-pass`, `email`, `phone-no`, `gender`, `state`) VALUES ('$n','$un','$p','$cp','$e','$pn','$g','$s')");
        if ($insert)
        {
            echo "<center>Data Successfully Submitted</center>";
        }
        else
        {
            echo "<center>Data Not Submitted</center>";
        }
    }
}
else
{
    echo "<center>Oh!no there is an error.<br><br>But don't worry we have an army of trained chimpanzies to deal with it.<br><br> <image src='images/chimps.jpg'><br><br>Come Back Soon</center>";
}
if (isset($_POST['home']))
{
    header("location:index.php");
}
if (isset($_POST['reset']))
{
    header("location:register.php");
}
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    you have 1 too many `)` at the end of your insert query – Matt Jul 02 '16 at 19:36
  • still in problem nothing happened – Shivam Mishra Jul 02 '16 at 21:19
  • remove `mysqli_query($con,"SELECT * FROM `register` ");` since that's not doing anything also echo out the insert query and directly paste it into the database and see if it throws any errors like a column name has been spelt wrong. – Matt Jul 02 '16 at 21:22
  • I've reopened the question since you deleted the extra `)`. You will need to post your HTML form in order to know if everything is correct in there. Otherwise, this is guesswork. We also don't know what you entering as data for the POST arrays and MySQL may be complaining about that, but you're not checking for errors via PHP and MySQL. So, do that. – Funk Forty Niner Jul 02 '16 at 21:35
  • i have tried to run queries separately and never got an error and even It actually enters the data in tables, but not from PHP. There are no spelling mistakes for column.i have also given screenshot of my DataBase just in case you want it – Shivam Mishra Jul 02 '16 at 23:55
  • As Fred says in his answer, getting an error from the MySQLi driver is essential here - let the system tell you what is wrong! This is the first thing to fix. – halfer Jul 10 '16 at 09:39

2 Answers2

1

Since you didn't post your HTML form, I am posting the following answer, which is what your HTML form should (basically) look like, which btw only contains the one example element.

You will need to fill in the rest and follow the same convention.

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

First name:
<input type="text" name="name">
<br>
<input type="submit" name="sub" value="Submit">

</form>

Then escape your values, should there contain any characters that MySQL may complain about, such as apostrophes.

I.e.: Doug's Bar & Grill.

Then:

$n= mysqli_real_escape_string($con, $_POST['name']);

Something you should use or a prepared statement since your code is presently open to an SQL injection.

And do the same for all your other POST arrays.

The name attribute is required for POST arrays when using pure PHP and a post method for the HTML form.

Sidenote: Your entire code is dependant on the following conditional statement
if (isset($_POST['sub'])){...}. So make sure that that form element bears the same name attribute for it.

  • Check for errors also.

Since this does not help you:

echo "<center>Data Not Submitted</center>";

References:

It's unclear as to what you're trying to do with this line:

mysqli_query($con,"SELECT * FROM `register` ");

If the goal is to check if a row exists, then consult one of my answers on Stack:


Passwords

I also noticed that you may be storing passwords in plain text. This is not recommended.

Use one of the following:

Important sidenote about column length:

If and when you do decide to use password_hash() or the compatibility pack (if PHP < 5.5) https://github.com/ircmaxell/password_compat/, it is important to note that if your present password column's length is anything lower than 60, it will need to be changed to that (or higher). The manual suggests a length of 255.

You will need to ALTER your column's length and start over with a new hash in order for it to take effect. Otherwise, MySQL will fail silently.

Other links of interest:


HTML stickler:

The <center> tag is deprecated and has been removed from Web standards.

For more information, visit the following:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

you can try this

$insert= mysqli_query($con,"INSERT INTO `register`(`name`, `username`, `password`, `confirm-pass`, `email`, `phone-no`, `gender`, `state`)VALUES ('$n','$un','$p','$cp','$e','$pn','$g','$s')");

you can try to use notepad++ or any other editor (e.g netbeans )that will make the open and closed brackets more obvious .

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
JokerDev
  • 151
  • 2
  • 15