填写用户名会话,同时通过形式做SQL插入

问题描述 投票:0回答:1

我想从用户当前登录谁把数据输入这又被记录到数据库表的形式记录用户ID

目前插入查询运行并更新所有,但用户id..the用户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();

        }
}
php sql session-variables
1个回答
0
投票

添加session_start()顶端和所有的工作。

© www.soinside.com 2019 - 2024. All rights reserved.