传递。$ row [something]的值以执行另一个查询

时间:2015-10-31 05:52:37

标签: php mysql

我有一个仪表板表(MySQL),它将其值打印到一个php文件(dashboard.php)中的html表(仪表板ID隐藏),另外一列用于调用另一个php的按钮(deletedashboard.php)文件删除行。我需要的是点击我需要的按钮获取特定行的仪表板ID并将其转发到deletedashboard.php,这样我就可以执行查询来删除该行。

dashboard.php中的表格:

<?php
$result = mysql_query("select dashboardid, date, message, remarks from dashbaord");

if (mysql_num_rows($result) > 0) {
   echo "<table class=\"table table-striped table-bordered table-hover\"><tr><th>Date</th><th>Message</th><th>Remarks</th><th></th></tr>";

while($row = mysql_fetch_assoc($result)) {
   echo "<tr><td style=\"display:none;\">".$row["dashboardid"]."</td><td>".$row["date"]."</td><td>".$row["message"]."</td><td>".$row["remarks"]."</td><td><center><button formaction=\"deletedashboard.php\" type=\"button\" class=\"btn btn-success glyphicon glyphicon-ok\" ><strong> Delete</strong></button>";
                      }
   echo "</table>";
   } else {
   echo "0 results";
   }
?>

deletedashboard.php:

session_start();

$conn = mysql_connect("localhost:3306", "root", "") or die (mysql_error($conn));
mysql_select_db("mydb", $conn);

$ID = *I need to get the ID from the .$row["dashboardid"] from dashboard.php*

$query = "DELETE from dashboard WHERE dashboardid=$ID";
$result = mysql_query($query);

if (!$result) {
    echo mysql_error($conn);
} else {
    header("Location: ../dashboard.php");
}

2 个答案:

答案 0 :(得分:1)

使用href

<a href="deletedashboard.php?id=<?php echo $row['dashboardid']; ?>">Delete</a>

更新你的代码: -

while($row = mysql_fetch_assoc($result)) {
   echo "<tr><td style=\"display:none;\">".$row["dashboardid"]."</td>
   <td>".$row["date"]."</td><td>".$row["message"]."</td><td>".$row["remarks"]."</td>
   <td><center>
    <a href="deletedashboard.php?id=<?php echo $row['dashboardid']; ?>">Delete</a>
    }

deletedashboard.php

$ID = $_GET["id"];//get id
$query = "DELETE from dashboard WHERE dashboardid=$ID";

答案 1 :(得分:0)

更改

<?php
. . . 
. . .    
while($row = mysql_fetch_assoc($result)) {
   echo "<tr>";
   echo "<td style=\"display:none;\">".$row["dashboardid"]."</td>";
   echo "<td>".$row["date"]."</td><td>".$row["message"]."</td>";
   echo "<td>".$row["remarks"]."</td>":
   // just append $row['dashboardid'] with page url
   echo "<td><center><button formaction='deletedashboard.php?id=".$row['dashboardid']."' type='button' class='btn btn-success glyphicon glyphicon-ok' ><strong> Delete</strong></button>";
}
   . . . 
   . . . 
?>

id中使用 $_GET 抓住deletedashboard.php,如:

session_start();

$conn = mysql_connect("localhost:3306", "root", "") or die (mysql_error($conn));
mysql_select_db("mydb", $conn);

$ID = $_GET["id"];  // here you can get your $row['dashboardid']

$query = "DELETE from dashboard WHERE dashboardid=$ID";
$result = mysql_query($query);

if (!$result) {
    echo mysql_error($conn);
} else {
    header("Location: ../dashboard.php");
}

<强>更新

使用逗号附加所有ids,只需:

formaction='deletedashboard.php?ids=".$row['dashboardid'].",".$row['someother_id'].",".$someStatic_id.";

捕获这些ID :使用 explode()函数获取ID数组:

$ids[] = explode(',',$_GET['ids']);
$id1 = $ids[0];
$id2 = $ids[1];
// and so on...