douzhuo5671 2014-09-04 16:00
浏览 74

使用单个复选框更改多行的复选框属性

Good Morning,

Im working on a larger project but i tried to do a simple page in order to check my work. php/mysql newb here. Sorry! :)

What im trying to accomplish is ultimately having a single user page shown with rows of tasks from a table and a single check mark in order to say if the user has completed the task or not by checking or unchecking the box.

For testing purposes, I have set up a table with rows labled testid, testdata and testcheck. The testid a INT(2)AutoIncrement and Primary and Unique, testdata is a VARCHAR(30) and testcheck is a TINYINT(1). The auto increment isnt really important because I manually populated all the rows. I have 5 rows (for the array sake) consisting of 1-5 testid, "testdata1-5" for testdata and either a 0 or a 1 for testcheck. The table is functioning fine and the database can be queried.

Here is the code for the php start page:

<html>

<?php

include_once('init.php');

$query = mysql_query("SELECT testid, testdata, testcheck FROM testtable");

?>
<h1>Test form for checkmarks</h1>
<form method="POST" action="testover.php">
<table border="1">
    <tr>
        <td>
            Test ID
        </td>
        <td>
            Test Data
        </td>
        <td>
            Checked
        </td>
        <td>
            Test Check
        </td>
    </tr>
<?php
    while ( $row = mysql_fetch_assoc($query)) {
?>            
    <tr>
        <td>
            <?php echo $row['testid']; ?>
        </td>
        <td>
            <?php echo $row['testdata']; ?>
        </td>
        <td>
            <center><input type="checkbox" name="<?php $row['testid'] ?>" value="<?php $row['testcheck']; ?>" <?php if($row['testcheck']=='1'){echo 'checked';} ?>></center>
        </td>
        <td>
            <center><?php echo $row['testcheck']; ?></center>
        </td>
    </tr>
<?php } ?>

</table>
           <input type="submit" name="confirm" value="Confirm Details">
           <a href="testcheck.php"><input type="button" value="Home"></a>
</form>
</html>

Ive been going back and forth trying to use an array for the inputs name (name="something[]") or the "isset" parameter but that is where my knowledge is failing. Ive read countless articles both here and on other websites and I cant seem to find the right code to use. Most sites have rows with multiple check boxes or a different layout of their table.

I posted here to hopefully be pointed in some direction as to how to update the DB with these values of the check marks.

  • 写回答

2条回答 默认 最新

  • dongsu3654 2014-09-04 16:35
    关注

    I am trying to make sense of your code on what you want, and if I understand it right, you want those 5 rows outputted right?

    <?php 
    echo '<form action=''>;
    while ( $row = mysql_fetch_array($query)) {
    echo "<tr>";
    echo "<td>".$row['testid']."</td>";
    echo "<td>".$row['testdata']."</td>";
    echo "<td><input='checkbox' name='".$row['testid']."' value='".$row['testcheck']."'  ".if($row['testcheck']=='1'){echo 'checked';}."></td>";
    echo "<td>".$row['testcheck']."</td>";
    echo "</tr>";
    }
    echo "<input type='submit' name='confirm'>";
    echo "</form>";
    ?>
    

    and then updating. since the form has no action, it returns to itself. So now you can add on top of your page your new php and updating code.

    <?php
    if(isset($_POST['confirm'])){ // the name of your submit button
        // your update code here.
    ?>
    

    -- edit --

    Checkbox looping in php while posting to database

    I had a while ago the same problem as you. I needed to loop through checkboxes to post. Please check this topic for relevant information.

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决