如何从sql表PHP加载选定的单元格


How to load a selected cell from sql table PHP

对不起,我的代码有点长,请耐心等待。我试图通过php加载存储在我的SQL表单元格内的链接。用户可以单击复选框并选择要加载的链接。然而,我有一点不对劲。它加载SQL表中的所有链接,而不是用户选择的链接。我做错了什么?请指导。谢谢你!

$sql = "SELECT * FROM previousbroadcast ORDER BY id DESC";
$result=mysql_query($sql);
$count = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
      if (isset($_POST['re_b'])){
      $xml = simplexml_load_file($row['bclink']);   
   }
}

HTML就像

<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['id']; ?>">

OK,使用您提供的附加信息进行新的尝试:

此解决方案基于以下假设:

  1. 你得到的复选框的值在某种程度上与数据库中的字段有关
  2. 为简单起见,我将该字段命名为id -它可以在数据库中以不同的方式命名,但只有您知道…

话虽如此:

$sql = "SELECT * FROM previousbroadcast ORDER BY id DESC";
$result=mysql_query($sql);
$count = mysql_num_rows($result);
// in this array, we now have all the values of the checkboxes the user selected
$checkboxvalues = $_REQUEST['checkbox'];
while ($row = mysql_fetch_assoc($result)) {
   if (isset($_POST['re_b'])){
      // if the ID of this row is mentioned in the checkboxes the user clicked
      // then - and only then - load the file
      if (in_array($row[id], $checkboxvalues)) {
          $xml = simplexml_load_file($row['bclink']);   
      }
   }
}

好吧,你的代码写得不太好,不完整。但对于我可以看到的$del_record值是键$ I的复选框数组问题是你每次都在调用数据库,所以很容易丢失。

您应该将获取的数组存储在另一个数组中,然后在那里迭代,而不是向数据库发出大量请求,这将使您的代码运行得更快,您将对它有更多的控制。

我会让SQL为您做所有的过滤:

if (isset($_POST['re_b'])) {
  $checkboxvalues = isset($_REQUEST['checkbox']) ? $_REQUEST['checkbox'] : array();
  $myCheckboxes = array();
  foreach ($checkboxvalues as $cbv) {
    $myCheckboxes[] = mysql_real_escape_string($cbv);
  }
  $sql = "SELECT * FROM previousbroadcast WHERE id IN ('" . implode(",", $myCheckboxes) . "') ORDER BY id DESC";
  $result=mysql_query($sql);
  while ($row = mysql_fetch_assoc($result)) {
    $xml = simplexml_load_file($row['bclink']);
    // do something here with $xml or it will get overwritten
  }
}