无法 post 将数据形成到 RDS MySQL 数据库
Unable to post form data to RDS MySQL Database
任何可以提供帮助(或遇到相同问题)的人您好。
我的网站上有一个表格,我需要将数据存储在数据库中。
我在使用任何其他数据库服务时都没有问题,但在使用 Amazon RDS 数据库时我就不走运了。
此php 文件用于将数据发送到数据库。
<?php
$dbhost = "xxxx.xxxx.ap-southeast-2.rds.amazonaws.com";
$dbuser = "username";
$dbpass = "pasword";
$userid = $_GET['UniqueID'];
$username = $_GET['name'];
$useremail = $_GET['email'];
$userphone = $_GET['phone'];
$userref = $_GET['refid'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO landing_post (`useid`, `name`, `email`, `phone`, `refid`) VALUES ('$userid', '$username', '$useremail', '$userphone', '$userref')";
mysql_select_db('bunker');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>
表单提交到原post动作后,打开下面url.
https://example.com.au/test/post.php?&UniqueID=10020&name=burger&email=test%40mvmedia.com&phone=1800+000+000&refid=28383
$_GET 函数使用 url 中的变量填写数据。
当 运行 来自基于 cpanel 的服务器的 php 时,我收到此错误。
Could not connect: Can't connect to MySQL server on 'xxxxx.xxxxxxx.ap-southeast-2.rds.amazonaws.com' (111)
当 运行 来自 AWS EC2 实例的 php 时,我什至没有得到错误读数。
如有任何帮助或见解,我们将不胜感激。
干杯,
安迪
向 JeanPaul98 致敬,是他让我走上了正确的道路。经过多次尝试和错误后,我发现下面的代码解决了这个问题。
<?php
$userid = $_GET['UniqueID'];
$username = $_GET['name'];
$useremail = $_GET['email'];
$userphone = $_GET['phone'];
$userref = $_GET['refid'];
$link = mysqli_connect('xxxxx.xxxxxx.ap-southeast-2.rds.amazonaws.com', 'User', 'password','database');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Check if server is alive
if (mysqli_ping($link))
{
echo "Connection is ok!";
}
else
{
echo "Error: ". mysqli_error($link);
}
mysqli_query($link,"INSERT INTO table (`useid`, `name`, `email`, `phone`, `refid`) VALUES ('$userid', '$username', '$useremail', '$userphone', '$userref')")
or die(mysqli_error($link));
echo "Entered data successfully\n";
mysqli_close($link);
?>
主要的变化是我不再使用 mysql 而是使用 mysqli。也有一些结构上的变化,使代码与 mysqli 一起工作。对于其他有类似问题的人,这里有几件事需要检查。
您已为RDS数据库安全组IP开放入端口。
数据库可公开访问。
- 您的 php 版本已安装 PDO_MYSQL 或兼容的驱动程序(更多详细信息,请点击此处
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_PHP.rds.html)
任何可以提供帮助(或遇到相同问题)的人您好。
我的网站上有一个表格,我需要将数据存储在数据库中。 我在使用任何其他数据库服务时都没有问题,但在使用 Amazon RDS 数据库时我就不走运了。
此php 文件用于将数据发送到数据库。
<?php
$dbhost = "xxxx.xxxx.ap-southeast-2.rds.amazonaws.com";
$dbuser = "username";
$dbpass = "pasword";
$userid = $_GET['UniqueID'];
$username = $_GET['name'];
$useremail = $_GET['email'];
$userphone = $_GET['phone'];
$userref = $_GET['refid'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO landing_post (`useid`, `name`, `email`, `phone`, `refid`) VALUES ('$userid', '$username', '$useremail', '$userphone', '$userref')";
mysql_select_db('bunker');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>
表单提交到原post动作后,打开下面url.
https://example.com.au/test/post.php?&UniqueID=10020&name=burger&email=test%40mvmedia.com&phone=1800+000+000&refid=28383
$_GET 函数使用 url 中的变量填写数据。
当 运行 来自基于 cpanel 的服务器的 php 时,我收到此错误。
Could not connect: Can't connect to MySQL server on 'xxxxx.xxxxxxx.ap-southeast-2.rds.amazonaws.com' (111)
当 运行 来自 AWS EC2 实例的 php 时,我什至没有得到错误读数。
如有任何帮助或见解,我们将不胜感激。 干杯, 安迪
向 JeanPaul98 致敬,是他让我走上了正确的道路。经过多次尝试和错误后,我发现下面的代码解决了这个问题。
<?php
$userid = $_GET['UniqueID'];
$username = $_GET['name'];
$useremail = $_GET['email'];
$userphone = $_GET['phone'];
$userref = $_GET['refid'];
$link = mysqli_connect('xxxxx.xxxxxx.ap-southeast-2.rds.amazonaws.com', 'User', 'password','database');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Check if server is alive
if (mysqli_ping($link))
{
echo "Connection is ok!";
}
else
{
echo "Error: ". mysqli_error($link);
}
mysqli_query($link,"INSERT INTO table (`useid`, `name`, `email`, `phone`, `refid`) VALUES ('$userid', '$username', '$useremail', '$userphone', '$userref')")
or die(mysqli_error($link));
echo "Entered data successfully\n";
mysqli_close($link);
?>
主要的变化是我不再使用 mysql 而是使用 mysqli。也有一些结构上的变化,使代码与 mysqli 一起工作。对于其他有类似问题的人,这里有几件事需要检查。
您已为RDS数据库安全组IP开放入端口。
数据库可公开访问。
- 您的 php 版本已安装 PDO_MYSQL 或兼容的驱动程序(更多详细信息,请点击此处 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_PHP.rds.html)