0

i have a validation checker that checks the database to see if the "email and username" is being used and if so then echo the email and username as "false". And if its not in the database then echo to "true". I am returning the results of the users email and username being in the sql sever. So i want to append "$json1 or $json2" to the "$first or $second" like this

 {"Email":false, "Username":true};

PHP Code:

<?php

if(isset($_GET['submit'])){

    //Connect to database
    $link = mysqli_connect("localhost", "root", "");
    mysqli_select_db($link, "magicsever");

    if(mysqli_connect_error()){

        die ("Database connection error");
    }

    //Find the email in the database
    $query = "SELECT * FROM app_signup WHERE email = '".mysqli_real_escape_string($link, $_GET['email'])."'";
    $query2 = "SELECT * FROM app_signup WHERE username = '".mysqli_real_escape_string($link, $_GET['username'])."'";

    $result = mysqli_query($link, $query);
    $result2 = mysqli_query($link, $query2);

    $email_checker1 = json_encode(array("Email"=>false));
    $email_checker2 = json_encode(array("Email"=>true));

    $username_checker1 = json_encode(array("Username"=>false));
    $username_checker2 = json_encode(array("Username"=>true));

    if(mysqli_num_rows($result)>0){

        echo $email_checker1;


    }

}
?>

5 Answers 5

1

make new array for email and username results

//Find the email in the database
$query = "SELECT * FROM app_signup WHERE email = '".mysqli_real_escape_string($link, $_GET['email'])."'";
$query2 = "SELECT * FROM app_signup WHERE username = '".mysqli_real_escape_string($link, $_GET['username'])."'";

$result = mysqli_query($link, $query);
$result2 = mysqli_query($link, $query2);

$dataResult = array();
$dataResult['Email'] = (mysqli_num_rows($result)>0)? false: true;
$dataResult['Username'] = (mysqli_num_rows($result2)>0)? false: true;

echo json_encode($dataResult );
Sign up to request clarification or add additional context in comments.

14 Comments

I got an error on the $dataResult = new array(); line.
or just use $dataResult = [];
or just remove the new in array. sorry. wrong syntax
D: i am returning the results of the users email and username being in the sql sever. So the set booleans wont be the result
but what if there's no result?
|
0

JSON is only a data format, a way to present a data structure. Until you have a data structure that is ready to be presented, you don't want to convert it to JSON.

What you should do is to build an array containing the validation errors:

// I'm assuming that the true/false means that there was an error with validating that value
// You can initialize the validation array with false values for all fields and change them to true in case of validation error
$errors = ['Email' => false, 'Username' => false];

// Then, change the values in case of an error
if (... /* invalid email */) {
    $errors[] = ['Email' => true];
}
if (... /* invalid username */) {
    $errors[] = ['Username' => true];
}

Then convert the array to JSON only when you display it:

echo json_encode($errors);

3 Comments

The true means "the email and username that was inputted is available to take" and false means " its has already been taken"
That doesn't change my answer in any way. But perhaps you'd like to write your code in a descriptive way, so that you don't have to explain what you just did with an extra comment. How about naming the keys in the array email_available and username_available?
i see what you do, but i want them in the same curly brackets not like separated.
0
 $first = json_encode(array("Email"=>false));
    $second = json_encode(array("Email"=>true));

    $json1 = json_encode(array("Username"=>false));
    $json2 = json_encode(array("Username"=>true));

use like this

$first = json_encode(array("Email"=>false)+array("Username"=>true));
$second = json_encode(array("Email"=>true)+array("Username"=>false));

1 Comment

Close the way i structure the code is i check if the email is taken first and if it is, then check if the username is taken and then from the username side i append the message from username to the email in the first if statement. So your way kinda works
0

If you really want merge them together go through this references

Good approach would be like

$resData1['email']=false;
$resData2['email']=true;

$resData1['Username']=false;
$resData2['Username']=true;

$json1 = json_encode($resData1);
$json2 = json_encode($resData2);

3 Comments

Hmm the problem with that is i am getting the same result "false and false" or "true and true" back. Is it possible to change like append he false and true with different variable names?
because the code is written like that.... you might need to use an if condition..like if email return as false set to false or give true. Something like that will works
Yea i thought of that but i would have to create and if statement for different scenarios and i am trying to find a less crazier way of doing it.
0

Why not do json_encode after the array append.

$first = json_encode(array("Email"=>false, "Username"=>false));

added.

$data['Email'] = false;
if('''')
{
  $data['Username'] = false;
}
$result = json_encode($data);

5 Comments

This would work if the username was true and email is false. I dont want a simply echo where i type it, i want to append if either or happens.
So you created a data varible and passed "email" to it and you set it to false? but the problem is i am returning the results of the users email and username being in the sql sever.
You just store the json string to your sql server, it have no difference which variable store this value.
its not being stored in the sql server. I am taking it info from the server then sending the json file to an app that reads the information and displays something to the user. Sorry if i seem "dumb" in programming D: i am still learning
$variable just store the value, what the variable name is have no difference.

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.