试图用PHP连接数据库

时间:2017-01-06 13:17:52

标签: php mysql

我正在制作注册页面,我在xampp上设置了phpmyadmin,我的apache的端口是8080并且 我的表名是registration,我的数据库的名称是loginregister, 无论何时我提交,价值都没有传到桌面,我没有看到任何错误,有帮助吗?

这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
</head>
<body>
    <form action="" method="post" name="form1">
    <table>
        <tr>
            <td>Enter your first name*</td>
            <td><input type="text" name="fname" required="yes" pattern="^[a-z1-9]+"></td>
            <td><p>Username takes only small letters or numbers, no capital letters</p></td>
        </tr>
        <tr>
            <td>Enter your last name*</td>
            <td><input type="text" name="lname" required="yes"></td>
        </tr>
        <tr>
            <td>Enter your password*</td>
            <td><input type="password" name="pw" required="yes"></td>
        </tr>
        <tr>
            <td>Enter your email adress*</td>
            <td><input type="email" name="email" required="yes"></td>
        </tr>
        <tr>
            <td>Enter your username*</td>
            <td><input type="text" name="uname" required="yes"></td>
        </tr>
        <tr>
            <td><input type="submit" value="submit" name="submit1"></input></td>
        </tr>
    </table>
    </form>
    <?php
        if(isset($_POST['submit1']))
        {
        $link=mysqli_connect('localhost','root','' , "loginregister");
        $res= "INSERT INTO loginregister (fname , lname , pw , email , uname) VALUES('$_POST[$fname]','$_POST[$lname]','$_POST[$pw]','$_POST[$email]','$_POST[$uname]'))";
        }
    ?>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

我完全忘了解释我的答案。我的坏!

在您的代码中,您没有向数据库发布任何内容。您所做的只是与if(isset($_POST['submit1'])) { /** * Connect to your database. */ try { $conn = new PDO('mysql:dbname=loginregister;host=localhost', 'root', ''); } catch (PDOException $e) { /** * Catch any exceptions in case your connection should fail. */ echo 'Failed to connect: '.$e->getMessage(); die(); } /** * Here is where you prepare your query. * This is what you did in your piece of code, but never executed. */ $stmt = $conn->prepare('INSERT INTO `registration` (`fname`, `lname`, `pw`, `email`, `uname`) VALUES (:fname, :lname, :pw, :email, :uname)'); /** * Your passwords should not be stored in plain-text. Ever. * And as John Conde pointed out, password_hash is the better way to do this. */ $password = password_hash($_POST['pw'], PASSWORD_DEFAULT); /** * Here we assign all variables to the query. */ $stmt->bindValue(':fname', $_POST['fname'], PDO::PARAM_STR); $stmt->bindValue(':lname', $_POST['lname'], PDO::PARAM_STR); $stmt->bindValue(':pw', $password, PDO::PARAM_STR); $stmt->bindValue(':email', $_POST['email'], PDO::PARAM_STR); $stmt->bindValue(':uname', $_POST['uname'], PDO::PARAM_STR); /** * And here is where we tell the PDO statement execute your query. */ $stmt->execute(); } 建立连接。

这是一段使用PDO安全地将注册用户保存到数据库的代码。

system service