2016-12-20 5 views
0

以下はPHPプログラムのサンプルコードです。私のデータベースは既に作成されています。php(HTTP POST)を使用してiOSからMySQLデータベースに接続しようとしています

createteam.php:

<?php 
$response = array(); 

if($_SERVER['REQUEST_METHOD']=='POST'){ 

//getting values 
$teamName = $_POST['name']; 
$memberCount = $_POST['member']; 

//including the db operation file 
require_once '../includes/DbOperation.php'; 

$db = new DbOperation(); 

//inserting values 
if($db->createTeam($teamName,$memberCount)){ 
    $response['error']=false; 
    $response['message']='Team added successfully'; 
}else{ 

    $response['error']=true; 
    $response['message']='Could not add team'; 
} 

}else{ 
$response['error']=true; 
$response['message']='You are not authorized'; 
} 
echo json_encode($response); 

のconfig.php:

<?php 

define('DB_USERNAME', 'root'); 
define('DB_PASSWORD', ''); 
define('DB_HOST', 'localhost'); 
define('DB_NAME', 'iphone'); 

DbConnect.php:

<?php 

class DbConnect 
{ 
private $conn; 

function __construct() 
{ 
} 

/** 
* Establishing database connection 
* @return database connection handler 
*/ 
function connect() 
{ 
    require_once 'Config.php'; 

    // Connecting to mysql database 
    $this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME); 

    // Check for database connection error 
    if (mysqli_connect_errno()) { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

    // returing connection resource 
    return $this->conn; 
} 
} 

DbOperation.php:私はChromeでポストマンを使用してHTTP POSTメソッドを使用すると

<?php 

class DbOperation 
{ 
private $conn; 


function __construct() 
{ 
    require_once dirname(__FILE__) . '/Config.php'; 
    require_once dirname(__FILE__) . '/DbConnect.php'; 
    // opening db connection 
    $db = new DbConnect(); 
    $this->conn = $db->connect(); 
} 

//Function to create a new user 
public function createTeam($name, $memberCount) 
{ 
    $stmt = $this->conn->prepare("INSERT INTO team(name, member) values(?, ?)"); 
    $stmt->bind_param("si", $name, $memberCount); 
    $result = $stmt->execute(); 
    $stmt->close(); 
    if ($result) { 
     return true; 
    } else { 
     return false; 
    } 
} 

} 

しかし、これは

この平均値を何

Notice: Undefined index: name in D:\xampp\htdocs\TestServ\api\createteam.php on line 9

Notice: Undefined index: member in D:\xampp\htdocs\TestServ\api\createteam.php on line 10

Warning: mysqli::__construct(): (HY000/1049): Unknown database 'iphone' in D:\xampp\htdocs\TestServ\includes\DbConnect.php on line 20 Failed to connect to MySQL: Unknown database 'iphone' Warning: mysqli::prepare(): Couldn't fetch mysqli in D:\xampp\htdocs\TestServ\includes\DbOperation.php on line 20

Fatal error: Uncaught Error: Call to a member function bind_param() on null in D:\xampp\htdocs\TestServ\includes\DbOperation.php:21 Stack trace: #0 D:\xampp\htdocs\TestServ\api\createteam.php(18): DbOperation->createTeam(NULL, NULL) #1 {main} thrown in D:\xampp\htdocs\TestServ\includes\DbOperation.php on line 21

、と私は何を変更する必要があります述べて?

+0

[PHP: "通知:未定義変数"と "通知:未定義インデックス"]の複製可能性(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined- index)その質問から始めます。 – Ohgodwhy

答えて

0

あなたの最初の2つのエラーが原因でこの2行の原因とされています

$teamName = $_POST['name']; 
$memberCount = $_POST['member']; 

エラー(Undefined index)は、あなたの$_POST[]変数からこの場合namememberに、いくつかの非既存の変数を持っていることを示しています。詳細については、undefined variableを参照してPOST要求が正しく行われていることを確認してください。

iphoneというデータベースがデータベースシステムに存在しないため、3番目のエラー(Warning: mysqli::__construct(): (HY000/1049))が発生します。正しく入力したか、または名前がiphoneの実際のデータベースをまだ作成していないかどうかを確認してください。

namememberが(正しく)彼らはNULLに設定されているとmysqliのライブラリがそのようにしないあなたのPOSTリクエストで定義されていないので、最後のエラーは、理由は最初の2つのエラーの原因です。

関連する問題