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;


    }

}
?>
Jon2713
  • 41
  • 1
  • 12

5 Answers5

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 );
Chinito
  • 1,085
  • 1
  • 9
  • 11
  • I got an error on the $dataResult = new array(); line. – Jon2713 May 18 '17 at 06:30
  • or just use `$dataResult = [];` – Chinito May 18 '17 at 06:30
  • or just remove the new in array. sorry. wrong syntax – Chinito May 18 '17 at 06:33
  • 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 – Jon2713 May 18 '17 at 06:34
  • but what if there's no result? – Chinito May 18 '17 at 06:36
  • There will be because, if the email is in the database then i return "Email: false" and if its not there then i return "Email: true". And now i want to append this result to the username second where if its there i return "Username: false" etc. – Jon2713 May 18 '17 at 06:38
  • `{"Email":false, "Username":true};` this is what you want right? or `{"Email":"SampleEmail", "Username":"SampleUsername"};` this? – Chinito May 18 '17 at 06:41
  • {"Email":false, "Username":true}; because it will return the true and false which the app creator can read and tell the user if the username or email is taken or not. – Jon2713 May 18 '17 at 06:44
  • then my answer is correct. `(mysqli_num_rows($result)>0)? false: true` it will return false if the result is greater than 0 rows, else true. – Chinito May 18 '17 at 06:46
  • OHH i do apologize then, can you please, if you can, go into more detail about each line of code you beginning with $dataResult. Sorry i am new to this backend stuff :D – Jon2713 May 18 '17 at 06:50
  • So what you did was set an empty array for the results then you said "if the email is in the rows set it to false OR true" and if "username is in he rows set to false or true" then print the results and it will put it in the same curly brackets? – Jon2713 May 18 '17 at 06:53
  • yes. `(mysqli_num_rows($result)>0)? false: true;` this one is called ternary operators (?:) `(mysqli_num_rows($result)>0)? 'indicate return value here if true' : 'else false';` – Chinito May 18 '17 at 06:55
  • oh but how did it know i wanted the result structured like i have there. Is it the ternary operators that automatically put it in the json format? – Jon2713 May 18 '17 at 07:03
  • no. this code `json_encode($dataResult );` is used to set your array in a json format. that ternary operator is an if else statement – Chinito May 18 '17 at 07:06
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);
Bartosz Zasada
  • 3,762
  • 2
  • 19
  • 25
  • The true means "the email and username that was inputted is available to take" and false means " its has already been taken" – Jon2713 May 18 '17 at 06:23
  • 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`? – Bartosz Zasada May 18 '17 at 06:26
  • i see what you do, but i want them in the same curly brackets not like separated. – Jon2713 May 18 '17 at 06:39
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);
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 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. – Jon2713 May 18 '17 at 06:24
  • 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. – Jon2713 May 18 '17 at 06:36
  • You just store the json string to your sql server, it have no difference which variable store this value. – LF00 May 18 '17 at 06:39
  • 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 – Jon2713 May 18 '17 at 06:41
  • $variable just store the value, what the variable name is have no difference. – LF00 May 18 '17 at 06:44
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));
Amit Gaud
  • 756
  • 6
  • 15
  • 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 – Jon2713 May 18 '17 at 06:25
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);
Community
  • 1
  • 1
Kuru
  • 738
  • 5
  • 18
  • 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? – Jon2713 May 18 '17 at 06:27
  • 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 – Kuru May 18 '17 at 06:44
  • 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. – Jon2713 May 18 '17 at 06:47