PHP ERROR缺少参数

时间:2012-09-07 05:21:28

标签: php ios json ios5

我正在按照本教程进行登录和注册http://www.raywenderlich.com/13511/how-to-create-an-app-like-instagram-with-a-web-service-backend-part-12。当我点击登录或注册时,我收到此错误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"} 

这是我的php代码:

<?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();
?>

和api.php代码:

<?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');
    }

}
?>

我需要帮助...

4 个答案:

答案 0 :(得分:4)

此:

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

几乎肯定是错的。试试这个:

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

并同时修复对register()的调用。

答案 1 :(得分:1)

更改以下块

   case "login":

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

        break;

    case "register":

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

        break;

您在函数调用中使用了&&。请改用,

答案 2 :(得分:1)

该登录功能需要两个参数。但是你只通过一个......

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

这应该有用..

答案 3 :(得分:0)

你错过了这个:

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