简体   繁体   English

为什么我的php连接到mysql服务器不起作用?

[英]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. 嗨,我正在尝试将信息发送到Sequel pro中显示的本地mysql服务器。 I have a form in html which looks like this: 我有一个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>

When I click the register button this javascript function is suppose to be called: 当我点击注册按钮时,假设这个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');

Which should then call this php file: 然后应该调用这个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);


?>

But when I try this nothing happens. 但是,当我尝试这个没有任何反应。 I don't even get an error message. 我甚至没有收到错误消息。

Someone please help me! 有人请帮帮我!

I think you need to add $connection to your query. 我认为您需要为查询添加$ connection。

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. 这一切都假设您实际上是在访问PHP文件,而这实际上就是问题所在。

You missed to add the database connection to select DB. 您错过了添加数据库连接以选择数据库。 You should write 你应该写

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM