0

Looking for a best solution:

$.getJSON("InsertData.php", {fullName:val1, course_id:course_id, occupation:val2}, function(data) {
        $.each(data, function(i, user) {
            //alert(user.aryA.status);
            if(user.aryA.status == 'true'){
                currentPosition = 2;
                checkData();
                nextSlide();
            }else{
                 nextSlide();
            }
        });

    })

Here is php code:

    mysql_select_db("db", $con);

    $Query="SELECT * from table WHERE fullName='".$fullName."' and course_id='".$cid."'";
    $result = mysql_query($Query);
    $totalRecords = mysql_num_rows($result);
    if($totalRecords) {
        while ($row = mysql_fetch_array($result)) {
                $returnData[]=array(    //for Json data array
            'userName' => $row['fullName'],
            'aryA' => array(
                'status' => $row['status']
                )
            );
        }

    }

    if(!$totalRecords) {
        $insertQuery="INSERT INTO table (fullName,course_id,occupation) VALUES ('".addslashes($fullName)."','".addslashes($cid)."','".addslashes($d3)."')";
        $result1 = mysql_query($insertQuery);

    }else{
        if($stat == "true"){$value = 1;}
        }

mysql_close($con);

echo json_encode($returnData);

So In first case when I hit the php through jquery it saves data in database but give me error or length. Because $returnData is empty. Is there a way if $totalRecords is false, how to send json_encode to say there is no data or any value through json_encode to my jQuery function.

Thanks in advance.

3
  • 5
    You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. Commented Apr 25, 2013 at 14:42
  • 1
    Please, please please... for the umptieth time: stop using mysql_*. It's unsafe and being deprecated, and will be removed from PHP altogether in time. use mysqli_* or PDO Commented Apr 25, 2013 at 14:42
  • Just add $returnData = array() at the top of your code. This will be encoded as '[]'. When $.each is passed an empty array, it will just do nothing. Commented Apr 25, 2013 at 14:47

2 Answers 2

1

Just setup an else statement, and add a 'success' key to your array:

if($totalRecords){
    while ($row = mysql_fetch_array($result)) {
            $returnData[]=array(    //for Json data array
        'success'=>'true',
        'userName' => $row['fullName'],
        'aryA' => array(
            'status' => $row['status']
            )
        );
    }
}else{
    $returnData = array('success'=>'false');
}

Then check the value of 'success' in your jQuery.

Also, you really shouldn't be using mysql_*.

Sign up to request clarification or add additional context in comments.

1 Comment

Just be sure to use the bind_param feature or you'll be missing the point. SQL injection bugs are no joke.
0
$returnData = array(); //add this
 $totalRecords = mysql_num_rows($result);
if($totalRecords) {
while ($row = mysql_fetch_array($result)) {
        $returnData[]=array(    //for Json data array
    'userName' => $row['fullName'],
    'aryA' => array(
        'status' => $row['status']
        )
    );
}

}
else
{
$returnData[] = 'no Record'; //add this
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.