将Web表单连接到MySQL数据库

时间:2017-03-30 06:53:42

标签: php html sql sql-server forms

使用MySQL数据库,我将用户添加到数据库中,就像这个测试数据一样:

INSERT INTO `database_users`.`Members` (`ID`, `Username`, `Password`,
 `First Name`, `Last Name`, `Email Address`, `Telephone Number`,
 `Address Line 1`, `Address Line 2`, `Town/City`, `Postcode`,
 `Mailing-list`, `Terms`) VALUES (NULL, 'Test', '', '', '', '', '', '',
 NULL, '', '', NULL, '');"

我的HTML表单如下所示:

<input type="submit" value="Join!">
    <div class="col-lg-6">
        <input type="text" placeholder="Town/City" id="Town/City">
    </div>
    <div class="col-lg-6">
        <input type="text" placeholder="Postcode" id="Postcode">
    </div>
</input>

我希望在我的网站上提交表单并将数据上传到我的MySQL数据库。任何人都可以就我在表格中遗漏的内容提出建议吗?

1 个答案:

答案 0 :(得分:0)

您的SQL查询似乎很好,但如果您不知道如何使用PHP,则会经常被阻止。

最好的办法是学习基本的PHP知道如何实现自己的注册页面,并在开发过程中遇到问题时询问stackoverflow。

http://www.learn-php.org/

您的表单不正确:您需要<form>输入并在其上定义方法(发布或获取)和操作(数据所在的文件)。

基本上,您的代码必须像:

HTML(form.html)

<form method="POST" action="save.php">
    <input type="text" name="pseudo">
    <input type="submit">
</form>

PHP(save.php)

<?php
    if(isset($_POST['pseudo'])){ // Test if the variable exists
        $pseudo = $_POST['pseudo'];
        // Your SQL query here which save the pseudo. Don't forget to test your variables.
    }
    else {
        echo "pseudo is required";
    }
?>

不要忘记转义变量并使用预准备语句。否则,您的网站将存在许多缺陷,如XSS或SQL注入。

部分链接

使用Mysqli准备好的声明

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

防止XSS攻击

https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet

什么是SQL注入?

http://php.net/manual/en/security.database.sql-injection.php