Search code examples
javascriptphpmysqlconnectionsequelpro

Why won't my php connection to mysql server work?


Hi I'm trying to send information to my local mysql server that is shown in Sequel pro. I have a form in html which looks like this:

    <a href="#openRegisterModal" id="main_header_register">register</a>
<div id="openRegisterModal" class="modalRegisterDialog">
    <section>
        <a href="#close" title="Close" class="close">X</a>
        <h1>Register</h1>
        <form name="registerform" class="registerform">
            <label for="usernamefield">Username: </label>
            <br>
            <input type="text" id="usernameregisterfield"></input>
            <br>
            <label for="emailfield">Email: </label>
            <br>
            <input type="text" name="emailfield" id="emailregisterfield"></input>
            <br>
            <label for="passwordfield">Password: </label>
            <br>
            <input type="password" name="passwordfield" id="passwordregisterfield"></input>
            <br>
            <label for="passwordrepeatfield">Repeat Password: </label>
            <br>
            <input type="password" name="passwordrepeatfield" id="passwordrepeatregisterfield"></input>
            <br>
            <!--Check field-->
            <br>
            <input type="checkbox" name="agreementbox" id="agreementbox"></input>
            <label for="agreementbox" id="termslink">I agree to the </label>
            <a href="" id="termslink">terms</a>
            <br>
            <input type="button" id="registerbutton" value="Register" onclick="registerUser()">

        </form>
    </section>
</div>

When I click the register button this javascript function is suppose to be called:

function initiate() {

}

function registerUser()
{

    var username = document.getElementById("usernameregisterfield").value;
    var email = document.getElementById("emailregisterfield").value;
    var password = document.getElementById("passwordregisterfield").value;


    console.log("Username: " + username);
    console.log("Email: " + email);
    console.log("Password: " + password);

    if (window.XMLHttpRequest)
    {
        // Code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // Code for IE6, IE5
        xmlhttp = ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET",
                 "RegisterUser.php?username=" + username
                 + "&email=" + email
                 + "&password=" + password,
                 true);

    xmlhttp.send();


}

initiate('load');

Which should then call this php file:

<?php

    $username = strval($_GET['username']);
    $email = strval($_GET['email']);
    $password = strval($_GET['password']);

    $connection = mysql_connect('localhost', 'root', '')
        or die("Unable to connect to database");


    $database = mysql_select_db("myDatabase")
        or die("Could not select database");

    $sql = "INSERT INTO registered_users VALUES ('$username', '$email', '$password');";
    $execute = mysql_query($sql);


?>

But when I try this nothing happens. I don't even get an error message.

Someone please help me!


Solution

  • I think you need to add $connection to your query.

    Firstly add your database to the connection statement

    $connection = mysql_connect('localhost', 'root', '', 'myDatabase')
        or die("Unable to connect to database");
    

    then after you build your query change the next line to this

    $execute = mysql_query($connection, $sql);
    

    This is all presuming that you're actually getting to the PHP file and that is where the problem actually is.