-1

When I click the submit button on index.php page, absolutely nothing happens which surprises me since I'm following a tutorial and the code is exactly the same. I checked it over several times but to no avail. One thing I noticed is that when I enter http://localhost:8888/phplessons/signup.php page, it actually creates a row in my database, however the row only has id which is the primary key and the auto-incremented field whilst first, last, username and password fields are empty. I can only assume something is wrong with my button?

index.php code:

<?php
    include 'db.php';
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title>Playground</title>
        <meta name="viewport" content="width=device-width, initial-scale=1 shrink-to-fit=no">
    </head>
<body>

<form action="signup.php" method="POST">
    <input type="text" name="first" placeholder="First Name"></form>
    <input type="text" name="last" placeholder="Last Name"></form>
    <input type="text" name="username" placeholder="Username"></form>
    <input type="password" name="password" placeholder="Password"></form>
    <button type="submit">Sign Up</button>
</form>
</body>
</html>

signup.php code:

<?php
    include 'db.php';
    $first = $_POST['first'];
    $last = $_POST['last'];
    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "INSERT INTO user (id, first, last, username, password) 
    VALUES (NULL, '$first','$last','$username','$password')";
    $result = mysqli_query($conn, $sql);
?>
  • What kind of error message are you getting please show your errors in details so we can help to debug it – Code Cooker May 17 '17 at 12:01
  • 1
    Your code is failing outright and this is a repost from a deleted question (I remember it). You see all of these in the inputs ``? There; remove them. That's why your code failed. – Funk Forty Niner May 17 '17 at 12:03
  • 1
    btw; if you want to keep your database intact, do NOT use this code. I will guarantee that you **will** get hacked. – Funk Forty Niner May 17 '17 at 12:08
  • Voted to close as a typo. – Funk Forty Niner May 17 '17 at 12:12
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 17 '17 at 12:45
  • **Never store plain text passwords!** Please use ***PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html)*** to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). ***It is not necessary to [escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard May 17 '17 at 12:45

2 Answers2

0

Correct your form html.

<form action="signup.php" method="POST">
    <input type="text" name="first" placeholder="First Name">
    <input type="text" name="last" placeholder="Last Name">
    <input type="text" name="username" placeholder="Username">
    <input type="password" name="password" placeholder="Password">
    <button type="submit">Sign Up</button>
</form>
Saurabh Parekh
  • 441
  • 1
  • 3
  • 11
0
 <?php
    include 'db.php';
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title>Playground</title>
        <meta name="viewport" content="width=device-width, initial-scale=1 shrink-to-fit=no">
    </head>
<body>

<form action="signup.php" method="POST">
    <input type="text" name="first" placeholder="First Name">
    <input type="text" name="last" placeholder="Last Name">
    <input type="text" name="username" placeholder="Username">
    <input type="password" name="password" placeholder="Password">
    <button type="submit">Sign Up</button>
</form>
</body>
</html>

end form tag properly

Anju
  • 502
  • 1
  • 6
  • 20