3

嗨,我正在尝试将信息发送到 Sequel pro 中显示的本地 mysql 服务器。我在 html 中有一个如下所示的表单:

    <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>

当我单击注册按钮时,应该调用此 javascript 函数:

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');

然后应该调用这个 php 文件:

<?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);


?>

但是当我尝试这个时,什么也没有发生。我什至没有收到错误消息。

有人请帮助我!

4

2 回答 2

2

我认为您需要将 $connection 添加到您的查询中。

首先将您的数据库添加到连接语句

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

然后在您构建查询后将下一行更改为此

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

这一切都假设您实际上正在访问 PHP 文件,而这正是问题所在。

于 2013-11-12T11:37:14.513 回答
2

您错过了添加数据库连接以选择 DB。你应该写

$database = mysql_select_db("myDatabase", $connection)
    or die("Could not select database");
于 2013-11-12T11:37:29.197 回答