Eric Oboite Eric Oboite - 5 months ago 8
JSON Question

PHP ERROR missing argument

I'm following this tutorial for the login and registration http://www.raywenderlich.com/13511/how-to-create-an-app-like-instagram-with-a-web-service-backend-part-12 . When I click login or registration I receive this error php

<br />
<b>Warning</b>: Missing argument 2 for login(), called in /Applications/XAMPP/xamppfiles/htdocs/iReporter/index.php on line 13 and defined in <b>/Applications/XAMPP/xamppfiles/htdocs/iReporter/api.php</b> on line <b>36</b><br />
{"error":"Wrong username or password"}


Here is my php code:

<?php
session_start();
require("lib.php");
require("api.php");

header("Content-Type: application/json");

//API
switch ($_POST['command']) {

case "login":

login($_POST['username'] && $_POST['password']);

break;

case "register":

register($_POST['username'] && $_POST['password']);

break;

case "upload":

upload($_SESSION['IdUser'], $_FILES['file'], $_POST['title']);

break;
}
exit();
?>


and api.php code:

<?php
//API implementation to come here
function errorJSON($msg){

//Returning erros back to user

print json_encode(array('error' => $msg));
exit();
}

//Register function
function register($user, $pass){
//checking if username exists
$login = query("SELECT username FROM login WHERE username='%s' limit 1", $user);

if (count($login['result']) >0) {
errorJSON('Username already exists');
}

//Registering user
$result = query("INSERT INTO login(username, pass) VALUES('%s', '%s')", $user, $pass);

if (!$result['error'] ) {

//Success
login($user, $pass);

} else {
//error
errorJSON('Sorry, something went wrong');

}

}

function login($user, $password){
$result = query("SELECT IdUser, username FROM login WHERE username = '%s' AND pass = '%s' limit 1 ", $user, $pass);

if (count($result['result']) > 0) {
//Authorized
$_SESSION['IdUser'] = $result['result'][0]['IdUser'];
print json_encode($result);

} else {
//Not authorized
errorJSON('Wrong username or password');
}

}
?>


I need help please...

Answer

This:

login($_POST['username'] && $_POST['password']);

is almost certainly wrong. Try this:

login($_POST['username'], $_POST['password']);

And fix the call to register() as well.