【问题标题】:Passing a value through a button in PHP通过 PHP 中的按钮传递值
【发布时间】:2019-03-19 15:11:01
【问题描述】:

我想通过 delete 按钮将$record['userId'] 值传递给另一个 php 函数。我怎样才能做到这一点?我要删除响应当前userId的列。

if($result_set){
  $table = '<table class="table">';
  $table .='<tr><th>Record No.</th><th>Race ID</th><th>User ID</th><th>Message</th><th>Date &</th><th></th><th></th></tr>';
  $count = 1;
  while($record = mysqli_fetch_assoc($result_set)){
    $table .= '<tr>';
    $table .= '<td>' . $count . '</td>';
    $table .= '<td>' . $record['userId'] . '</td>';
    $table .= '<td>' . $record['raceId'] . '</td>';
    $table .= '<td>' . $record['message'] . '</td>';
    $table .= '<td>' . $record['createdAt'] . '</td>';
    $table .= '<td>
      <div class="btn-group" role="group" aria-label="Record Actions" style="float:left" >
        <a href="inc/edit.php" class="btn btn-success">Edit</a>                                        
      </div>
    </td>';  

    $table .= '<td>
      <div class="btn-group" role="group" aria-label="Record Actions" style="float:left" >                                        
        <a href="inc/delete.php" class="btn btn-danger">Delete</a>
      </div>
    </td>';  
    $table .= '</tr>';
    $count = $count+1;
  }

  $table .='</table>';
  echo $table;
} else {
  echo "Query Failed"; 
}

【问题讨论】:

  • 在表单或查询字符串中隐藏输入。
  • 它给了我这个错误“语法错误,意外的'userId'(T_STRING)”
  • 是什么给了你这个错误,在哪一行?
  • 你确定只用PHP来删除吗?或者可以使用 AJAX / JQuery 吗?

标签: php button


【解决方案1】:

我认为你必须使用这个按钮;

<a href="inc/delete.php?userId="'.$record['userId'].'" class="btn btn-danger">Delete</a>

现在在inc/delete.php 使用$_GET['userId'] 接收用户ID

所以,总数将是;

 if($result_set){

                        $table = '<table class="table">';
                        $table .='<tr><th>Record No.</th><th>Race ID</th><th>User ID</th><th>Message</th><th>Date &</th><th></th><th></th></tr>';
                        $count = 1;
                        while($record = mysqli_fetch_assoc($result_set)){
                            $table .= '<tr>';
                            $table .= '<td>' . $count . '</td>';
                            $table .= '<td>' . $record['userId'] . '</td>';
                            $table .= '<td>' . $record['raceId'] . '</td>';
                            $table .= '<td>' . $record['message'] . '</td>';
                            $table .= '<td>' . $record['createdAt'] . '</td>';
                            $table .= 
                            '<td>
                                <div class="btn-group" role="group" aria-label="Record Actions" style="float:left" >
                                    <a href="inc/edit.php" class="btn btn-success">Edit</a>                                        
                                </div>
                            </td>';  

                            $table .= 
                            '<td>
                                <div class="btn-group" role="group" aria-label="Record Actions" style="float:left" >                                        
                                    <a href="inc/delete.php?userId="'.$record['userId'].'" class="btn btn-danger">Delete</a>
                                </div>
                            </td>';  
                            $table .= '</tr>';
                            $count = $count+1;
                        }

                        $table .='</table>';

                        echo $table;
                    }
                    else{
                        echo "Query Failed"; 
                    }

【讨论】:

  • 它给出一个空值。地址栏显示“localhost/typr/inc/delete.php?userId=”。
  • 警告! 这对CSRF attacks 开放。如果有人使用其他 ID 创建了指向 delete.php 的直接链接,然后让管理员单击它,它将删除该用户。你应该在做这样的事情时添加 CSRF-tokens。
  • @shayanmalinda 那么$record['userId'] 没有设置?
  • @AaronJonk 已设置
  • @shayanmalinda 那不可能,它会出现在表格中吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 2012-02-22
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多