-2
<?php 
session_start();
$host="localhost";
$username="root";
$password="";
$db_name="registrering";
$tbl_name="users";

$conn = mysqli_connect($host, $username, $password, $db_name);

if(!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

 ?>
<html>
<head>
    <title>user registration system using php and PHP and Mysq</title>
    <!---<link rel="stylesheet" type="text/css" href="style.css">-->
</head>
<body>
<div style="float:right; width:70%">
<table width="150px" border="0" cellpadding="3" cellspacing="1">
        <h2>Registrer<h2/>
        <form method="post" action=" ">
        <br>
            <tr>
                <td>Brugernavn</td>
                <td>:</td>
                <td><input type="text" name="Brugernavn"> </td>
            </tr>
            <br>
            <tr>
                <td>Email</td>
                <td>:</td>
                <td><input type="text" name="Email"> </td>
            </tr>
            <br>
            <tr>
                <td>Password</td>
                <td>:</td>
                <td><input type="password" name="Password"></td>
            </tr>
                <input type="submit" name="registrer" value="Registrer">

                <p>
                    Allerede medlem? <a href="login.php">Log ind</a>
                </p>
        </form>
</div>
</table>
</body>
</html>
<?php
if (isset($_POST["registrer"]))
{
$my_username=$_POST["Brugernavn"];
$my_email=$_POST["Email"];
$my_password=$_POST["Password"];

$sql = "INSERT INTO 'users'(`username`, `email`, `password`) VALUES ('$my_username','$my_email','$my_password')";
$resultat = mysqli_query($conn, $sql);
}
?>

It needs to connect to a database, but it doesn't. It's on a localhost and we can't insert data into a database. The database consists of a username, email and password. We are using varchar(65) and utf8_general_ci.

  • Learn about prepared Statements to prevent SQL injection – Jens Jun 12 '17 at 09:34
  • Check for Errors after execution SQL Statements (`mysqli_error`) – Jens Jun 12 '17 at 09:34
  • is it the connection that's failing, or the query? Are you getting any errors from mysqli_connect_error (after connecting) or mysqli_error (after querying)? And as others have said, your code is vulnerable to SQL injection attacks, you should switch to using parameters to help protect against this. – ADyson Jun 12 '17 at 09:35
  • Im not getting any errors. It just kinda refreshes. – HaxCookiesDK Jun 12 '17 at 11:51

2 Answers2

1

Assuming the connection is working - the insert should have back ticks around the user name, not normal quotes.

$sql = "INSERT INTO `users`(`username`, `email`, `password`) VALUES ('$my_username','$my_email','$my_password')";

I would also recommend looking into prepared statements and bind parameters, not forgetting to NOT store passwords as plane text.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
1

Just a tip. Try using die() to print out the mysql error every time you run a mysql query. Hope it will save you a lot of effort and time in the debugging process. Also use back-ticks users near insert statement.