gpt4 book ai didi

php - 发布了一个网站,但现在我的数据不会转移到 MySQL

转载 作者:行者123 更新时间:2023-11-28 00:48:35 25 4
gpt4 key购买 nike

我制作了一个非常简单的网站,要求用户提供他们的姓名和电子邮件,然后将这些输入发送到 MySQL 中的表中。我在 xampp 中离线测试它,它工作得很好。所以我去了,我把它发布到网上(我打算使用 github,但我不认为 github 允许 php,所以我不得不放弃那个)。我正在使用无限免费(或 vista 面板,该网站有点困惑)。带有表单的初始页面加载正常,但是当用户提交他们的输入时,它将他们重定向到一个页面,上面写着“错误:无法连接。没有这样的文件或目录”。

html代码如下(index.html):

<html>
<head>
<meta charset='utf-8'>
<meta content='IE=edge' http-equiv='X-UA-Compatible'>
<meta content='width=device-width, initial-scale=1' name='viewport'>
<title>Mongolia to the Moon</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/0.7.13/webcomponents-lite.min.js"></script>
</head>
<body>
<div id="title">
<center><h1>Official Petition to Relocate Mongolia to the Moon</h1><center>
</div>

<center><h3>In our eyes, Mongolia is insignificant, and therefore should
be removed. However, it would be inhumane to destroy innocent lives, even
for our organization. So we have come to a secondary solution that
Mongolia must be transferred elsewhere. The closest place away from earth
would be the moon. Please sign the petition below to remove Mongolia from
the earth and transfer it to the moon.
</h3><center>

<form action="connect.php" method="post">
<table width="20%" border="0" cellspacing="2" cellpadding="1">
<tr>
<td>Name:</td>
<td colspan="2"><input type="text" name="name"></td>
</tr>
<tr>
<td>Email:</td>
<td colspan="2"><input type="text" name="email"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>

</body>
</html>

这是我的 php (connect.php):

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "mypet");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Prepare an insert statement
$sql = "INSERT INTO user (name,email) VALUES (?,?)";

if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ss", $name,$email);

// Set parameters
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
echo "<center><h1>Official Petition to Relocate Mongolia to the
Moon</h1><center>";
echo "<center><h3>In our eyes, Mongolia is insignificant, and therefore
should be removed. However, it would be
inhumane to destroy innocent lives, even for our organization. So we have
come to a secondary solution that
Mongolia must be transferred elsewhere. The closest place away from earth
would be the moon. Please sign the
petition below to remove Mongolia from the earth and transfer it to the
moon.</h3><center>";
echo "<center><h2>Thank You for Signing the Petition</h2></center>";
} else{
echo "<center><h1>Official Petition to Relocate Mongolia to the
Moon</h1><center>";
echo "<center><h3>In our eyes, Mongolia is insignificant, and therefore
should be removed. However, it would be
inhumane to destroy innocent lives, even for our organization. So we have
come to a secondary solution that
Mongolia must be transferred elsewhere. The closest place away from earth
would be the moon. Please sign the
petition below to remove Mongolia from the earth and transfer it to the
moon.</h3><center>";
echo "<center><h2>Oops! Something went wrong. Please try again.</h2>
</center>";
}
}

// Close statement
mysqli_stmt_close($stmt);

// Close connection
mysqli_close($link);
?>

这是我第一次尝试制作网站并使用 html 和 php 进行编码,我唯一使用过的语言是 pascal,据我所知,这对制作网站并没有多大帮助。我的标题标签中提到的任何想法都不应该被认真对待。只是想我会把它放在那里,以防有人误会我的(坏)笑话。谢谢。

此外,如果您出于某种原因需要查看网站,链接为 http://mongolia-pet.rf.gd

最佳答案

您的错误在:

$link = mysqli_connect("localhost", "root", "", "mypet");

mysqli_connect() 的第一个参数需要主机名或 IP 地址,并且您提供的“localhost”可能在您的测试环境中有效,但是当您发布站点时,您在本地删除了连接。

很可能您的 MySQL 没有转移到实时服务器上。在这种情况下,您需要:

  • 提供 MySQL 数据库所在服务器的外部 IP 地址,而不是“localhost”。
  • 将 MySQL 移动到与站点相同的服务器上

希望这对您有所帮助!

关于php - 发布了一个网站,但现在我的数据不会转移到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48797201/

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