-1

I'm new to PHP, so I have a hard time finding my errors, and this one I simply can't find. The code I have simply doesn't register the user on submit, and I have no clue why it doesn't perform the last bit of code. - It stops when it comes to the "insert into" function.

Any suggestions?

<?php 
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Member system version 1.0</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
    <h1>Simpelt login system</h1>
</header>
<nav>
    <ul>
        <li><a href="./">Forside</a></li>
        <li><a href="login.php">Log ind</a></li>
        <li><a href="register.php">Registrer dig</a></li>
        <li><a href="memberpage.php">Medlemsside</a></li>
        <li><a href="contact.php">Kontakt</a></li>
    </ul>
</nav>
<section>
    <aside>
        <ul>
            <li><h3>Lille test overskrift</h3></li>
            <li></li>
        </ul>
    </aside>
    <article>
        <?php
            if($_POST['registerbtn']) {
                $getuser = $_POST['user'];
                $getemail = $_POST['email'];
                $getpass = $_POST['pass'];
                $getconfirmpass = $_POST['confirmpass'];

                if ($getuser){
                    if ($getemail) {
                        if ($getpass) {
                            if ($getconfirmpass) {
                                if ($getpass === $getconfirmpass) {
                                    if ((strlen($getemail) >= 7) && (strstr($getemail, "@")) && (strstr($getemail, "."))) {
                                        require("connect.php");

                                        $query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
                                        $numrows = mysql_num_rows($query);
                                        if ($numrows == 0) {
                                            $query = mysql_query("SELECT * FROM users WHERE email='$getemail'");
                                            $numrows = mysql_num_rows($query);
                                            if ($numrows == 0) {
                                                $getpass = md5(md5("Jasdl".$getpass."SDa823k"));

                                                $date = date("F, d Y");
                                                $code = md5(rand());

                                                mysql_query("INSERT INTO users VALUES(
                                                    '', '$getuser', '$getpass', '$getemail', '0', '$code', '$date'  
                                                )");

                                                $query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
                                                $numrows = mysql_num_rows($query);
                                                if ($numrows == 1) {

                                                    $site = "http://localhost:8888/php-projekter/login-projekt";
                                                    $webmaster = "Kasper Legarth <kaspersunivers@gmail.com>";
                                                    $headers = "From: $webmaster";
                                                    $subject = "Aktivering af din konto";
                                                    $message = "Tak fordi du oprettede en konto, for at aktivere din konto skal du klikke på nedstående link.\n";
                                                    $message .= "$site/activate.php?user=$getuser&code=$code\n";
                                                    $message .= "Du skal aktivere din konto for at kunne logge ind.";

                                                    if (mail($getemail, $subject, $message, $headers)) {
                                                        $confirmpass_error = "DET VIRKER!!!!";
                                                        $getuser = "";
                                                        $getemail = "";
                                                    }
                                                    else
                                                        $confirmpass_error = "Der skete en fejl, din mail blev ikke sendt";
                                                }
                                                else
                                                    $confirmpass_error = "Der skete en fejl, din bruger blev ikke oprettet";
                                            }
                                            else
                                                $email_error = "Den email er allerede i brug";  
                                        }
                                        else
                                            $user_error = "Det brugernavn er desværre allerede taget";

                                        mysql_close();
                                    }
                                    else
                                        $email_error = "Du skal bruge en gyldig email";
                                }
                                else
                                    $confirmpass_error = "Dine adgangskoder er ikke ens";
                            }
                            else
                                $confirmpass_error = "Du skal skrive din kode igen";
                        }
                        else
                            $pass_error = "Du skal vælge en adgangskode";

                    }
                    else
                        $email_error = "Du skal indstate din email";
                }
                else
                    $user_error = "Du skal indtaste et brugernavn";

            }

        $form = "<form action='register.php' method='post'>
            <h2>Opret din egen konto</h2>
            <p>Ønsket brugernavn:
            <br><input type='text' name='user' value='$getuser' placeholder='Brugernavn' />
            <br><small>$user_error</small></p>
            <p>Din email adresse:
            <br><input type='text' name='email' value='$getemail' placeholder='Email adresse' />
            <br><small>$email_error</small></p>
            <p>Vælg en adgangskode:
            <br><input type='password' name='pass' value='' placeholder='Adgangskode' />
            <br><small>$pass_error</small></p>
            <p>Gentag adgangskode:
            <br><input type='password' name='confirmpass' value='' placeholder='Gentag' />
            <br><small>$confirmpass_error</small></p>
            <p><input type='submit' name='registerbtn' value='Registrer dig' /></p>
        </form>";

        echo $form;

        ?>
    </article>
</section>
<footer>
    Copyright &copy; 2013 - Design: <a href="http://kaspersunivers.dk">Kasper Legarth</a>
</footer>
 </body>
 </html>
mdml
  • 22,442
  • 8
  • 58
  • 66
Legarndary
  • 957
  • 2
  • 16
  • 37
  • What problems are you facing? Dont just show code, detail your problem so we can help you. Also, there's several ways to make your code more readable. Not tell us, what the mysql function output? – Patrick Bassut Nov 05 '13 at 01:36
  • 1
    `INSERT INTO users >>> (?,?,?,?,?) <<< VALUES` ? `$????? = "what?";` [**`INSERT`**](http://beginner-sql-tutorial.com/sql-insert-statement.htm) - You're not telling WHAT to INSERT INTO. – Funk Forty Niner Nov 05 '13 at 01:43
  • 1
    There's so many things wrong with this code that it's impossible to start with any one thing. If you're positive it's your mysql statement you can do mysql_query("QUERY HERE") or die(mysql_error()); to die with the reason for the error... – skrilled Nov 05 '13 at 01:46
  • Note that the mysql extension is now deprecated and will be removed sometime in the future. That's because it is ancient, full of bad practices and lacks some modern features. Don't use it to write new code. Use PDO or mysqli_* instead. Your query is prone to SQL Injection. – Mark Nov 05 '13 at 02:07
  • for your insert... if the first column is an `auto_increment` id of some sort, I think you need to use `null` instead of an empty string. It would be something like `insert into users values (null, '$getuser', ...)` – gloomy.penguin Nov 05 '13 at 02:15
  • 1
    Ewww, that code is nasty! – Federico Piragua Nov 05 '13 at 02:18
  • @gloomy.penguin You may very well be right (about NULL). It's hard to say what type of structure the OP has. There's one thing for sure though, there are no values to be passed, and that's where the "brick wall" comes into effect! – Funk Forty Niner Nov 05 '13 at 02:20

2 Answers2

0

You have an error in your insert statement.

    mysql_query("INSERT INTO users VALUES('', '$getuser', '$getpass', '$getemail', '0', '$code', '$date')");

You are setting the values that you are gonna insert into the Database but not the fields where those values go, try this

mysql_query("INSERT INTO users (field1, field2, field3, field4, field5, field6, field7) VALUES ('','$getuser','$getpass','$getemail','0','$code','$date')");

(Replace "field1, field2, etc" for the name of your fields on the Table)

Also you are inserting an empty value on first field, not sure if it's on purpose but also check on that.

Hope it works for you

DJ22T
  • 1,628
  • 3
  • 34
  • 66
0

"ONE" of the reasons as to why it's not working is that you are not telling it "WHAT" to INSERT INTO

For example and assuming your DB columns are named:

id - getuser - getpass - getemail - column_for_0 - code - date

You would need to do something like:

mysql_query("INSERT INTO users (id, getuser, getpass, getemail, column_for_0, code, date) 
VALUES('', '$getuser', '$getpass', '$getemail', '0', '$code', '$date' )");

I almost must note that your code is open to injection and MySQL_ is deprecated therefore it is strongly suggested to now use MySQLi_ and/or PDO.

You also need to make sure that session_start(); is inside ALL your linked or included files.

I must also make another note that storing passwords using MD5 is no longer considered safe.

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