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

时间:2011-08-25 18:48:45

标签: php mysql

抱歉,我的代码有点长,请耐心等待。我试图通过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']; ?>">

3 个答案:

答案 0 :(得分:1)

好的,这是一个新的尝试,其中包含您提供的其他信息:

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

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

    $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']);   
          }
       }
    }
    

答案 1 :(得分:1)

好吧,你的代码不是很好,它不完整。 但是我可以看到$ del_record值是带有键$ i的复选框数组 问题是你每次都在调用数据库,所以很容易迷失。

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

答案 2 :(得分:1)

我会让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
  }
}