[英]PHP can't connect to Linux Apache/MYSQL Server [Edited]
簡單的Web表單和表,用於將文本數據存儲在MySQL數據庫中。 我使用的是Ubuntu 16.04,以及MySQL和Apache的最新形式。 該站點有2個頁面,一個顯示表中的數據,另一個是允許用戶輸入新數據的表單。
問題:我的代碼似乎沒有連接到MySQL數據庫。 根據Fred -ii-我的PHP可能沒有正確配置。 下面的鏈接是我按照設置一切的教程。
下面是我的代碼的屏幕截圖,以及應該顯示數據的主頁。 我覺得表單頁面有點沒用,因為它在頁面上或單擊提交時沒有顯示錯誤代碼。 如果您需要更多信息,請告訴我們!
帶表格的頁面的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();
?>
您的代碼出現在輸出的HTML上這一事實表明PHP未被解析/執行。 您確定在Apache / PHP端正確設置了所有內容嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.