2016-12-20 152 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:

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

} 

然而,当我使用邮差使用HTTP POST方法在Chrome中,它指出这个

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:“Notice:Undefined variable”和“Notice:Undefined index”]的可能重复(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-索引)从这个问题开始。 – Ohgodwhy

回答

0

你的前两个错误,因为这两条线造成的:

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

错误(Undefined index)告诉你,你有一些不存在的变量,在这种情况下namemember$_POST[]变量。确保您的POST请求正确,请参阅undefined variable了解更多信息。

您的第三个错误(Warning: mysqli::__construct(): (HY000/1049))是由于您的数据库系统中没有名为iphone的数据库造成的。检查您是否正确输入了该名称,或者如果尚未创建名称为iphone的实际数据库。

最后的错误,是因为前两个错误引起的,因为(正确地)在您的POST请求,他们已被设为NULL和mysqli的库不一样,定义namemember都没有。