【问题标题】:How can i disable selected checked in other to prevent another user from selecting it again如何禁用选中的签入其他以防止其他用户再次选择它
【发布时间】:2019-11-28 06:45:08
【问题描述】:

这就是我将复选框保存到数据库的方式。我不希望复选框在数据库中存在两次

如果一个用户选择了一个复选框,另一个用户应该不能再次选择同一个复选框

【问题讨论】:

  • 将您的代码作为文本而不是图像放在问题中。
  • 您尝试过什么调试问题?为什么不在持久化之前过滤现有的复选框?

标签: php laravel


【解决方案1】:

从截图中我了解到,您不希望在同一部门的同一单位分配多个讲师。

所以在创建allocated 行之前,您需要检查这些条目是否已经存在。

$data = compact('course_id', 'department', 'unit');
// check the department and unit already assigned or not. 
if(!allocated::where($data)->exists()){
   $data['lecturers_name'] = $lecturers_name;
   allocated::create($data);
}

【讨论】:

  • 放入if语句后,检查记录是否存在,只存储一个值,
【解决方案2】:

对于初学者,您没有提供任何代码示例。所以我后面的例子不会基于你的代码。

您可以禁用复选框,但如果有人真的想要,这很容易绕过。检查器可用于更改 html,以便启用禁用的复选框。当然,您可以在后端验证这一点。

我会选择仅显示具有其他人尚未选择的值的复选框。我的示例包含这两种解决方案。

// create connection with your database.
$conn = new mysqli($servername, $username, $password, $dbname);

// array with values that can be picked.
$values = ['Apple', 'Banana', 'Lemon'];

// loop trough the array.
foreach( $values as $fruit ) 
{
    // select query -> check if someone else already choose the fruit.
    $sql = "SELECT chosen_fruit FROM friends WHERE chosen_fruit = " . $fruit;

    // execute the query.
    $result = $conn->query($sql);

    // if no results are found, meaning nobody else have picked this fruit yet.
    if ($result->num_rows == 0) {
        // enabled checkbox that is free to use
        echo '<input type="checkbox" name="chosen_fruit" value="' . $fruit . '">'
    } else {
        // disabled checkbox because this fruit has already been chosen.
        echo '<input type="checkbox" name="chosen_fruit" value="' . $fruit . '" disabled>'
    }
}

编辑:我在发布答案后看到了您的编辑。

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多