Reputation: 49
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;
}
}
?>
Upvotes: 0
Views: 1838
Reputation: 1175
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 );
Upvotes: 1
Reputation: 28529
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);
Upvotes: 0
Reputation: 738
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);
Upvotes: 0
Reputation: 766
$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));
Upvotes: 0
Reputation: 3900
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);
Upvotes: 0