SPA simple registration username stored as “root” instead of given username

假如想象 提交于 2019-12-25 01:44:46

问题


I am creating a simple SPA member registration system. Upon registration - the user gets stored in the database - and redirected to a profile page. Seems fairly simple, however, after trying to register as "joe", with password, "joe", and email, "joe@joe.com", I get redirected to a profile page which displays that my username is "root". I have checked to make sure my mysqli connection variable $username is not getting mixed in with the storing of data (as that has the value of "root"). Here is my code:

checkreglogin.php

<?php

session_start();

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="bonjour3"; // Mysql password
$db_name="itit"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
$mysqli = new mysqli("$host", "$username", "$password", "$db_name")or die("cannot connect");

// Define $myusername and $mypassword
$rmyusername=$_POST['rmyusername'];
$rmypassword=$_POST['rmypassword'];
$myemail=$_POST['myemail'];

$sql2 = $mysqli->prepare("SELECT * FROM $tbl_name WHERE username=? and password=?");
$sql2->bind_param('ss',$rmyusername,$rmypassword);
$sql2->execute();
$sql2->store_result();//apply to prepare statement
$numRows = $sql2->num_rows;
if($numRows != null) {
    echo "Username taken.";
    session_destroy();
}
else {
    // Insert data into mysql
    $sql = "INSERT INTO $tbl_name(username, password, email)VALUES(?,?,?)";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param("sss", $rmyusername, $rmypassword, $myemail);
    $stmt->execute();

    $_SESSION['username'] = $rmyusername;
}
?>

The above script gets run when a user registers. Then they get "redirected" (not really because it's an SPA) to the profile page below:

templates/login_success.php

<?php
session_start();
?>

<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
<div id="logoutlinkdiv" >
    <a href = "#" >Log out</a>
</div>

The username should show "joe" where it says <? echo $_SESSION['username']?>, however it says "root" instead...

UPDATE

js/application.js

$(document).ready(function() {
$.get('templates/regform.php', function(data) {
    $('#register').html(data);

    $("#registerform").submit(function(e) {  
        e.preventDefault();
        if ($('#rmyusername').val() != '' || $('#rmypassword').val() != '' || $('#myemail').val() != '') {
            if(!isValidEmailAddress($('#myemail').val())) {
                alert("Please enter a valid email address");
            }
            else {
                $.post("checkreglogin.php", $(this).serialize(), function(){
                    $("#main").load("templates/login_success.php");
                    $("#login").remove();
                    $("#register").remove();
                });
            }
        } else {
            alert('Please enter a Username/Password and email.');
        }
    });
}); 
});

UPDATE

I changed the $_POST calls to $_GET calls, and I used the url to create a user, and it worked - however I get a blank page with a warning on it:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Users/Eamon/Sites/checkreglogin.php:1) in /Users/Eamon/Sites/checkreglogin.php on line 3

来源:https://stackoverflow.com/questions/17240718/spa-simple-registration-username-stored-as-root-instead-of-given-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!