在 PHP 中使用表
如果这个问题让任何人感到沮丧,我很抱歉...我确实是 PHP + MYSQL 的初学者,但我很乐意在这方面取得一些进展!
我的目标:
- 创建一个可以在网页中查看的表格。
- 能够将数据(行)添加到该表中,并且在下次加载页面时数据仍然存在。
步骤 1:创建表
<html>
<body>
<?php
// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";
mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";
// Create a MySQL table in the selected database (called ExampleTable)
$sql = "CREATE TABLE ExampleTable
(
// Set the primary key (personID)
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
ColumnOne varchar(15)
ColumnTwo varchar(15)
ColumnThree varChar(30)
)";
// Execute query
mysql_query($sql,$con);
mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data1', 'Data2', 'Data3')");
mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data_Data1', 'Data_Data2', 'Data_Data3')");
mysql_close($con);
?>
</body>
</html>
因此,我现在创建了一个 MYSQL 表,其中包含两个三列和两行数据。我怎样才能让这个表格显示在网页上?
第 2 步:保存/检索保存的数据 有什么方法可以将数据添加到表中,以便数据永久存在吗? - 或者这是默认情况下的工作方式?
例如:假设我有一个带有按钮的表单。单击该按钮时,新行将添加到表“ExampleTable”中。下次用户访问该页面时,该表将使用新添加的数据进行更新。
非常感谢您的帮助。我知道我是初学者,还没有完全理解这些主题。任何回复将不胜感激。
I'm sorry if this question frustrates anyone ... I am truly a beginner to PHP + MYSQL but I would love to make some progress on this one!
My Goal:
- To create a table that I can view in a web page.
- To be able to add data (rows) to this table and still have the data be there the next time the page is loaded.
Step 1: Creating the table
<html>
<body>
<?php
// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";
mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";
// Create a MySQL table in the selected database (called ExampleTable)
$sql = "CREATE TABLE ExampleTable
(
// Set the primary key (personID)
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
ColumnOne varchar(15)
ColumnTwo varchar(15)
ColumnThree varChar(30)
)";
// Execute query
mysql_query($sql,$con);
mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data1', 'Data2', 'Data3')");
mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data_Data1', 'Data_Data2', 'Data_Data3')");
mysql_close($con);
?>
</body>
</html>
So, I have now created a MYSQL table with two three columns, and two rows of data. How can I get this table to show up on a web page?
Step 2: Saving / Retrieving saved data
Is there some way that I can add data to a table, so that the data will be there permanently? - or is this how it works by default?
For example: Let's say that I have a form with a button on it. When the button is clicked a new row is added to the table 'ExampleTable'. The next time the user visits the page the table will be updated with his newly added data.
Thank you very much for any help. I understand that I am a beginner and do not fully understand these topics yet. Any responses will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议在单独的步骤中创建表,而不是从 PHP 中创建;您不希望每次刷新网页时都创建一个新表。
创建表后,您可以通过执行(在 PHP 中,使用 mysql_query)类似“SELECT * from {tablename}”的查询来从中获取数据。从 mysql 获得查询结果后,您可以使用各种 PHP 循环和 mysql 记录读取方法,以您想要的形式将查询结果输出到 PHP 脚本将服务于客户端的页面中。
I'd recommend creating the table in a separate step, not from PHP; you don't want every time your web page is refreshed to create a new table.
Once your table is created, you can get the data from it by executing (within your PHP, using mysql_query) a query like "SELECT * from {tablename}". Once you've got that query result from mysql, then you can use the various PHP looping and mysql record reading methods to output the results from your query in the form you want into the page your PHP script will be serving to your client.
有 SELECT 和 UPDATE 查询可以执行此操作。例如,如果您想显示表中的数据,您可以使用如下所示的查询:
在 PHP 中,您可以使用这些数据,例如如下所示:
对于 UPDATE 查询:
PHP 中的用法也与 mysql_query 一起使用。您还可以在查询中使用 WHERE 条件。
There are SELECT and UPDATE queries to do this. For example if you want to show data in a table, you would use query looking like this:
In PHP, you can work with these data for example like this:
And to the UPDATE query:
Usage in PHP is also with mysql_query. You can also use WHERE conditions in the query.
1 - 显示
来自以下位置的所有数据: http://www.tizag.com/mysqlTutorial/mysqlselect.php< /a>
2 - 数据存储
任何
INSERT
插入数据库的内容都将保留*在数据库中,直到被显式删除。*除了少数例外 - 但您稍后会在数据库之旅中了解它们:)
1 - Displaying All Data
From: http://www.tizag.com/mysqlTutorial/mysqlselect.php
2 - Data Storage
Anything
INSERT
-ed into a database will remain* in the database until it is explicitly deleted.*with few exceptions - but you'll learn about them a little later in your database journey :)