简体   繁体   English

从上一页获取数据以传输到第二页PHP / MySQL

[英]Getting Data from Previous Page to Transfer to Second Page PHP/MySQL

so I'm querying some data from my MySQL database to fill out a drop down box dynamically, containing a Date and then a Unique ID for that date (the unique id is a primary key, and increments just in case the same date is in the database). 因此,我要查询MySQL数据库中的一些数据,以动态填写一个下拉框,其中包含一个日期,然后是该日期的唯一ID(唯一ID是主键,以防万一,如果相同的日期在数据库)。

I'm having trouble accessing the unique ID on the second page which would query the data for the rest of my data based on the date selected. 我在访问第二页上的唯一ID时遇到问题,该ID将根据选择的日期向我的数据查询其余数据。 That's kinda confusing so I'll try and break it down. 这有点令人困惑,所以我将尝试将其分解。

Table: 表:

=================================================== ================================================== =

|Unique ID|User ID|Weapon|Date|Location|Range|Score| |唯一ID |用户ID |武器|日期|位置|范围|得分|

This is for a marksmanship web page that tracks a user's score. 这是用于跟踪用户得分的枪法网页。

I have my drop down box accessing the data based on the user ID, and then it populates a dropdown box with 我有一个下拉框,用于根据用户ID访问数据,然后使用以下命令填充一个下拉框:

Date - Unique ID. 日期-唯一ID。

On the next page, I can get the date fine, but I'm unsure of how to get the Unique ID so I can select the rest of the data based on these two data entries. 在下一页上,我可以确定日期,但是我不确定如何获取唯一ID,因此我可以根据这两个数据条目选择其余数据。

Here's my code for this portion: 这是我这部分的代码:

  <!--- Form. Selecting match and all the data under it. !--->
<form action = "retrieved.php"  method = "post" style="margin-bottom:0px">

<select name="date"><option value="">-- Select A Date --</option>
<?php
    // While there is a row of data, print out the date to the drop down box.
    while($row = mysqli_fetch_array($result))
      {
          echo("</option><option value=". $row['date'] . ">". $row['date'] . " 
| Unique ID: " . $row['uniqueID'] . "
                </option>");
      }
 ?>
</select>
<input type="submit" name="submit" value="Retrieve Data" />
</form>

E.G. 
DropdownBox Options:
2014-08-16 | Unique ID: 5
2014-09-17 | Unique ID: 8
2014-09-17 | Unique ID: 10
2014-09-17 | Unique ID: 25

I thought about having Two Dropdown boxes where the user selects a date and then the second dropdown has the Unique ID which would update dynamically using AJAX or jquery or something, but I'm unfamiliar with those, and would still need to know how to access the data on the next page.... 我考虑过要有两个下拉框,用户可以在其中选择一个日期,然后第二个下拉框具有唯一ID,该ID会使用AJAX或jquery或其他东西动态更新,但是我不熟悉这些,并且仍然需要知道如何访问下一页上的数据。...

My second page needs something like this: 我的第二页需要这样的内容:

$query = "SELECT * FROM matches WHERE id = '".$id."' AND date = '".$date."' AND uniqueID = '".$uniqueID'"; but I can't get the uniqueID variable to work. I tried using $_SESSION and $_POST but nothing is saving to the next page.

Thanks in advance! 提前致谢!

EDIT: In the end, I want to be able to access all the data (weapon, range, score, etc) corresponding to the date and Unique ID selected. 编辑:最后,我希望能够访问与所选日期和唯一ID相对应的所有数据(武器,范围,分数等)。

You need to set the unique id in the value field of your option: 您需要在选项的值字段中设置唯一ID:

while($row = mysqli_fetch_array($result))
{
    echo(
 "</option><option value='". $row['date'] . ",".$row['uniqueId']."'>".                           </option>"
);
}

Then when retrieving the value on the next page use: 然后,在下一页上检索值时,请使用:

$valueArray = explode(',', $value);

This will yield an array like so: 这将产生一个像这样的数组:

['date', 'uniqueID']

Your query will look like this: 您的查询将如下所示:

$query = "SELECT * FROM matches WHERE uniqueId = ".$valueArray[0] ." AND date     =".$valueArray[1]."

Note 注意

Your query here is highly susceptible to SqlInjection 您的查询在这里很容易受到SqlInjection的影响

You should be using PDO or some other orm 您应该使用PDO或其他Orm

To manage your queries and mitigate the risks of malicious sql being injected. 为了管理您的查询并减轻注入恶意SQL的风险。

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

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