1

好的,所以我有一个简单的 php 选择脚本,我有下面的代码来定义变量。

$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");

   $numrows = mysqli_num_rows($result);
   $id = $row['id'];
   $total_min = $row['total_min'];
   $due_date = $row['due_date'];

然后我有:

echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i> </a> </td>";

这应该在单击时将变量从 php 选择脚本传递到 url <a>

我最终得到的是account/practiceSheets?id=没有实际身份的。我确定这是非常简单的事情,我很抱歉,因为我是 PHP 新手,也不知道该怎么称呼它才能在搜索引擎中获得可用的结果!如果有帮助,我的完整代码如下。

<?php
$con = mysqli_connect("50.63.106.47", "usd309bands", "MacBook1!", "usd309bands");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con, "SELECT * FROM practice_sheets

 WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");

 $numrows   = mysqli_num_rows($result);
 $id        = $row['id'];
 $total_min = $row['total_min'];
 $due_date  = $row['due_date'];

 if ($numrows == 0) {
   echo "<div class='alert alert-danger'>";
   echo "No Entries, See your instructor for details.";
   echo "</div>";
 } else {

   echo "<table class='mws-table table-striped table-hover'>";
   echo "<thead align='center'>";
   echo "<tr>";
   echo "<th>Sheet Number</th>";
   echo "<th>Total Minutes</th>";
   echo "<th>Due Date</th>";
   echo "<th>View</th>";
   echo "</tr>";
   echo "</thead>";
   echo "<tbody align='center'>";

   while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['total_min'] . "</td>";
    echo "<td>" . $row['due_date'] . "</td>";
    echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i>  </a> </td>";
    echo "</tr>";
    }
     echo "</tbody>";
     echo "</table>";

    mysqli_close($con);
    }
    ?>
4

3 回答 3

2
$result = mysqli_query($con,"SELECT * FROM practice_sheets
                                                    WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");

$numrows = mysqli_num_rows($result);
$id = $row['id'];

看不到$row是从哪里来的。

我认为您忘记将 MySQL 响应提取到关联的数组中。(你可以用mysqli_fetch_assoc()它来完成)

这是一个简单的例子:

$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$allRows = mysqli_fetch_assoc($result);
foreach($allRows as $row) {
  echo $row['id'].'<br/>';
}

这应该列出数据库请求中的所有 ID。

于 2013-07-29T03:43:42.967 回答
0

我变了

echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'></i></a></td>"

对此:

echo "<td> <a href='account/practiceSheets?id=" . $row["id"] . "'> <i class='icon-eye-open'> </i> </a> </td>";

并得到了想要的结果!

于 2013-07-29T04:10:14.250 回答
0

除非名称像这样存储在您的表中,否则此 sql 查询可能不会返回任何内容

第一后

"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'"

你可能想要这样的东西

$_SESSION[SESS_FIRST_NAME] . " " . $_SESSION[SESS_LAST_NAME]

返回一个名字

第一个最后一个

于 2013-07-29T03:43:46.703 回答