首页 > 解决方案 > 有没有办法从 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();         
}

标签: phpmysqlsqldatabaseforms

解决方案


您需要使用 anUPDATE而不是INSERTonuser_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();     

    }
....
....

推荐阅读