【发布时间】:2019-08-30 13:26:22
【问题描述】:
我在第 1 页 (cpu.php) 上的 phpmyadmin 数据库中创建了一个带有 PHP while 循环的表。但是,我无法仅将名称变量 ( $row['name'] ) 从一个选定的行发送到另一页,即第 2 页 (cpumore.php)。我尝试过使用会话,最接近的是给出的代码,它将我最后列出的 cpu 的名称从 cpu.php 页面输出到 cpumore.php 页面。我希望有人可以帮助我将变量从选定的行传递到第 2 页上的另一个表(cpumore.php)。非常感谢!
第 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>
第 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>
我希望能够单击将名称字段从一行发送到另一页的按钮。
【问题讨论】:
-
您必须在向页面发送任何内容之前启动会话。因此,将您的
session_start()移到两个脚本中的任何 HTML 输出上方 -
我也知道它只是术语,但
phpMyAdmin是用 PHP 编写的工具。不是 DBMS,而是称为MySQL,或者可能是mariaDB -
查看你的错误日志,你会看到类似
Headers already sent的消息 -
很高兴知道,这是有道理的。但我没有收到任何错误代码。我的代码运行顺利,问题是我无法从第一列中连续选出一个特定名称并将其移至第二页。唯一转到我的其他页面的是我表中的姓氏变量
-
$_SESSION['cpuName']=$row['name'];你在一个循环中。您将数据多次放入相同的变量$_SESSION['cpuName'][] = $row['name'];将创建一个cpuName数组,它们都将在某处
标签: php html mysql session html-table