PHP循环通过MySQL表与提交按钮

时间:2021-10-29 06:44:01

Okay, I've looked for a solution to this question, but to no avail. It could be the way I worded it, but I've tried almost everything I could think of. And I know this is probably something so simple, I just can't wrap my head around it.

好的,我已经找到了这个问题的解决方案,但无济于事。这可能是我说的方式,但我已经尝试了几乎所有我能想到的东西。而且我知道这可能是非常简单的事情,我无法绕过它。

The problem I'm having is this:

我遇到的问题是:

I have a form that is prepopulated with data from a MySQL database. This part of the form is strictly for viewing the records (that were previously entered by the user). I can get it to work; I have the database linked, and it shows the data for the row of the particular table that I want. But this is what I'm having trouble with.

我有一个预填充MySQL数据库数据的表单。表单的这一部分严格用于查看记录(以前由用户输入)。我可以让它上班;我链接了数据库,它显示了我想要的特定表的行的数据。但这就是我遇到的麻烦。

I want there to be a button that the user can press that cycles through each row of data in the database, and outputs it in the form accordingly.

我希望有一个按钮,用户可以按下该按钮循环访问数据库中的每一行数据,并相应地输出它。

It's a little complicated, so I'll use a simple example.

这有点复杂,所以我将使用一个简单的例子。

Let's say I have a basic table in a MySQL database with three columns: ID, Name, and EmailAddress.

假设我在MySQL数据库中有一个基本表,其中包含三列:ID,Name和EmailAddress。

Here's the query that grabs the data:

这是抓取数据的查询:

$sql = "SELECT * from tbl_Users ORDER BY ID ASC";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);

Now I know this is deprecated, but I am working in an older version of php, and I'm trying to stay consistent other pages/apps in this particular domain.

现在我知道这已被弃用了,但我正在使用旧版本的php,我正在努力保持这个特定域中的其他页面/应用程序的一致性。

Now let's say I have a simple form with two inputs and a submit button.

现在让我们说我有一个带有两个输入和一个提交按钮的简单表单。

HTML

<form name="button" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="Name" value="<?php echo $row[Name]; ?>" />
<input type="text" name="emailAddress" value="<?php echo $row[EmailAddress]; ?>" />
<input type="submit" name="submit" value="Next Record" />
</form>

Now I can get it to echo what I need in the form. It can prepopulate the form with a row, as I want it to. The trouble lies with the submit button.

现在我可以让它回应我在表单中需要的内容。如我所愿,它可以预先填充表格。问题在于提交按钮。

When the submit button is clicked, I want the next row to populate in the form, and so on until the end of the records.

单击提交按钮时,我希望在表单中填充下一行,依此类推,直到记录结束。

I've tried the if(isset($_POST['submit']{...}, yet I don't know exactly to get this to work. I've tried loops, etc. I know there is a way, I just cannot comprehend one right now. Thanks in advanced.

我已经尝试了if(isset($ _ POST ['submit'] {...},但我不知道到底是为了让它工作。我已经尝试过循环等等。我知道有办法,我现在无法理解。感谢先进。

2 个解决方案

#1


1  

Okay, so I DID manage to get it to work with PHP. I found a simple PHP Pagination script and changed the limit of the query to 1. Instead of echoing out the results of the query in the WHILE loop, I just called the variables for the form

好的,所以我DID设法让它与PHP一起工作。我发现了一个简单的PHP分页脚本,并将查询的限制更改为1.而不是在WHILE循环中回显查询的结果,我只是调用了表单的变量

#2


0  

Unfortunately in your case, PHP is server side, and it will run any queries before the client side has a chance to catch up and output it. I believe you will need to use javascript for this.

不幸的是,在你的情况下,PHP是服务器端,它将在客户端有机会赶上并输出它之前运行任何查询。我相信你需要使用javascript。

You can do one of two things, 1. Pull all table information on load (slower way) and use javascript to show/hide certain elements of that query. Or 2. You can use AJAX to pull the data on command.

您可以执行以下两项操作之一:1。在加载时拉出所有表信息(较慢的方式)并使用javascript显示/隐藏该查询的某些元素。或者2.您可以使用AJAX来获取命令数据。

I would suggest learning javascript (e.g. use a framework like jQuery) to perform an AJAX call for this.

我建议学习javascript(例如使用像jQuery这样的框架)为此执行AJAX调用。

#1


1  

Okay, so I DID manage to get it to work with PHP. I found a simple PHP Pagination script and changed the limit of the query to 1. Instead of echoing out the results of the query in the WHILE loop, I just called the variables for the form

好的,所以我DID设法让它与PHP一起工作。我发现了一个简单的PHP分页脚本,并将查询的限制更改为1.而不是在WHILE循环中回显查询的结果,我只是调用了表单的变量

#2


0  

Unfortunately in your case, PHP is server side, and it will run any queries before the client side has a chance to catch up and output it. I believe you will need to use javascript for this.

不幸的是,在你的情况下,PHP是服务器端,它将在客户端有机会赶上并输出它之前运行任何查询。我相信你需要使用javascript。

You can do one of two things, 1. Pull all table information on load (slower way) and use javascript to show/hide certain elements of that query. Or 2. You can use AJAX to pull the data on command.

您可以执行以下两项操作之一:1。在加载时拉出所有表信息(较慢的方式)并使用javascript显示/隐藏该查询的某些元素。或者2.您可以使用AJAX来获取命令数据。

I would suggest learning javascript (e.g. use a framework like jQuery) to perform an AJAX call for this.

我建议学习javascript(例如使用像jQuery这样的框架)为此执行AJAX调用。