1

the problem is..i try to parsing json from php..here is jquery code:

$.post( "confirmsignup.php", $("#signupform").serialize()).always(function( data ) {
    alert(data.msg);
}, "json");

PHP code:

if (isset($_POST['gender'])&&isset($_POST['fname'])&&isset($_POST['sname'])&&isset($_POST['username'])&&isset($_POST['dob'])) {
    $gender=secureing($_POST['gender']);
    $fname=secureing($_POST['fname']);
    $sname=secureing($_POST['sname']);
    $username=secureing($_POST['username']);
    $email=secureing($_POST['email']);
    $dob=secureing($_POST['dob']);
    if (isset($_POST['agree'])&&isset($_POST['pass'])&&isset($_POST['repass'])) {
        $pass=secureing($_POST['pass']);
        if ($_POST['pass']==secureing($_POST['repass'])) {
            $query = "INSERT INTO users VALUES('$username', '$gender', '$fname', '$sname', '$email', '".md5($pass)."', '$dob')";
            if(!($query_run = mysql_query($query))){
                $msg = "error";
            }else{
                $msg = "complete";
            }
        }
    }
}
header('Content-Type: application/json');
?>
{
"msg": "<?php echo $msg ." - ". $query; ?>"
}

secureing() is for returning string after escape_string.. $msg suppose to return string "complete"... but it returning "error".. however in phpmyadmin, query is successfully executed..but something not right at userdob

i think there is no mistake..what is my mistake?please help..

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
Fadhil Ahmad
  • 1,097
  • 11
  • 19
  • 1
    why aren't you checking for the real error? – Funk Forty Niner May 13 '16 at 12:06
  • You have not return or echo anything from your php script to pass in into your js file!! – Saty May 13 '16 at 12:08
  • 3
    1. Don't build json manually, use `json_encode()`, 2. don't use the deprecated `mysql_*` functions, 3. NEVER EVER use `md5()` to hash a password. – jeroen May 13 '16 at 12:09
  • i did echo $msg at the last line and it return to alert message on jquery.post().done() callback...and ive try to echo mysql_error but it return nothing... – Fadhil Ahmad May 13 '16 at 12:19
  • @jeroen thanks for the advice...but why i cant use md5 to hash password? – Fadhil Ahmad May 13 '16 at 12:22
  • It's easy to retrieve on a modern computer using brute force. See also http://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords – jeroen May 13 '16 at 12:33
  • @jeroen what did you mean by "deprecated mysql_*" – Fadhil Ahmad May 13 '16 at 12:34
  • Read this post on [why you shouldn't use mysql_* functions in PHP](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). Use [MySQLi](http://php.net/manual/en/book.mysqli.php) (or better [PDO](http://php.net/manual/en/book.pdo.php)) instead. – Mikey May 13 '16 at 12:42

4 Answers4

0

Can you please change the following code with your code, I have used mysql_insert_id to check record is inserted or not.

$query = "INSERT INTO users VALUES('$username', '$gender', '$fname', '$sname', '$email', '".md5($pass)."', '$dob')";
$query_run = mysql_query($query);
$id        = mysql_insert_id();
if($id > 0)
{
    $msg = "complete";
}
else
{
    $msg = "error";
}

It may help you.

Harsh Sanghani
  • 1,666
  • 1
  • 14
  • 32
0

I have same issue long time ago!

it's because you haven't any AI primary key in the table!

add a field like this to the table: id INT Primary Key Auto Increment. this will works!

and migrate to mysqli or pdo for better security and support! ;)

Pouya Darabi
  • 2,246
  • 18
  • 23
0

try my code is seem you have put wrong condition

if (isset($_POST['gender'])&&isset($_POST['fname'])&&isset($_POST['sname'])&&isset($_POST['username'])&&isset($_POST['dob'])) {
    $gender=secureing($_POST['gender']);
    $fname=secureing($_POST['fname']);
    $sname=secureing($_POST['sname']);
    $username=secureing($_POST['username']);
    $email=secureing($_POST['email']);
    $dob=secureing($_POST['dob']);
    if (isset($_POST['agree'])&&isset($_POST['pass'])&&isset($_POST['repass'])) {
        $pass=secureing($_POST['pass']);
        if ($_POST['pass']==secureing($_POST['repass'])) {
            $query = "INSERT INTO users VALUES('$username', '$gender', '$fname', '$sname', '$email', '".md5($pass)."', '$dob')";
            if(!(mysql_query($query))){
                $msg = "error";
            }else{
                $msg = "complete";
            }
        }
    }
}
0

i just found the solution, after change my php API to connecting to database.
from:

if(!@mysql_connect('localhost', 'root', '')||!@mysql_select_db('knewit')){
    die("something not right");
}

to:

    $mysqli = new mysqli("localhost", "root", "", "knewit");

after that, i change my executing method from mysql_query to $mysqli->query.
i also change my jquery post method from always to done. and the final code be like this.
jQuery script:

$.post( "confirmsignup.php", $("#signupform").serialize()).done(function( data ) {
                alert(data.msg);
            }, "json");

PHP script:

        if (isset($_POST['agree'])&&isset($_POST['pass'])&&isset($_POST['repass'])) {
        $passs=secureing($_POST['pass']);
        if ($_POST['pass']==secureing($_POST['repass'])) {
            $query = "INSERT INTO users VALUES('$username', '$gender', '$fname', '$sname', '$email', '$passs', '$dob')";
            if(!($query_run = $mysqli->query($query))){
                $msg = "error";
            }
            else
            {
                $msg = "complete";
            }
        }
    }
}
header('Content-Type: application/json');
$arrayName = array('msg' => $msg );
echo json_encode($arrayName);

thank you for answer my question. i really appreciate it.

Fadhil Ahmad
  • 1,097
  • 11
  • 19