Eric Oboite Eric Oboite - 1 year ago 116
JSON Question

PHP ERROR missing argument

I'm following this tutorial for the login and registration . 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:


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

switch ($_POST['command']) {

case "login":

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


case "register":

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


case "upload":

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


and api.php code:

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

//Returning erros back to user

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

//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'] ) {

login($user, $pass);

} else {
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) {
$_SESSION['IdUser'] = $result['result'][0]['IdUser'];
print json_encode($result);

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


I need help please...

Answer Source


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

is almost certainly wrong. Try this:

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

And fix the call to register() as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download