-1

I have created on table with a PHP while loop right from the phpmyadmin database fine on page 1 (cpu.php). However, I am not able to send just the name variable ( $row['name'] ) from one selected row to another page, page 2 (cpumore.php). I have tried using sessions and the closest I have come is the code given that outputs the name of my last listed cpu from the cpu.php page to the cpumore.php page. I hope someone can help me pass variables from a selected row to the other table on page 2 (cpumore.php). Thank you so much!

PAGE 1 CPU.PHP

<table id="myTable">

<thead>
    <tr>
      <th onclick="sortTable(0)">Name</th>
      <th onclick="sortTable(1)">Socket</th>
      <th onclick="sortTable(2)">Speed</th>
      <th>Price</th>
      <th></th>
     </tr>
</thead>

<?php
    session_start();
    if(isset($_POST['search'])) {
        $valueToSearch = $_POST['valueToSearch'];
        // search in all table columns
        // using concat mysql function
        $query = "SELECT name, socket, speed, price FROM `cpu` WHERE 
        CONCAT(`name`, `socket`, `speed`, `price`) LIKE 
        '%".$valueToSearch."%'";
        $search_result = filterTable($query);
    } else {
        $query = "SELECT name, socket, speed, price FROM `cpu`";
        $search_result = filterTable($query);
    }

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "root", "", "brodbuilds");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}
?>
<?php while($row = mysqli_fetch_array($search_result)):?>
     <tr>
     <form action="cpumore.php" method="post">
        <td><?php echo $row['name']; $_SESSION['cpuName']=$row['name'];?</td>
        <td><?php echo $row['socket'];?></td>
        <td><?php echo $row['speed'];?></td>
        <td><?php echo $row['price'];?></td>
        <td><input type="submit" name=submit id="cpuname" value="Add"/> 
        </div</td>
      </form>
      </tr>
<?php endwhile;?>
</table>

PAGE 2 CPUMORE.PHP

<table id="myTable">
 <thead>
 <tr>
  <th onclick="sortTable(0)">Name</th>
  <th></th>
 </tr>
</thead>
                <tr>
                    <td> <?php session_start(); echo $_SESSION['cpuName'];?> </td>
                </tr>
</table>

I would like to be able to click on a button that send the name field from one row to another page.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • You must start the session BEFORE sending anything to the page. So move your `session_start()` above any HTML output in both scripts – RiggsFolly Apr 09 '19 at 09:48
  • Also I know its just terminology, but `phpMyAdmin` is a tool written in PHP. It is not the DBMS, that is called `MySQL`, or possibly `mariaDB` – RiggsFolly Apr 09 '19 at 09:52
  • Look in your error log, you will see messages like `Headers already sent` – RiggsFolly Apr 09 '19 at 09:55
  • Good to know, that makes sense. But I am not receiving any error codes. My code runs smoothly the problem is I am not able to pick out one specific name in a row from the first column and move that to the second page. The only thing that goes to my other page is the last name variable in my table – Brent Rodden Apr 09 '19 at 10:39
  • `$_SESSION['cpuName']=$row['name'];` You are in a loop. You are placing data into THE SAME variable multiple times `$_SESSION['cpuName'][] = $row['name'];` will create an array of `cpuName` and they will all be in there somewhere – RiggsFolly Apr 09 '19 at 11:07
  • Awesome!! I think that might have done the trick. Do you know how I can call one thing out of that array then? – Brent Rodden Apr 09 '19 at 11:39
  • You will either have to know which one you want or write another loop on the receiving end – RiggsFolly Apr 09 '19 at 12:31
  • I just want the row that I click on with the submit button in that same row – Brent Rodden Apr 09 '19 at 16:49

2 Answers2

0

You missed '>' Try now

<td><?php echo $row['name']; $_SESSION['cpuName']=$row['name'];?></td>
Deepak A
  • 1,624
  • 1
  • 7
  • 16
0

I'm not too sure if I get the question, the best is to have a session manager, or alternatively on more add a variable as page?varname=value on page to use:

$Var = $_Get['varname'];
Damien Flament
  • 1,465
  • 15
  • 27
Mr Mash
  • 1
  • 2
  • I am looking to try and get the first row, "name" to be a variable I can pass to another page. When I try now it only pulls the last "name" in the table. Maybe this has something to do with creating the table with a while loop in php? – Brent Rodden Apr 09 '19 at 10:15
  • If you have more row labels do this $fnane = $row['fname']; $lname = $row['lname']; $age = $row['age']; //To pass do href = on second page use – Mr Mash Apr 09 '19 at 10:42