gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:04:32 24 4
gpt4 key购买 nike

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

由于我不太了解 PHP,所以我认为我的错误位于 Register.php 的某个地方

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

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

注册.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);

最佳答案

好的,这里有很多事情要提。

首先,您使用 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

关于php - 通过 PHP - WAMP 服务器将数据插入 MySQLi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31770014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com