插入的数据未保存在phpmyadmin数据库中

时间:2014-11-30 10:57:01

标签: php mysql

我是php新手。我想在php mysql中登录和注册。为此,我创建一个简单的表单,并测试数据是否插入phpmyadmin。这些值不会保存在db中。我不知道错误在哪里。 我已经在下面添加了编码

MY Db连接:

<?php

  $connect = mysql_connect("localhost", "root", "") or die("Unable to connect database".mysql_error());

  mysql_select_db("logindb", $connect) or die ("sorry db connection fail");
?>

我的注册表格:

<?php
include("config.php");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login screen</title>
    <style type="text/css">
    .register-form{
        width: 500px;
        margin: 0 auto;
        text-align: center;
        padding: 10px;
        padding: 10px;
        background : #c4c4c4;
        border-radius: 10px;
        -webkit-border-radius:10px;
        -moz-border-radius:10px;
    }
    .register-form form input{padding: 5px;}
    .register-form .btn{
        background: #726E6E;
        padding: 7px;
        border-radius: 5px;
        text-decoration: none;
        width: 50px;
        display: inline-block;
        color: #FFF;
    }
    .register-form .register{
        border: 0;
        width: 60px;
        padding: 8px;
    }
    </style>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$name=mysql_real_escape_string($_POST['username']);
$email=mysql_real_escape_string($_POST['email']);
$password=mysql_real_escape_string($_POST['password']);
$cpassword=mysql_real_escape_string($_POST['confirmpassword']);
$query = mysql_query("inser into register values('', '$name', '$email', '$password',   '$cpassword')");
if($query)
{
    header("location:success.php");
}
}

?>
    <div class="register-form">
        <h1>Register</h1>
        <form action="" method="post">
            <p><label>User Name: </label>
            <input type="text" id="username" name="username" placeholder="Username">
            </p>
            <p><label>E-Mail&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :</label>
            <input id="email" name="email" type="email">
            </p>
            <p><label>Password&nbsp;&nbsp; :</label>
            <input id="password" name="password" type="password">
            </p>
            <p><label>confirm Password &nbsp;&nbsp; :</label>
            <input id="confirmpassword" name="confirmpassword" type="password">
            </p>
            <input type="submit" name="submit" value="register" class="btn register">
            <a class="btn" href="login.php">Login</a>
        </form>
    </div>
</body>
</html>

在创建新页面成功后,我重定向到success.php

<!DOCTYPE html>
<html>
<head>
    <title>success full</title>
</head>
<body>
    <h1>Successfully registered</h1>
</body>
</html>

输入数据后,我点击提交。我是回声并看到了SQL查询,我正在获得价值,但它没有保存在phpmyadmin数据库中。我是php的新手,请帮我开发更多的php。 我再次感谢你。

3 个答案:

答案 0 :(得分:1)

除非必要,否则请在任何HTML之前保留PHP。另外,在查询中指定列,因为空字符串可能导致查询失败 您真的需要将确认密码存储在数据库中吗?我不相信你这样做,并确保你哈希。确认数据库中的所有列都与您要插入的内容相匹配。

<?php
include("config.php");

if(isset($_POST['submit'])) {
    $name=mysql_real_escape_string($_POST['username']);
    $email=mysql_real_escape_string($_POST['email']);
    $password=mysql_real_escape_string($_POST['password']);
    $cpassword=mysql_real_escape_string($_POST['confirmpassword']);
    $query = mysql_query("INSERT INTO register
                          (name_column,email_column,password_column)
                          VALUES('".$name."', '".$email."', '".$password."')");
    if($query) {
        header("location:success.php");
    }

} else {
    echo "no form data received.";
}

?>
<!DOCTYPE html>
//HTML HERE//

答案 1 :(得分:0)

更改

$query = mysql_query("insert into register values('', '$name', '$email', '$password',   '$cpassword')");

// Specify the last parameter to the DB NAME!
$connect = mysql_connect("localhost", "root", "logindb")

删除

mysql_select_db("logindb", $connect) or die ("sorry db connection fail");

答案 2 :(得分:0)

insert into register values('', '".$name."', '".$email."', '".$password."','".$cpassword."')