0

This is my index.html file

<form method="post" id="form" action="join.php">
<ul>
    <li><input id="name" name="name" type="text" /></li>
    <li><input id="username" name="username" type="text" /></li>
    <li><input id="password" name="password" type="password" /></li>
    <li>
        <select id="gender" name="gender"> 
            <option value="">Gender</option>
            <option value="1">Male</option>
            <option value="2">Female</option>
        </select>
    </li>
</ul>
<div>
    <input type="submit" value="Submit" class="submit"/>
    <span class="error" style="display:none"> Please Enter Valid Data</span>
    <span class="success" style="display:none"> Registration Successfully</span>
</div>
</form>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</script>
<script type="text/javascript" >
$(function() {
    $(".submit").click(function() {
        var name = $("#name").val();
        var username = $("#username").val();
        var password = $("#password").val();
        var gender = $("#gender").val();
        var dataString = 'name='+ name + '&username=' + username + '&password=' + password + '&gender=' + gender;

        if(name=='' || username=='' || password=='' || gender=='') {
            $('.success').fadeOut(200).hide();
            $('.error').fadeOut(200).show();
        } else {
            $.ajax({
                type: "POST",
                url: "join.php",
                data: dataString,
                success: function(){
                $('.success').fadeIn(200).show();
                $('.error').fadeOut(200).hide();
                }
            });
        }
        return false;
    });
});
</script>

And this is join.php

<?php
$conn = mysql_connect('localhost','root','') or die (mysql_error);
$db=mysql_select_db('test', $conn) or die (mysql_error);
if($_POST) {
    echo"processing...........";
    $name=$_POST['name'];
    $username=$_POST['username'];
    $password=$_POST['password'];
    $gender=$_POST['gender'];
    mysql_query("INSERT INTO user1 (name,username,password) VALUES('$name','$username','$password')");
} else {
    echo"unsuccessful";
}
?>

I want to store the values of name,username and password in the database but it is getting stored only for the first attempt. When I submit it the second time nothing happens. why is that?

Jadeye
  • 3,551
  • 4
  • 47
  • 63
Sneha P
  • 31
  • 1
  • 5
  • It is not clear what do you mean by submitting for the second time? Do you mean submitting the same form details again?are there any console log errors? are there any php errors? can you echo the mysql_error() output? – liorsolomon Feb 26 '17 at 17:02
  • If it's the same details you are submitting you could have a unique column in the table which is preventing the same details from being inserted. – Nick Duncan Feb 26 '17 at 17:41
  • it works fine for the first set of inputs but when i try it the second time with new values it is not getting stored in the database. i have to refresh the page again. – Sneha P Feb 26 '17 at 18:40
  • Thank you soo much Nick Duncan it worked!!. removed the unique cloumn. – Sneha P Feb 26 '17 at 19:20
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 27 '17 at 17:03
  • **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Feb 27 '17 at 17:03
  • Please do not use this code in a production environment. It is dangerous and will get hacked. – Jay Blanchard Feb 27 '17 at 17:03
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Feb 27 '17 at 17:04

0 Answers0