在php中与SQL数据库建立安全连接

时间:2015-01-27 23:54:14

标签: php sql security connection

我正在尝试保护我的数据库凭据,但它们会一直显示在firebug控制台中。

我只想将表单信息保存到数据库中。

我的表单(form.html)

<form id="Form" method="POST" >
    <table>
        <tr>
            <td>Name:</td>
            <td><input id="name" name="name" type="text" /></td>
        </tr>
        <tr>
            <td>Address:</td>
            <td><input id="address" name="address" type="text" /></td>
        </tr>
        <tr>
            <td>Telephone:</td>
            <td><input id="telephone" name="telephone" type="text" /></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><input id="email" name="email" type="email" /></td>
        </tr>
    </table>
    <div class="SubmitButton">
        <input type="submit" id="submit" name="submit" value="Submit" />
    </div>
    </form>

    <div id="Response"></div>
</div>

<!-- javascript to handle form data -->
<script type="text/javascript">    
$("document").ready(function(){
    var frm = $('#Form');
    frm.submit(function (ev) {      
        ev.preventDefault();  

        var email = $('#email').val();
       if(email)
       {
            $.ajax({
                type: "POST",
                dataType:"json",
                url: "submit.php",
                data: frm.serialize(),
                success: function () {
                    $('#Response').empty();
                    $('#Response').wrapInner("<span class='SuccessMessage'>Your information has been submitted successfully!</span>")
                    return;
                },
                error: function () {
                 $('#Response').empty();
                 $('#Response').wrapInner("<span class='ErrorMessage'>Your information was not submitted successfully.  Please try again.</span>")
              }
            });
        } else {
            $('#Response').empty();
            $('#Response').wrapInner("<span class='EmailBlankMessage'>Email address is a required field.</span>")
            return;
        }
    });
});
</script>

这是我的submit.php

<?php
if($_POST)
{
    require(dirname(__FILE__)."/../config.php");

    $name = $_POST["name"];
    $address = $_POST["address"];
    $telephone = $_POST["telephone"];
    $email = $_POST["email"];

    $name = mysql_real_escape_string($name);
    $address = mysql_real_escape_string($address);
    $telephone = mysql_real_escape_string($telephone);
    $email = mysql_real_escape_string($email);

    mysql_query("INSERT INTO XXXXX(name, address, telephone, email) VALUES('$name', '$address', '$address', '$email')"); 


}else { 
    header("Location: /Form.html");
    die();
}

&GT?;

这是我的config.php

 <?

$hostname = "XXXXXX";
$database = "XXXXXX";
$username = "XXXXXX";
$password = "XXXXXX";

mysql_connect($hostname, $username , $password) or die (mysql_error());
mysql_select_db($database ) or die (mysql_error());

配置文件位于根目录之外。但是,我遇到的两个主要问题是它不会将表单数据插入表中,而且config.php中的数据库凭据也会显示在firebug控制台的响应中。我不明白我的代码有什么问题。

1 个答案:

答案 0 :(得分:2)

config.php中您的php开放代码为<? - 这是short open tag,我认为您的配置已禁用该代码。然后您的config.php中的PHP不会被解释并显示为原样。将其更改为常规开放代码<?php

另请注意,不推荐使用mysql扩展程序,您应切换到PDO并准备好语句。