使用PHP到MySQL创建帐户


Create account using PHP to MySQL

我对PHP/MMySQL不太了解,请原谅我的无知。我在我的网站上有一个登录表单,我已经创建了一个创建帐户的表单,从视觉上看一切都很好,但是,我需要将数据发送到mysql中的客户帐户表。当我提交表单时,它似乎可以工作,创建ID 1,但所有其他字段都是空白的。

PHP:

<?php
$hostname="localhost";
$username="createaccount";
$password="******";
$dbname="Island_Web_Design";
$usertable="customer_accounts";
$connection = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname, $connection);
$sql="INSERT INTO customer_accounts (firstname, lastname, email, password)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[password]')";

if (!mysql_query($sql,$connection))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
mysql_close($con);

?>

HTML:

<form role="form" action="Create_Account.php">
                            <div class="row">
                                <div class="col-xs-6 col-sm-6 col-md-6">
                                    <div class="form-group">
                                        <input type="text" name="firstname" id="firstname" class="form-control input-sm floatlabel createinput" placeholder="First Name">
                                    </div>
                                </div>
                                <div class="col-xs-6 col-sm-6 col-md-6">
                                    <div class="form-group">
                                        <input type="text" name="lastname" id="last_name" class="form-control input-sm createinput" placeholder="Last Name">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <input type="email" name="email" id="email" class="form-control input-sm createinput" placeholder="Email Address">
                            </div>
                            <div class="row">
                                <div class="col-xs-6 col-sm-6 col-md-6">
                                    <div class="form-group">
                                        <input type="password" name="password" id="password" class="form-control input-sm createinput" placeholder="Password">
                                    </div>
                                </div>
                                <div class="col-xs-6 col-sm-6 col-md-6">
                                    <div class="form-group">
                                        <input type="password" name="password_confirmation" id="password_confirmation" class="form-control input-sm createinput" placeholder="Confirm Password">
                                    </div>
                                </div>
                            </div>
                            <input type="submit" name="submit" value="Create Account" class="btn btn-primary btn-block register">
                        </form>

提前谢谢。

对于初学者,您应该停止为新代码使用mysql_*函数。在最新版本(7)中,它已从语言中删除。而是升级到mysqliPDO:

为什么我不应该在PHP中使用mysql_*函数?。

其次,您应该防止代码中存在SQL注入漏洞:

如何防止PHP中的SQL注入?

第三,当你的应用程序中有一个安全漏洞时,你应该散列你的密码,以防止它们泄露。

password_hash()

最后来谈谈你的问题:如果你不把你的表格告诉POST,它会提出GET请求。将表单更改为<form role="form" action="Create_Account.php" method="post">

对于这样的问题,您可以也应该自己调试代码。最简单的形式是在变量上使用var_dump();来查看它们是否包含您认为包含的内容。您还可以通过打开您喜爱的浏览器的开发工具来检查正在发出的请求。

您的代码可以/应该重写为:

<?php
$dbConnection = new PDO('mysql:dbname=Island_Web_Design;host=localhost;charset=utf8', 'createaccount', '******');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbConnection->prepare('INSERT INTO customer_accounts (firstname, lastname, email, password) VALUES (:firstname, :lastname, :email, :password');
try {
    $stmt->execute([
        'firstname'=> $_POST[firstname],
        'lastname' => $_POST[lastname],
        'email' => $_POST[email],
        'password' => password_hash($_POST[password], PASSWORD_DEFAULT, ['cost' => 14]),
    ]);
} catch('PDOException $e) {
    die('Error: ' . $e->getMessage());
}
echo "1 record added";

请永远不要这样做:

('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[password]')

阅读以下内容:POST是否可以进行SQL注入?

修正了在表单应答器内添加方法="post",感谢@jay