Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试用php连接数据库_Php_Mysql - Fatal编程技术网

尝试用php连接数据库

尝试用php连接数据库,php,mysql,Php,Mysql,我正在做一个注册页面,我在xampp上设置了phpmyadmin,我的apache端口是8080和 我的表名为registration,数据库名为loginregister, 无论何时我提交它,值都不会传输到表中,并且我没有看到任何错误,帮助 这是我的代码: <!DOCTYPE html> <html> <head> <title>Register</title> </head> <body> &

我正在做一个注册页面,我在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>

我完全忘了解释我的答案。我的错

在这段代码中,您没有向数据库发布任何内容。您所做的只是创建一个与mysqli_connect的连接

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

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();
}

您甚至没有尝试向数据库中插入任何内容。您实际上丢失了执行此操作所需的几乎所有代码。你觉得这怎么行?那我怎么办?我使用$res=mysqli_queryINSERT登录注册表fname、lname、pw、email、uname值“$\u POST[$fname]”、“$\u POST[$lname]”、“$\u POST[$pw]”、“$\u POST[$email]”、“$\u POST[$uname]”;在您实际尝试执行该查询之前,您应该阅读sql注入……您的问题没有显示mysqli_查询。此外,您从未检查您的连接是否已建立。@moran在您的代码示例中没有mysqli\u查询。检查您的代码,感谢您的有用评论John。我完全忘了详细说明我的答案。现在你的答案是值得的。现在让我们希望它对莫兰有用!谢谢工作完美无瑕,虽然第69行的密码没有定义,所以我猜你是指$u POST['pw']?如果是的话,那就是我把它改成的,但是它显示pw为0,所以我不确定这是否应该是那样的是的,确实,密码后字段没有设置。