Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

php - Inserted data was not saving in phpmyadmin database

i am new to php. I am trying to do login and registration in php mysql. for that i create one simple form and tested whethet the data was inserted in phpmyadmin or not. The values are not saving in db. I dont know where is the error. I have include the coding below

MY Db connection:

<?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");
?>

My Registratiion form:

<?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>

After success in create a new page i redirect to success.php

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

After i enter data, i click submit. i was echo and saw the sql queries, i am getting value but its not saving in phpmyadmin database. I am new to php, please help me to develop more php. Once again i am thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Keep your PHP before any HTML unless it's necessary. Also, specify the columns in your query because a blank string may cause the query to fail.
Do you really need to store the confirmation password in the database? I don't believe you do, and make sure you hash it. Confirm all your columns in the database are matching up with what you're inserting.

<?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//

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...