-2

基本上,我的表单必须向已在页面中加载的表格中检查的注册用户添加积分。当用户点击提交按钮时,被选中的注册用户将获得额外的积分。我的问题是我不知道如何检索循环表中检查的行。这是我的代码。

<form name="blah" method="POST" action="">
<?php
include("mysql_config.php");
// INITIAL OUTPUT WHEN THE PAGE IS LOADED
echo "<table class='sample'><tr>
<td></td>
<td><b>User ID</b></td>
<td><b>Username</b></td>
<td><b>First Name</b></td>
<td><b>Last Name</b></td>
<td><b>Credits</b></td>
</tr>";
$q = "SELECT * FROM users_credits";
$res = mysql_query($q);
$num_rows = mysql_num_rows($res);
for($i=0; $i<$num_rows; $i++){
    $db_field = mysql_fetch_assoc($res);
    $reg_id = $db_field['reg_id'];
    $credits = $db_field['credits'];
    $q2 = "SELECT username, fn, ln FROM reg_users WHERE reg_id = $reg_id";
    $res2 = mysql_query($q2);
    $db_field2 = mysql_fetch_assoc($res2);
    $username = $db_field2['username'];
    $fn = $db_field2['fn'];
    $ln = $db_field2['ln'];
    echo "<tr>
        <td><input type='checkbox' value='$i' name='add_credit'></td>
        <td>$reg_id</td>
        <td>$username</td>
        <td>$fn</td>
        <td>$ln</td>
        <td>$credits</td>
        </tr>";
}
?>
<input type="submit" name="save" value="Save" >
</form>

提前感谢会帮助我的人。:)

4

3 回答 3

0

编写一个要调用的 Javascript 函数,当单击按钮时 -

function updateFilter(){
        var tmp="";
        checkboxes = document.getElementsByName('<checkbox id>');
        for(var i in checkboxes){
            if(checkboxes[i].checked)
                tmp=tmp + "," + checkboxes[i].value;
        }
        tmp=tmp.substring(1);   
        parent.window.location.href="<file to open>.php?p=" + tmp;
    }

您的复选框 HTML 如下所示 -

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

理想情况下,这将连接复选框的所有值并将其作为参数传递(在本例中为 PHP),并且可以在下一页中将其解码为

$r=mysql_query("select * from product_master where Product_ID in (" . $p.")");

或使用拆分。我建议复选框的值应该是 ID 而不是文本。

于 2012-11-11T05:40:21.683 回答
0

将您的复选框更改为:

<input type='checkbox' value='$reg_id' name='add_credit[]'>

然后,当您发布到您的操作时,您将拥有所有已检查用户的 reg_id 的列表。这将是一个您可以使用 $_POST['add_credit'] 访问的数组

于 2012-11-11T05:41:44.317 回答
0

请将您的页面更新为:

<form name="blah" method="POST" action="update.php">
<?php
include("mysql_config.php");
// INITIAL OUTPUT WHEN THE PAGE IS LOADED
echo "<table class='sample'><tr>
<td></td>
<td><b>User ID</b></td>
<td><b>Username</b></td>
<td><b>First Name</b></td>
<td><b>Last Name</b></td>
<td><b>Credits</b></td>
</tr>";
$q = "SELECT * FROM users_credits";
$res = mysql_query($q);
$num_rows = mysql_num_rows($res);

    while($db_field = mysql_fetch_assoc($res))
    {
    $reg_id = $db_field['reg_id'];
    $credits = $db_field['credits'];
    $q2 = "SELECT username, fn, ln FROM reg_users WHERE reg_id = $reg_id";
    $res2 = mysql_query($q2);
    $db_field2 = mysql_fetch_assoc($res2);
    $username = $db_field2['username'];
    $fn = $db_field2['fn'];
    $ln = $db_field2['ln'];
    echo "<tr>
        <td><input type='checkbox' value='".$reg_id."' name='add_credit[]'></td>
        <td>$reg_id</td>
        <td>$username</td>
        <td>$fn</td>
        <td>$ln</td>
        <td>$credits</td>
        </tr>";
}
?>

将 update.php 中的表单操作设置为

<?php
$users=$_REQUEST['add_credit'];

if(is_array($users))
{
foreach($users as $key=>$val)
{

echo $val;

}

}
?>

在这里,您将获得数组中的用户 ID。

于 2012-11-11T05:41:49.623 回答