php - 有没有办法从 2 个不同的表单中插入数据并插入到列中的 1 行中?
问题描述
我有具有以下列的用户表:user_id、username、first_name、last_name、email、token、password、location、phone。
我在两个不同的页面上有 2 个表格。1.registration.php 2.user_info.php。
在registration.php 中,我得到用户的电子邮件、用户名和密码。在 user_info.php 我得到用户的名字、姓氏、国家、电话。
我想在 1 行中插入两个表单数据。所以有什么办法吗?
现在用我的代码。它将两种表单中的信息插入数据库,但它在每个表单中插入 2 行不同的数据。
这是我的registration.php
<?php
if (isset($_POST['signup-submit'])) {
$url = "https://www.google.com/recaptcha/api/siteverify";
$data = ['secret' => "[xxxx]", 'response' => $_POST['token'], 'remoteip' => $_SERVER['REMOTE_ADDR']];
$options = array('http' => array('header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data)));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$res = json_decode($response, true);
if ($res['success'] == true) {
require("dbh.inc.php");
require("functions.php");
$username = escape($_POST['username']);
$email = escape($_POST['email']);
$token = bin2hex(random_bytes(50));
$password = escape($_POST['password']);
$passwordRepeat = escape($_POST['confirm_password']);
if (empty($username) || empty($email) || empty($password) || empty($passwordRepeat)) {
header("Location: ../registration.php?error=emptyfields");
exit();
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL) || !preg_match("/^[a-zA-Z0-9]*$/", $username)) {
header("Location: ../registration.php?error=invalidmailuid");
exit();
} elseif (strlen($username) <= '6') {
header("Location: ../registration.php?error=usernamecheck");
exit();
} elseif (strlen($password) <= '8') {
header("Location: ../registration.php?error=passwordcheck");
exit();
} elseif ($password !== $passwordRepeat) {
header("Location: ../registration.php?error=passwordverify");
exit();
} else {
$sql = "SELECT username, email FROM users WHERE username = ? AND email = ?";
$stmt = mysqli_stmt_init($connection);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../registration.php?error=sqlerror");
exit();
} else {
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultCheck = mysqli_stmt_num_rows($stmt);
if ($resultCheck > 0) {
header("Location: ../registration.php?error=usermailtaken");
exit();
} else {
$sql = "INSERT INTO users(username, email, password, token, joined) VALUES(?, ?, ?, ?, now())";
$stmt = mysqli_stmt_init($connection);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../registration.php?error=sqlerror2");
exit();
} else {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssss", $username, $email, $hashed_password, $token);
mysqli_stmt_execute($stmt);
header("Location: ../user_info.php");
exit();
}
}
}
}
mysqli_stmt_close($stmt);
mysqli_close($connection);
} else {
header("Location: ../registration.php?error=captcha");
exit();
}
} else {
header("Location: ../registration.php?error=emptyfields");
exit();
}
这是我的user_info.php
<?php
if (isset($_POST['profile-submit'])) {
require("dbh.inc.php");
require("functions.php");
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$location = $_POST['location'];
$phone = $_POST['phone_number'];
if (empty($first_name) || empty($last_name) || empty($location) || empty($phone)) {
header("Location: ../user_info.php?error=emptyfields");
exit();
} else {
$sql = "INSERT INTO users(first_name, last_name, location, phone) VALUES(?, ?, ?, ?)";
$stmt = mysqli_stmt_init($connection);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../user_info.php?error=sqlerror");
exit();
} else {
mysqli_stmt_bind_param($stmt, "sssi", $first_name, $last_name, $location, $phone);
mysqli_stmt_execute($stmt);
header("Location: ../index.php?signup=success");
exit();
}
}
mysqli_stmt_close($stmt);
mysqli_close($connection);
} else {
header("Location: ../user_info.php?error");
exit();
}
解决方案
您需要使用 anUPDATE
而不是INSERT
onuser_info.php
INSERT 添加新行。https://dev.mysql.com/doc/refman/8.0/en/insert.html
INSERT 将新行插入到现有表中。
UPDATE 连续修改数据。https://dev.mysql.com/doc/refman/8.0/en/update.html
UPDATE 是修改表中的行的 DML 语句。
当你这样做时,UPDATE
你需要添加一个WHERE
子句来只更新你想要的行。您通常使用主键来执行此操作,在这种情况下我假设它是user_id
.
您可以使用在查询运行mysqli_insert_id($connection)
后获取最后插入的 id 。INSERT
我建议然后将其存储在一个$_SESSION
变量中,然后user_info.php
通过 POST 或 GET 访问它而不是传递。这样,另一个用户不能只在 GET 或 POST 请求中输入一个 ID 并更新另一个用户的信息。这是一些指导您的代码。
注册.php
//start the session
session_start();
...
...
} else {
mysqli_stmt_bind_param($stmt, "sssi", $first_name, $last_name, $location, $phone);
mysqli_stmt_execute($stmt);
$_SESSION['user_id'] = mysqli_insert_id($connection);
header("Location: ../index.php?signup=success");
exit();
}
}
....
....
用户信息.php
....
....
if (empty($first_name) || empty($last_name) || empty($location) || empty($phone) || !isset($_SESSION['user_id')) {
header("Location: ../user_info.php?error=emptyfields");
exit();
} else {
$sql = "UPDATE users SET first_name = ?, last_name = ?, location = ?, phone =? WHERE user_id = ?";
$stmt = mysqli_stmt_init($connection);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../user_info.php?error=sqlerror");
exit();
} else {
mysqli_stmt_bind_param($stmt, "sssi", $first_name, $last_name, $location, $phone, $_SESSION['user_id']);
mysqli_stmt_execute($stmt);
header("Location: ../index.php?signup=success");
exit();
}
....
....
推荐阅读
- sql - SQL如何在分隔符后拆分字符串
- vb.net - 将某些扩展名类型的文件从用户选择的文件夹移动到新文件夹
- authentication - 我应该将 cognito 身份验证令牌存储在我的数据库中吗?
- c++ - 设计帮助 - 用于处理不同类型消息的通用接口的设计模式
- c++ - 如何在 C/C++ 中使用 main() 中的两个单独函数 - 一个 lesserThan 和另一个 GreaterThan - 围绕一个值对一个单链表进行分区
- sql - 如何编写查询以使用 SQL 选择在标签 ID 数组中具有标签 ID 的元素?
- php - 主题、样式、图像在 MAMP 本地开发 WP 网站上不起作用
- php - 有没有办法像这样从 MySQL 解析 JSON
- layout - SwiftUI:无法在 ForEach 循环中调用 .frame
- python - Python:我无法修复 TypeError:'str' 对象不可调用