单击后更新mysql表值

时间:2014-11-28 12:17:22

标签: php

我有html表从mysql获取一些值。我需要通过用户ID更新该表中的特定行。

这是admin.php代码

    if (isset($_POST['btnApprove'])) {
         update_approve_user();
    }

<form action="./admin.php" method="post">
   <table class="table table-striped table-hover">
     <tr>
       <th>user id</th>
       <th>Approved?</th>
       <th></th>
     </tr>
     <?php get_users_func(); ?>
   </table>
</form>

我的PHP代码

<?php

function get_users_func() {

    $query = "SELECT user_id, approved FROM users";
    $run = mysql_query($query);
    if ($run = mysql_query($query)) {
        while ($query = mysql_fetch_array($run)) {
          echo '<tr>
                    <td name="uId">' . $query['user_id'] . '</td>
                    <td>' . $query['approved'] . '</td>
                    <td> <input type="submit" name="btnApprove" class="btn btn-sm btn-primary" value="Approve" /> </td>
                </tr>';
        }
    } else {
        echo 'Problem with function';
    }
}

function update_approve_user(){
    $user_id = $_POST['uId'];

    $query = "UPDATE users SET approved = 0 WHERE user_id = '{$user_id}'";
    if ($run = mysql_query($query)) {
        echo "Approved successfully !!";
        return TRUE;
    }
    else{
        echo "Problem with query";
        return FALSE;
    }
}
?>

我得到的错误是:

Undefined index: uId.

如何将此post方法传递给变量?

1 个答案:

答案 0 :(得分:1)

您需要添加名称为uId的文本元素:

您要将“uId”指定为<td>元素的名称,但不会将{umd作为<td>提交。

变化:

 echo '
        <tr> 
            <td name="uId">' . $query['user_id'] . '</td>
            <td>' . $query['approved'] . '</td>
            <td> <input type="submit" name="btnApprove" class="btn btn-sm btn-primary" value="Approve" /> </td>
            </tr>';

 echo '<tr> 
<td name="uId"><input type="text" name="uId" value="' . $query['user_id'] . '</td>
<td>' . $query['approved'] . '</td>
<td> <input type="submit" name="btnApprove" class="btn btn-sm btn-primary" value="Approve" /> </td>
</tr>';