0

我的目标是允许用户更新当前是否有多个成员处于活动状态。让我解释一下我到目前为止所做的事情。

这是我的表单/表格在浏览器中的样子:

表格示例

唯一可以修改的部分显然是带有复选框输入的“当前发布者”列。这是生成表格的代码:

public function selectAllMembers()
        {
            //Declerations
             $i =""; 

            $stmt = $this->dbh->prepare("SELECT UserId, Firstname, Lastname, Active, UserPriv FROM Members;"); //Select ALL data from table, Perhaps had a limit later 
            $stmt->execute(); //Execute the query
            $result = $stmt->fetchAll(PDO::FETCH_NUM); //Fetch all of the data

            //Display results
            echo "<form name=\"UpdateUser\" method=\"Post\" action=\"\">";
            echo "<table border='1'>
            <tr>
            <th>User Id </th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Current Publisher</th>
            <th>User Priveleges</th>
            </tr>
            <tr>";

            if($result == false) 
                echo "<td>For some reason, there are no memebers.</td></tr></table>"; //Just in case there are no members...:)

            else
            {
                foreach($result as $row)
                    foreach($row as $col)
                    {

                        $i++;

                        switch ($i) { 
                            case 4: //Is it a current publisher?
                                if($col == 0) 
                                    echo "<td><input type=\"checkbox\" class=\"current\" value=\"1\" ></td>";
                                else
                                    echo "<td><input type=\"checkbox\" class=\"current\" value=\"1\" checked ></td>";
                                break;

                            case 5: //Do they have user priveleges?
                                if($col == 0)
                                    echo "<td>No</td>";
                                else
                                    echo "<td>Yes</td>";
                                echo "</tr><tr>"; //start a new row
                                $i=0; //reset counter
                                break;

                            default:
                                echo "<td>$col</td>";

                        }

                    }
                echo "</tr></table>";
                echo "</form>";
            }
        }

switch 语句中的案例 4 和案例 5 确定它是真还是假(我在 MySQL 数据库中分别使用 1 或 0 来表示真或假)。同样在案例 4 中,我将 true 或 false 转换为输入复选框。

我想要做的是在提交时(我的表单还没有提交按钮),有一个功能可以比较“当前发布者”列的任何更改。如果有任何更改,请更新相应的表。现在,大家都很活跃。假设 Bob Smith 不再活跃,我希望他在表中的值更改为 false。

我在实现这一点时遇到了几个问题。

1) 如何修改我的复选框,使其可以与 PHP 进行比较/处理?这就是他们现在的样子:

<input type="checkbox" value="1">

我已经看到了一些关于如何提交多个复选框并随后使用 PHP 处理它们的示例。例如,我可以这样修改它们:

<input type="checkbox" name="member[]" value="1">

不过,我的想法是使用用户 ID 作为名称,因为它始终是唯一的。然后我还可以在 SQL 查询中使用它来识别正确的用户。

2) 如何根据输入更新我的表格?我应该简单地使用更新查询更新所有内容,还是只查找更改了哪些值?什么是最有效的?

任何帮助将不胜感激 - 特别是以示例的形式。如果有任何问题,只需将它们放在评论中。我希望我传达了我想做的事情的症结所在。

注意:如果未“选中”,也存在未提交复选框的问题。有什么解决方法吗?我已经看到了一个 javascript 解决方法,但这是假设用户启用了 javascript(我不知道谁不再启用,但我至少应该考虑这种可能性)。

4

2 回答 2

0

Considering the content, you must only print the "Firstname/Last Name/Current Publisher/User Privilege" and leaving the User ID;

Instead!, use the User ID as a link. I know you know how to create basic tables, you may add this kind of options

@testallmembers.php

    <table>
        <tr>
           <td><a href="edit.php?id=<?php echo $row['user_id']?>">Edit</a></td>
           <td>F_NAME</td>
           <td>L_NAME</td>
           <td>CURRENT_PUBLISHED</td>
           <td>USER_PRIVILEGE</td>
        </tr>
    </table>

Using the $row['user_id'], or whatever row index you have in your sql, should be editable.

@edit.php

In you edit.php you must use do this

    if(isset($_GET['id']) && !empty($_GET['id'])) {
        /*
        * use mysqli_real_escape_string to quote the id, for anti sql attacks
        * if you know PDO then use predefined.
        */

        //[SQL CODE HERE] - search the $_GET['id'] basing from the user_id

        /* then print the "EDITABLE FORM" 
        *  SAMPLE: <form method='post' action='edit.php'>
        *               <input type='text' name='f_name' value='<?php $row['f_name']?>' />
        *               <input type='submit' name='submit'/>
        *          </form>  
        */  

    //using $_POST['submit'] to get the EDITABLE form
    }elseif(isset($_POST['submit'])) {
        /*
        *   Save the values to your database, you also need to check first the input values for anti sql injection..
        */
    }
于 2013-06-19T18:52:52.617 回答
0

我认为您正在处理数据库结果(关联数组)。尝试这个:

$stmt = $this->dbh->prepare("SELECT UserId, Firstname, Lastname, Active, UserPriv FROM Members;"); //Select ALL data from table, Perhaps had a limit later 
$stmt->execute(); //Execute the query
$result = $stmt->fetchAll(PDO::FETCH_NUM); //Fetch all of the data

//..... your html.....

for ($i = 0; $i < count($result); $i++)
{
    if($result[$i]['currentPublisher'] == 0) 
    {
      echo "<td><input type=\"checkbox\" name=\"$result[$i]['currentPublisher']\" class=\"current\" value=\"0\" ></td>";
      // NOTE: The value need to be 0   
    }
    else
    {
       echo "<td><input type=\"checkbox\" name=\"$result[$i]['currentPublisher']\" class=\"current\" value=\"1\" checked ></td>";
    }
}

//..... your html.....
于 2013-06-19T18:57:14.433 回答