dongyanzhui0524 2016-12-20 19:35
浏览 131

尝试使用php(HTTP POST)从iOS连接到MySQL数据库

Below is my sample code for my PHP program, and my database has already been created.

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

 }

However when I use HTTP POST method using Postman in Chrome, it states this

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

What does this mean, and what should I change?

  • 写回答

1条回答 默认 最新

  • duanbing2963 2016-12-20 21:04
    关注

    Your first two errors are caused because of these two lines:

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

    The error (Undefined index) tells you that you have some non-existing variables, in this case name and member from your $_POST[] variable. Make sure your POST request is correctly made, see undefined variable for more information.

    Your third error (Warning: mysqli::__construct(): (HY000/1049)) is caused because there isn't a database called iphone in your database system. Check if you have typed it correctly or if you haven't yet created an actual database with the name iphone.

    The last error is caused because of the first two errors, since name and member aren't (correctly) defined in your POST request they have been set to NULL and the mysqli library doesn't like that.

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题