如何让我的html代码以有效的方式输出数据?

时间:2016-04-05 14:31:24

标签: php html mysql

我正在尝试输出我的MySQL用户表的内容,我设置的代码的方式是管理员可以在表中添加,编辑和删除行。在仔细研究这个问题之后,我发现html代码以笨拙的方式输出值,因为它只将列中的第一个单词计为有效值,因此忽略其他单词。我该怎么办呢?

例如,假设我正在尝试输出名为Skulls&的专辑。骨头表上的输出只是骷髅头。

Admin_Album_Page.php

<!DOCTYPE HTML>
<html>
<head>
<title>View Album Table</title>
</head>
<body>

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
require_once 'ConnectorCode.php';

//mysql_query command is used to select data from Albums table
$result = mysqli_query($conn, "SELECT * FROM tbl_Albums");

//Echos setting established onto the main table
echo "<table border='1'>";
echo "<tr> <th>Album ID</th> <th>Album Name</th> <th>Number Of Tracks</th>        
<th> Genre </th> <th>Track ID</th> </tr>";

//Results are looped and then displayed in tables while($row =     mysqli_fetch_array($result)) {
echo "<form action=Admin_Album_Page.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=number name=albumid value=" . $row['Album_id'] . " </td>";
echo "<td>" . "<input type=text name=albumname value=" . $row['Album_Name'] . " </td>";
echo "<td>" . "<input type=number name=numberoftracks value=" . $row['Number_Of_Tracks'] . " </td>";
echo "<td>" . "<input type=text name=genre value=" . $row['Genre'] . " </td>" ;
echo "<td>" . "<input type=number name=artistid value=" . $row['Artist_id'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $row['Album_id'] . " </td>";
echo "<td>" . "<input type=submit name=update value=Update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=Delete" . " </td>";
echo "</form>";
}

echo "<form action=Admin_Artist_Page.php method=post>";
echo "<tr>";
echo "<td></td>";
echo "<td><input type=text name=ualbumname></td>";
echo "<td><input type=text name=unumberoftracks></td>";
echo "<td><input type=text name=ugenre></td>";
echo "<td><input type=number name=uartistid></td>";
echo "<td>" . "<input type=submit name=add value=Add" ." </td>";
echo "</form>";
echo "</table>";

//Connection is closed
mysqli_close($conn);
?>


<p><a href="Admin.php">Return to main page</a></p>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

问题在于HTML渲染。

您需要将代码更改为:

echo "<form action=\"Admin_Album_Page.php\" method=\"post\">";
echo "<tr>";
echo "<td>" . "<input type=\"number\" name=\"albumid\" value=\"" . $row['Album_id'] . "\" </td>";
echo "<td>" . "<input type=\"text\" name=\"albumname\" value=\"" . $row['Album_Name'] . "\" </td>";
echo "<td>" . "<input type=\"number\" name=\"numberoftracks\" value=\"" . $row['Number_Of_Tracks'] . "\" </td>";
echo "<td>" . "<input type=\"text\" name=genre value=" . $row['Genre'] . " </td>" ;
echo "<td>" . "<input type=\"number\" name=\"artistid\" value=\"" . $row['Artist_id'] . "\" </td>";
echo "<td>" . "<input type=\"hidden\" name=\"hidden\" value=\"" . $row['Album_id'] . "\" </td>";
echo "<td>" . "<input type=\"submit\" name=\"update\" value=\"Update" . "\" </td>";
echo "<td>" . "<input type=\"submit\" name=\"delete\" value=\"Delete" . "\" </td>";
echo "</form>"

echo "<form action=\"Admin_Artist_Page.php\" method=\"post\">";
echo "<tr>";
echo "<td></td>";
echo "<td><input type=\"text\" name=\"ualbumname\"></td>";
echo "<td><input type=\"text\" name=\"unumberoftracks\"></td>";
echo "<td><input type=\"text\" name=\"ugenre\"></td>";
echo "<td><input type=\"number\" name=\"uartistid\"></td>";
echo "<td>" . "<input type=\"submit\" name=\"add\" value=\"Add" ."\" </td>";
echo "</form>";
echo "</table>";

解释

HTML的工作方式如下:

<tag attribute="value"></tag>

您无法通过

简单地调用它
<tag attribute=value></tag>

在我向您展示的代码中,您希望确保"符号是字符串的一部分,因此您可以使用\

来转义它