通过PHP - WAMP服务器将数据插入MySQLi

时间:2015-08-02 08:32:35

标签: php android mysql mysqli wamp

我有一个wamp服务器设置。它完美地工作:) 然后我输入phpMyAdmin并创建了一个表。使用我制作的Android应用程序,我想在我的数据库中插入一条记录。 android(java)代码是正确的,我100%肯定。当我创建一个记录时,它不起作用。

因为我根本不了解PHP,所以我认为我的错误在于Register.php

这是文件: 任何洞察我的问题都会很棒!

请注意我在真实文件中使用正确的公共IP。我刚刚为下面的代码输入了一个。此外,我创建了一个具有所需权限的用户(代替用户名和密码)。数据库"数据库"也存在。

Register.php

$con = mysqli_connect("http://148.12.0.153:3306","username","password", "database");

$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$phone = $_POST["phone"];
$balance = $_POST["balance"];
$NameAndSurname = $_POST["NameAndSurname"];
$DateOfBirth = $_POST["DateOfBirth"];
$SchoolName = $_POST["SchoolName"];
$Gender = $_POST["Gender"];
$Grade = $_POST["Grade"];
$Class = $_POST["Class"];
$Country = $_POST["Country"];
$Province = $_POST["Province"];
$Address = $_POST["Address"];
$City = $_POST["City"];
$PostalCode = $_POST["PostalCode"];

$statement = mysqli_prepare($con, "INSERT INTO users (username, email, password, phone, balance, NameAndSurname, DateOfBirth, SchoolName, Gender, Grade, Class, Country, Province, Address, City, PostalCode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "ssssisssiisssssi", $username, $email, $password, $phone, $balance, $NameAndSurname, $DateOfBirth, $SchoolName, $Gender, $Grade, $Class, $Country, $Province, $Address, $City, $PostalCode);
mysqli_stmt_execute($statement);

mysqli_stmt_close($statement);

mysqli_close($con);

2 个答案:

答案 0 :(得分:1)

这里有很多事要提。

首先,您使用Android应用程序在Apache服务器上启动此Register.php脚本,就像它是一个网页一样,因此该脚本在服务器而不是您的手机或平板电脑上运行。因此,Apache和MySQL以及脚本都在WAMPServer PC上运行。所以你的连接字符串不需要一些真正的IP地址,它可以使用,应该使用localhost127.0.0.1

之类的东西

接下来,您的数据库访问代码假设一切都将正确发生,这可能不是上面的段落。因此,请始终检查状态代码并将状态报告回调用程序,以便明确决定下一步该做什么。将错误记录到PHP错误日志中也是一个好主意,因此当它生效时,您可以检查日志,看看是否有任何问题,而无需运行手机应用程序。

所以尝试这些改变:

// init the reply class
$result = new stdClass();
$result->status = 'OK';  


$con = mysqli_connect("127.0.0.1","username","password", "database");
if ( ! $con ) {
    $result->status        = 'ERROR';
    $result->error_code    = mysqli_connect_errno();
    $result->error_message = mysqli_connect_error();
    // terminate and report to error log
    error_log('Database connection failed'.mysqli_connect_error(), 0);
    echo json_encode($result);  // return status as json
    exit;
}

// You should never use data sent from the screen without
// validating it and cleaning it up so you need some sort of 
// $_POST = validate_sanity($_POST);

$username       = $_POST["username"];
$email          = $_POST["email"];
$password       = $_POST["password"];
$phone          = $_POST["phone"];
$balance        = $_POST["balance"];
$NameAndSurname = $_POST["NameAndSurname"];
$DateOfBirth    = $_POST["DateOfBirth"];
$SchoolName     = $_POST["SchoolName"];
$Gender         = $_POST["Gender"];
$Grade          = $_POST["Grade"];
$Class          = $_POST["Class"];
$Country        = $_POST["Country"];
$Province       = $_POST["Province"];
$Address        = $_POST["Address"];
$City           = $_POST["City"];
$PostalCode     = $_POST["PostalCode"];

$sql = "INSERT INTO users 
           (username, email, password, phone, 
            balance, NameAndSurname, DateOfBirth, 
            SchoolName, Gender, Grade, Class, 
            Country, Province, Address, City, 
            PostalCode) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

$statement = mysqli_prepare($con, $sql );
if ( ! $statement ) {
    $result->status        = 'ERROR';
    $result->error_code    = mysqli_errno();
    $result->error_message = mysqli_error();
    // terminate and report to error log
    error_log('Database connection failed'.mysqli_error(), 0);
    echo json_encode($result);  // return status as json
    exit;
}

$res = mysqli_stmt_bind_param($statement, "ssssisssiisssssi",
               $username, $email, $password, $phone, $balance, 
               $NameAndSurname, $DateOfBirth, $SchoolName, $Gender, 
               $Grade, $Class, $Country, $Province, $Address, $City, 
               $PostalCode);

if ( ! $res ) {
    $result->status        = 'ERROR';
    $result->error_code    = mysqli_errno();
    $result->error_message = mysqli_error();
    // terminate and report to error log
    error_log('Database connection failed'.mysqli_error(), 0);
    echo json_encode($result);  // return status as json
    exit;
}

if ( mysqli_stmt_execute($statement) ) {
    $result->status        = 'OK';
    $result->message       = 'Row deleted';
    echo json_encode($result);  // return status as json
    exit;
} else {
    $result->status        = 'ERROR';
    $result->error_code    = mysqli_errno();
    $result->error_message = mysqli_error();
    // terminate and report to error log
    error_log('Database DELETE failed'.mysqli_error(), 0);
    echo json_encode($result);  // return status as json
    exit;
}

//mysqli_close($con);
//PHP will do all the connection and statment closing automatically 
// So you dont actually need to do any of this unless you are running 
// a script the will consume large numbers of statement and you may
// feel it necessary to close them out to kepp the memory footprint smaller

答案 1 :(得分:0)

将mysqli_stmt_close更改为

mysqli_stmt_close($statement) or die(mysqli_error());

这将为您提供更准确的错误,说明失败的原因。