在通过表单进行 SQL 插入的同时填充会话用户 ID

瑞恩柯林斯

我想记录当前登录用户的用户 ID,该用户将数据输入到表单中,然后将数据记录到数据库表中

目前,插入查询正在运行并更新除用户 id 之外的所有内容。用户 id 变量肯定有效,因为我能够在同一页面上将其回显而不会出现任何问题

代码如下;

$barcode = $_POST['barcode'];
  $weight = $_POST['weight'];
  $userId = $_SESSION['userId'];

//error handling begins

  // check for any empty inputs.
  if (empty($barcode) || empty($weight)) {
    header("Location: ../record.php?error=emptyfields&barcode=".$barcode."&weight=".$weight);
    exit();
  }
  //we check if valid barcode entered. In this case ONLY letters and numbers.
  else if (!preg_match("/^[a-zA-Z0-9]*$/", $barcode)) {
    header("Location: ../record.php?error=invalidbarcode&barcode=".$weight);
    exit();
  }
  // check for an invalid weight. In this case ONLY numbers.
  else if (!preg_match("/^[0-9].*$/", $weight)) {
    header("Location: ../record.php?error=invalidweight&barcode=".$barcode);
    exit();
  }
  else {

        $sql = "INSERT INTO trimrecords (barcode, weight, createdby) VALUES (?,?,?);";
        // initialize a new statement using the connection from the dbh.inc.php file.
        $stmt = mysqli_stmt_init($conn);
        //  prepare  SQL statement AND check if there are any errors with it.
        if (!mysqli_stmt_prepare($stmt, $sql)) {
          // If there is an error send the user back to the record page.
          header("Location: ../record.php?error=sqlerror");
          exit();
        }
        else {

          // If there is no error continue the script!

          // bind the type of parameters we expect to pass into the statement, and bind the data from the user.
          mysqli_stmt_bind_param($stmt, "ssi", $barcode, $weight, $userId);
          // execute the prepared statement and send it to the database!
          // data is registered to Db at this stage
          mysqli_stmt_execute($stmt);
          // send back with success
          header("Location: ../record.php?record=success");
          exit();

        }
}
理查德·豪沙姆

添加session_start()到顶部,一切正常。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

SQL更新:通过在@之前添加+ ID对电子邮件地址进行重复数据删除

jQuery通过ID和表单组进行验证

来自插入行的IDENTITY ID的SQL插入ID

使用Stripe PHP API通过用户卡ID进行付款?

MS Access与MS SQL Server用户ID的无DNS连接/通过失败

SQL查询创建会话ID

通过两列生成SQL会话ID

Rails 5,找不到没有ID的用户。您可以通过表单传递对象吗?

如何通过xhr进行动态SQL插入?

使用用户表中的角色ID进行T-SQL查询以获取角色

通过servlet进行SQL插入或更新

通过位于另一个表中的用户tag_id进行表单搜索

如何将表单输入中的用户ID值与数据库中的记录进行匹配?

SQL,通过LAST_INSERT_ID()选择最后一个插入ID

通过用户ID和日期进行查询来获取对象ID

休眠会话-如何使用原始SQL插入并获取生成的ID?

用户ID的SQL更新

SQL / PHP对表单中的字段进行排序以进行插入

通过表单输入进行SQL注入攻击

LDAP:如何通过系统ID /密码绑定,然后通过用户ID /密码绑定进行授权?

通过ID对观察结果进行分组,同时还创建特征变量

通过用户表单替换自动填充的文本

将用户 ID 插入表单 db 提交 php

按月对 ID 重叠(通过内部联接)进行分组的 SQL 查询

Wordpress sql 插入表单

将用户在表单上选择的友好名称对应的 ID 传递给 SQL Server

通过 AJAX 进行 SQL 插入和删除查询

如何在通过 post 方法提交表单时跟踪用户 ID

通过 id 停止 chrome 自动填充/建议表单字段