简体   繁体   English

PHP无法连接到Linux Apache / MYSQL Server [已编辑]

[英]PHP can't connect to Linux Apache/MYSQL Server [Edited]

Simple web form and table that stores text data in a MySQL database. 简单的Web表单和表,用于将文本数据存储在MySQL数据库中。 I am using Ubuntu 16.04, and the newest forms of MySQL and Apache. 我使用的是Ubuntu 16.04,以及MySQL和Apache的最新形式。 The site has 2 pages, one that shows the data that is in the table, and the other is the form that allows the user to input new data. 该站点有2个页面,一个显示表中的数据,另一个是允许用户输入新数据的表单。

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

Problem: My code does not appear to be connecting to the MySQL database. 问题:我的代码似乎没有连接到MySQL数据库。 According to Fred -ii- my PHP may not be properly configured. 根据Fred -ii-我的PHP可能没有正确配置。 The link below this is the tutorial I followed to set everything up. 下面的链接是我按照设置一切的教程。

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

Below are screenshots of my code, and the home page that should display the data. 下面是我的代码的屏幕截图,以及应该显示数据的主页。 I felt the form page was kind of pointless because it shows no error code on the page or on clicking submit. 我觉得表单页面有点没用,因为它在页面上或单击提交时没有显示错误代码。 If you need more information please let me know! 如果您需要更多信息,请告诉我们!

在此输入图像描述

HTML For Page with the Table: 带表格的页面的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 for Form: 表格的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 To Store Data: 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();
?>

The fact that your code appears on the outputted HTML, suggests that PHP is not parsed/executed. 您的代码出现在输出的HTML上这一事实表明PHP未被解析/执行。 Are you sure you have everything setup correctly on the Apache/PHP side? 您确定在Apache / PHP端正确设置了所有内容吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM