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

时间:2018-02-14 22:22:12

标签: php html mysql

我创建了一个非常简单的网站,询问用户的姓名和电子邮件,然后将这些输入发送到MySQL中的表中。我在xampp离线测试它完美无缺。所以我去了,我在网上发布了它(我打算使用github,但我不认为github允许php,所以我不得不放弃它)。我使用无限远(或vista面板,该网站有点令人困惑)。具有表单的初始页面加载正常,但是当用户提交其输入时,它会将它们重写为读取" ERROR:无法连接的页面。没有这样的文件或目录"。

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

1 个答案:

答案 0 :(得分:1)

您的错误在:

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

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

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

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

希望这有帮助!