简体   繁体   English

如何将数据从一个表拉到另一个表

[英]How to pull data from one table into another

I have a form (called suggestions) on my site that users fill in and submit.我的网站上有一个表单(称为建议),用户填写并提交。 The data from the form is pulling through properly however I want to extract the email of the user too when they submit - this data is currently in the 'users' table (user will have given this info on sign up) in the database and I'm not sure how to access it?表单中的数据正在正确提取,但是我想在用户提交时也提取用户的 email - 此数据当前位于数据库中的“用户”表中(用户将在注册时提供此信息),我'不确定如何访问它?

Here's what I have so far:这是我到目前为止所拥有的:

$table = 'suggestions';
$id = (isset($_SESSION['u_id']) ? $_SESSION['u_id'] : null);
$email =
$optionOne = '';
$optionTwo = '';

$suggestions = selectAll($table);

if (isset($_POST['new-suggestion'])) {
  global $conn;

  $id;
  $email;
  $optionOne = $_POST['optionOne'];
  $optionTwo = $_POST['optionTwo'];
  $sql = "INSERT INTO $table (user_id, email, option_1, option_2) VALUES (?, ?, ?, ?)";

  if (!empty($optionOne) && !empty($optionTwo)) {
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('ssss', $id, $email, $optionOne, $optionTwo);
    $stmt->execute();

  } else {
    echo "All options must be entered";
  }
}

User ID is extracted via session so I'm thinking I could use this to get their email?用户 ID 是通过 session 提取的,所以我想我可以用它来获取他们的 email? Not sure what the '$email' variable should equal to get me this.不确定 '$email' 变量应该等于什么才能让我得到这个。

$id = (isset($_SESSION['u_id']) ? $_SESSION['u_id'] : null);

if ( NULL !== $id ) {
  // Assumes user table is named "users" and email column is named "email":
  $sql = "SELECT email FROM users WHERE id = ?";
  $stmt = $conn->prepare($sql);
  $stmt->bind_param('s', $id);
  $stmt->execute();
  $row = $stmt->fetch();
  $email = $row[ 'email' ];
} // else no id specified in session

This is missing a whole bunch of error checking but the general idea should work.这缺少一大堆错误检查,但总体思路应该可行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从一个表中提取数据,并将其插入到另一个表中的列? - How do i pull data from one table and insert it into a column in another table? PHP从一个表中获取ID,并从另一表中提取与这些ID相对应的数据 - PHP take id's from one table and pull data that corresponds with those id's from another table 如何在 php-mysql 中使用 pdo 从一台服务器连接(拉取数据)到(插入 - 创建表)另一台服务器,可能是获取? - how to connect ( pull data ) from one server to ( insert into - create table ) another server using pdo in php-mysql , possibly fetch ? 在PHP中,如何从一个表中获取信息以另一表的形式获取信息 - in PHP, how do I get info from one table to pull up in a form for another 如何从一个表中获取数据并插入到php中的另一个表中 - how to fetch data from one table and insert into another table in php MySql中如何将列数据从一张表复制到另一张表? - How to Copy Column data from one table to another table in MySql? 需要从一个数据库中提取大量数据并插入另一个数据库 - Need to pull large set of data from one database and insert into another 如何从一个SQL表中的数据减去PHP中的另一个SQL表? - How to subtract data from one sql table from another in php? sql like search如何根据搜索结果从一个表中提取数据 - sql Like search how pull out data from one table based on search result 如何将数据从一个表动态插入到另一个表? - How should I insert data from one table to another dynamically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM