[英]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.