0

I have some drop down lists that contain values and when the user selects the serial number and hits submit, it posts to Display.php and the tables on that page successfully fill with the values from the database. I've now added a table on the page with the dropdowns that creates a table filled with all entries from the database.

The table populates correctly, and there is a 'View' Hyperlink at the end. I want this to open Display.PHP also, but I want the tables on that page to fill from the database corresponding to the 'View' link selected, rather than the submit button. When I run the following code with the isset($_POST) line (from Display.php) and use the dropdown and Submit button option, it populates the tables with the record attached to the serial number from the drop down.

But when I comment that out and use the isset($_REQUEST) line, I just get a blank Display.php page except for my successful connection message. Is there an issue somewhere with the way I'm calling the request?

dashboard.php

<body>

<?php include 'connectionDB.php';
$query1 = "SELECT * FROM staging;";
$result1 = mysqli_query($connect,$query1);?>

<div class="dashboardTable">
<table style="border: 1px solid black;">
<tr>
    <th>Work Order Packet</th>
    <th>Work Order Number</th>
    <th>Date</th>
    <th>Utility</th>
    <th>Service Name</th>
    <th>Address</th>
    <th>Serial No.</th>
</tr>

<?php
  while($row = mysqli_fetch_array($result1)){
?>
<tr>
    <td><? echo $row['workOrderPacket'];?>&nbsp;</td>
    <td><? echo $row['workOrderNum'];?>&nbsp;</td>
    <td><? echo $row['date'];?>&nbsp;</td>
    <td><? echo $row['utility'];?>&nbsp;</td>
    <td><? echo $row['serviceName'];?>&nbsp;</td>
    <td><? echo $row['address'];?>&nbsp;</td>
    <td><? echo $row['serialNumber'];?>&nbsp;</td>
    <td><a href="Display.php?serialNumber=<? echo $row['serialNumber'];? >">view</a></td>
</tr>
<?}?>
</table>
</div>
</body>

Display.php

<?php
if(isset($_GET['serialNumber'])) 
{

$query1 = "SELECT * FROM staging WHERE stageID =  ".$_REQUEST['serialNumber'].";";
$result1 = mysqli_query($connect,$query1);

while($row = mysqli_fetch_array($result1)){
?>

////////HTML Tables here
H.Norman
  • 113
  • 2
  • 12
  • Have you tried passing a view Parameter in the url and then using: if(isset($_GET['something'])) { // Do your SQL Query here and display table } So if that variable is passed and found that if statement will be true and only it will fire. you can use this to make sure only certain segments get triggered when the page loads. – Brett Apr 12 '17 at 18:10
  • I haven't tried that, so how exactly would I modify the URL? – H.Norman Apr 12 '17 at 18:16
  • Are you getting the serialnumber in href, You don't need extra $query1 = "SELECT * FROM staging WHERE stageID = ' ".$_REQUEST['serialNumber']." ' "; – user3127648 Apr 12 '17 at 18:17
  • By default, `url?param=value` will be received under `$_GET` array. Clicking on submit won't pass anything to `Display.php`, since there are no form fields in the code. And when you click on the hyperlink, `$_POST` is irrelevant (unless the hyperlink click event is somehow manipulated by Javascript / jQuery and parameters are submitted as post). – Dhruv Saxena Apr 12 '17 at 18:18
  • 1
    You would pass the variable in the link like: something.com?var1=this?var2=this See here for more info: http://stackoverflow.com/questions/13102489/passing-multiple-variables-to-another-page-in-url Then on the display.php page you would have something Like: if(isset($_GET['var1']) == something){ } If var1 is equal to something it will run the if code. – Brett Apr 12 '17 at 18:19
  • 1
    You do not need Javascript to manipulate the url, it looks like you have your URL begin generated by the PHP Page Load after querying your Database, just make sure that when the url is created that it includes parameters to be passed and then make sure the display.php has a check mechanism for said passed parameters. – Brett Apr 12 '17 at 18:21
  • Also please note that queries like `$query1 = "SELECT * FROM staging WHERE stageID = ".$_REQUEST['serialNumber'];` are susceptible to [SQL Injection Attacks](http://stackoverflow.com/q/60174/2298301). Since you're already using `mysqli_*()` functions, please do also take a look at and consider implementing [MySQLi Prepared Statements](http://php.net/manual/en/mysqli.prepare.php) to secure your code from such risks. – Dhruv Saxena Apr 12 '17 at 18:24
  • @DhruvSaxena Thank you, the submit actually passes the values selected from the drop down box, but it doesn't relate to the table at all. So display.php pulls any value from the submit action that are listed in the drop down. I'm just now trying to get the same to happen if a link is selected from the table – H.Norman Apr 12 '17 at 18:30
  • @Brett thank you, I will try and modify my href URL with variable and see if I get a change – H.Norman Apr 12 '17 at 18:31
  • @Brett thank you, I tried with this: if(isset($_GET['var1'])=='serialNumber') and on the table my new URL: But still nothing other than my database connection message. The format looks right, doesn't it? – H.Norman Apr 12 '17 at 18:38
  • @H.Norman Could you post more of the code please? and can you verify the variable is displayed properly in the url – Brett Apr 12 '17 at 18:39
  • @H.Norman, The comment I made was pertaining to the code shown, which was to say that submit (i.e. `$_POST`) has no bearing on the hyperlink (i.e. `$_GET`) and also vice-versa. Although I read your remark _Unnecessary for this question_ in the code block, I didn't quite pay enough attention to process that dropdown's chosen value would indeed show up on the next page (for it being a part of the form). Now just so that I understand this properly, are you saying that you need both - the dropdown's value and also the parameter value from the hyperlink - in one single request on the next page? – Dhruv Saxena Apr 12 '17 at 18:40
  • @DhruvSaxena originally, I tried to have dropdowns as well as the table with link so that the user had an option, but now I've changed it to put the drop downs on one page and the table with links on the other, but depending on the option they can both still pull up and fill the table on Display.php. I just need to make Display.php fill accordingly whether with POST and submit, or with the link on the table – H.Norman Apr 12 '17 at 18:42
  • @DhruvSaxena So, the table on dashboard.php shows up fine and is populated by database, I just need the 'view' link for each row to pass that row's serial number field to display.php so that my SQL query on display.php can fill the tables there accordingly – H.Norman Apr 12 '17 at 18:48
  • @H.Norman That's what I'd been trying to tell.... Clicking on the hyperlink will indeed pass the serial number, although you will receive it in `$_GET` and without the dropdown value (assuming `display.php` was reached at after clicking on the hyperlink). Do you need _both_ the serial number as well as the dropdown box's value when you get to `display.php`? You see, that's going to create a problem with the current setup, as they're both mutually exclusive right now. One value will be passed as `$_GET` (serial number -> hyperlink event) and the other as `$_POST` (dropdown -> submit event). – Dhruv Saxena Apr 12 '17 at 19:11
  • @DhruvSaxena I don't need both, no. I've now moved the table with the hyperlink to another page, so this issue now doesn't involve the drop down. Display.php works fine with the dropdown. I copied the table code to a different page that ONLY shows the table with the hyperlink and then I copied the table code from display.php to only associate with that table and hyperlink. The current code with $_GET and the hyperlink just aren't working – H.Norman Apr 12 '17 at 19:14
  • @DhruvSaxena I've edited my code to better reflect the current situation – H.Norman Apr 12 '17 at 19:19
  • Thanks. I'd like to suggest a couple of tests to identify the problem then... (1) Could you try going to `display.php` directly from the address bar: `http://{project}/display.php?serialNumber=123` (provide an input value that you know is valid for processing). (2) In `display.php` try debugging by temporarily adding `print_r($_GET);` at the top and see if you receive anything in `$_GET` array when you hit the direct url. (3) If that works (and it ideally should), then the table and hyperlinks in `dashboard.php` will have to be closely examined to see what they really contain. – Dhruv Saxena Apr 12 '17 at 19:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141584/discussion-between-dhruv-saxena-and-h-norman). – Dhruv Saxena Apr 12 '17 at 19:29

0 Answers0