【问题标题】:PHP can't connect to Linux Apache/MYSQL Server [Edited]PHP 无法连接到 Linux Apache/MYSQL Server [已编辑]
【发布时间】:2017-09-07 04:37:27
【问题描述】:

在 MySQL 数据库中存储文本数据的简单 Web 表单和表格。我使用的是 Ubuntu 16.04,以及最新形式的 MySQL 和 Apache。该站点有 2 个页面,一个显示表格中的数据,另一个是允许用户输入新数据的表单。

  • Ubuntu 16.04
  • MySQL 版本 14.14 Distrib 5.7.17
  • PHP 7.0.15
  • Apache2 2.4.18

问题:我的代码似乎没有连接到 MySQL 数据库。根据 Fred -ii- 我的 PHP 可能没有正确配置。下面的链接是我设置所有内容时遵循的教程。

https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04

下面是我的代码截图,以及应该显示数据的主页。我觉得表单页面有点毫无意义,因为它在页面上或单击提交时没有显示错误代码。如果您需要更多信息,请告诉我!

带有表格的页面的 HTML:

 <!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Technological Pioneers</title>
  <style>
    body {background-color: powderblue;}
    h1   {color: blue;}
    p    {color: red;}
</style>
</head>
<body>
<font face="verdana"><center><h1>Technological Pioneers</h1></center>
<br>
<center><a href="input.html">Click here to add a name to the list!</a></center></font>
  <?php
    $servername = "localhost";
    $username = "XXXXX";
    $password = "XXXXX";
    $dbname = "Apollo";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);

  //execute the SQL query and return records
  $result = mysql_query("SELECT * FROM Employees");
  ?>
  <table font face="verdana" style= "background-color: #b0e0e6; color: #000000; margin: 0 auto;" >
  <thead>
    <tr>
      <th>Employee Name</th>
      <th>Job Title</th>
      <th>Job Summary</th>

    </tr>
  </thead>
  <tbody>
    <?php
      while( $row = mysql_fetch_assoc( $result ) ){
        echo
        "<tr>
          <td>{$row\['name'\]}</td>
          <td>{$row\['title'\]}</td>
          <td>{$row\['summary'\]}</td>
        </tr>\n";
      }
    ?>
  </tbody>
</table>
 <?php mysql_close($connector); ?>
</body>
</html>

表单的 HTML:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Make your mark!</title>
    <style>
        body {background-color: powderblue;}
        h1   {color: blue;}
        p    {color: red;}
    </style>
</head>
 <body>
<center>
    <font face="verdana">
    <h1>Make your mark on history!</h1><br>
    <p>Know someone who worked on the Apollo Program? Did you work on Apollo? <br> Add the NASA employee's name to our database here, so their contribution will always be remembered.</p>
</center><
<center>
    <form action="store.php" method="POST">
        Employee Name:<br>
        <input type="text" name="name"  size="30" maxlength="60" autofocus ><br><br>
        Job Title:<br>
        <input type="text" name="title" size="30" maxlength="60" ><br><br>
        Job Summary:<br>
        <textarea rows="4" cols="50" name="summary" maxlength="249"></textarea><br><br>
        <input type="submit" name="submit" value="Submit">
    </form></font>
</center>
</body>
</html>

PHP 存储数据:

<?php
$servername = "localhost";
$username = "XXXXX";
$password = "XXXXX";
$dbname = "Apollo";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO Employees (name, title, summary)
VALUES ('name', 'title', 'summary')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

【问题讨论】:

  • 代码图片;为什么?如果有人需要重新输入内容怎么办?
  • 您可以在问题框中添加代码。请使用粘贴的代码更新您的问题。
  • 一个问题:转储到浏览器的代码是connect_erroer),而您发布的代码没有错字,表明您在某些时候进行了一些更改,因此没有办法告诉问题是否相关。邮政编码,不是图片。
  • 我很抱歉那些家伙。已进行更改。我看到了我的 connect_error 错字在哪里,并且已经修复了它。

标签: php html mysql apache html-table


【解决方案1】:

您的代码出现在输出的 HTML 中的事实表明 PHP 没有被解析/执行。你确定你在 Apache/PHP 端的所有设置都正确吗?

【讨论】:

  • 同意。然而,这部分你的答案 “你确定你在 Apache/PHP 端正确设置了所有东西吗?” 应该读到类似 “确保你在 Apache/PHP 上正确设置了所有东西” PHP 方面。” 而不是问问题。那些是cmets,只是说。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
相关资源
最近更新 更多