0

I'm trying to insert user data from a form html page to my database, but nothing is being inserted. I'm not getting any mysqli connection errors so I think I'm connecting to the database, but I just feel like nothing is being passed to it. Please any help would be awesome! Thank you! Here is the register.php code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>

</head>

<body>
<?php

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $user = $_POST[user];
        $pwd = $_POST[pwd];

        require 'mysqli_connect.php';

        $query = "INSERT INTO users (user_name, pass, registration_date) VALUES ($user,$pwd, NOW())"; 
        $r = mysqli_query($conn, $query);
        echo $query;

            if ($r) {
                echo "<h1>Thank You</h1>";
            }       
    }

?>    

<form action="register.php" method="post">
<label for="user">User</label>
<input type="text" name="user">
<label for="pwd">Password</label>
    <input type="text" name="pwd">
    <input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Here is my mysqli_connet.php code

<?php

DEFINE ('USER_NAME', 'dhaval');
DEFINE ('PASS', 'open94');
DEFINE ('HOST', 'localhost');
DEFINE ('DATABASE', 'register');

$conn = @ mysqli_connect(HOST, USER_NAME, PASS, DATABASE) OR die ('Could not connect to MySQL: ' . mysqli_connect_error());


mysqli_set_charset($conn, 'utf8');

?>
  • 1
    See what [`mysqli_error`](http://php.net/manual/en/mysqli.error.php) has to tell you, and strings (your variables) should be wrapped in single quotes, `VALUES ('$user', '$pwd', NOW())` – Qirel May 01 '16 at 18:32
  • 1
    Read up on String Literals http://dev.mysql.com/doc/en/string-literals.html and http://php.net/manual/en/mysqli.error.php would have signaled that syntax error. – Funk Forty Niner May 01 '16 at 18:32
  • $_POST[user] should be $_POST['user']. Same thing with pwd. Also this setup is very open to injection attacks. – Garrett R. Davis May 01 '16 at 18:34
  • @GarrettR.Davis I did that but it still won't insert to the database. It was working fine last Monday but won't anymore. And yes I know but this is for my intro to web design class so he doesn't care much. – Dhaval Desai May 01 '16 at 18:37
  • Add this after your query: echo mysqli_error($con) to see what is going wrong. I would also remove the "@" from in front of you connection line, the @ suppresses error output and you really want to see those types of errors. – Garrett R. Davis May 01 '16 at 18:41

0 Answers0