【问题标题】:Get specific data after output everything from database从数据库中输出所有内容后获取特定数据
【发布时间】:2014-11-14 08:11:46
【问题描述】:

我在这里有一个表,其中包含我数据库中的所有数据。在每一行的最后,我放了一个值为 1 的复选框,以更新我的数据库中的“接受”状态,默认为 0。

我的问题是,勾选复选框的值应仅在按下提交按钮后更新其行中的条目中的“接受”状态。

所以基本上我需要检查复选框是否被勾选,如果勾选,复选框行中的“接受”状态的值为 1。为此,我认为我需要获取“match_id”行。

这是它在网站上的外观:

这是桌子:

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db('lr') or die(mysql_error());

$result = mysql_query("SELECT * FROM `challenge`") or die(mysql_error());

echo "<table class='match-table'>";
      echo "<thead><tr> <th><h1>match_id</h1></th> <th><h1>Team</h1></th><th><h1>Accept Status</h1></th><th><h1>Accept</h1></th> </tr></thead><tbody>";
            while($row = mysql_fetch_array($result)){
        echo "<tr><td>"; 
        echo $row['match_id'];
          echo "</td><td>"; 
        echo $row['team'];
          echo "</td><td>";
        echo $row['accept'];
          echo "</td><td>";
      ?>
      <form action="" method="post">
      <input type="checkbox" name="challenge_accept" value="1"/>
      <?php   
        echo "</td></tr>";  
      }
      echo "</tbody></table>";
            ?>
      <input type="submit" value="Save"/>
      <input type="reset" value="Delete"/>
      </form>

有什么建议吗?

【问题讨论】:

  • 您可以执行 ajax 发布请求,然后逐行更新您的数据库..

标签: php mysql checkbox html-table row


【解决方案1】:

您的 HTML 完全无效,打开了多个 &lt;form&gt;,页面末尾只有一个 &lt;/form&gt;。您还将标签拆分为垃圾,例如

<tr><td><form></td></tr>
[many repeats]
</table>
</form>

您的表单字段也不包括任何识别实际点击了哪一行的方法,例如&lt;input type="submit" name="selected_row" value="&lt;?php echo $row['id'] ?&gt;" /&gt;。就目前而言,您已经对值 1 进行了硬编码,因此无论您吐出多少按钮中的哪一个,它们都会将 1 发送回您的代码。

【讨论】:

  • 这样我就可以通过 mysql_fetch_row 函数确定实际点击了哪一行,或者有更好的解决方案吗?
  • 不使用 fetch_row(),但是你需要在每一行的按钮中嵌入一些东西,就像我上面展示的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 2013-09-02
相关资源
最近更新 更多