即使我的连接文件有效,phpmyadmin上的数据库也不会保存数据

时间:2017-03-12 20:10:30

标签: php mysql

这是我的连接文件,它没有错误,但似乎没有连接到我的数据库。

<?php
$username="root";$password="Abubba21";$database="posts";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
mysql_close();
?>

这是我的代码,我甚至必须将php标题放在文件的底部,因为否则不会显示任何内容

<body>

<?php

 $db_host = "localhost";
 $db_username = "root"
 $db_pass = "Abubba21";
 $db_name = "accounts";

 try
 {
    $db = new PDO('mysql:host='.$db_host.';dbname'.$db_name,$db_username,$db_pass);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    //$result = $db->query
 }
 catch(PDOException $e)
 {
    echo "Server error: ",$e->getCode()."invalid server";
 }
 $reg = @$_POST['reg'];

 $un = "";
 $pswd = "";
 $em = "";
 $em2 = "";
 $pswd2 = "";

 $un = strip_tags(@$_POST['username']);
 $pswd = strip_tags(@$_POST['password']);
 $em = strip_tags(@$_POST['email']);
 $pswd2 = strip_tags(@$_POST['password2']);
 $em2 = strip_tags(@$_POST['email2']);

 if($reg){
    if($em == $em2){

        $u_check = mysql_query("SELECT username FROM accounts WHERE username = '$un'");
        $check = mysql_num_rows($u_check);
        if($check==0){
            if($un&&$pswd&&$em&&$pswd2&&$em2){
                if($pswd == $pswd2){

                if(strlen($un)>25)
                {
                    echo "The maximum limit for the username is 25 characters.";

                }
                else
                {
                    if(strlen($pswd)>30||strlen($pswd)<5){
                    echo "Your password has to be between 5 and 30 characters.";
                }
                else
                {
                    $pswd = md5($pswd);
                    $pswd2 = md5($pswd2);
                    $query = mysql_query("INSERT INTO accounts VALUES ('','un','pswd','em')");
                    die("<h2>Welcome</h2> Log in to start");
                }   
                }
                }
                else
                {
                    echo "Your passwords don't match";
                }
                }
                else
                {
                    echo "You must fill in all fields";
                }
                }
                else
                {
                    echo "Username is already token";
                }
                }
                else
                {
                    echo "Your emails don't match";
                }
            }

?>



    <div class="body">


        <div class="register">
            <h1 class="heads">Register</h1><br><br><br><br>

            <form action="#" method="post">
                <div class="login">
                    <input type="text" name="username" value="" placeholder="Username ...">
                    <p />
                    <input type="password" name="password" value="" placeholder="Password ...">
                    <p />
                    <input type="password" name="password2" value="" placeholder="Password ...">
                    <p />
                    <input type="email" name="email" value="" placeholder="someone@somesite.com">
                    <p />
                    <input type="email" name="email2" value="" placeholder="someone@somesite.com">
                    <p />
                    <input type="submit" name="createaccount" value="Create Account">
                </div>
            </form>
        </div class="register">
        <div class="grad"></div>
        <div class="header">
            <div><span>Welcome</span></div> <br><br>
        </div>
        <br>
        <form action="home.php" method="post">
            <div class="login">
                <input type="text" name="username" value="" size="30" placeholder="username"><br>
                <input type="password" name="password" value="" size="30" placeholder="password">
                <input type="submit" name="login" value="Login"><br>

            </div>
        </form>
    </div>

    <?php include("connect.php"); ?>

2 个答案:

答案 0 :(得分:0)

我相信您的查询字符串不正确:

$query = mysql_query("INSERT INTO accounts VALUES ('','un','pswd','em')");

要:

$query = mysql_query("INSERT INTO accounts (\`my_column_name\`,\`another_column\`,\`etc\`,\`etc2\`) VALUES ('','un','pswd','em')");

答案 1 :(得分:0)

示例代码使用mysql_query函数,但连接是PDO。那是开始工作。 not 混合来自数据库扩展的函数调用。选择一个

使用PHP,有三种常用的数据库接口扩展:

  • mysql_ do 将此用作新代码

  • 库MySQLi

  • PDO_ - 支持MySQL和其他数据库

请参阅此处的通知:

http://php.net/manual/en/function.mysql-query.php

  

警告此扩展在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。相反,应该使用MySQLi或PDO_MySQL扩展。另请参阅MySQL:选择API指南和相关的常见问题解答以获取更多信息。该功能的替代方案包括:   
。 mysqli_query()   
。 PDO ::查询()

http://php.net/manual/en/mysqlinfo.api.choosing.php

混合来自数据库界面扩展的函数调用。这不会起作用。

MD5哈希不适合密码。 http://php.net/manual/en/faq.passwords.php