PHP MySQL - 用户输入名称并插入相应的ID

标签 php mysql

我有两个表,membersgamesmembers中是member_idfirst_namelast_name等数据

我想做的是为 games 创建一个表单,用户可以在其中输入参与成员的名字和姓氏(在一个字符串中,而不是单独的)和一些 PHP代码查询此名称,找到相应的 ID 并将其存储起来。当然,member_idgames中是一个外键,但是用户不会知道成员(member)的id,他们只会知道他们的名字。

如果有人能解释我将如何着手这样做,我将不胜感激。

表格:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form</title>
</head>
<body>
<form action="action.php" method="post">
    <p>
        <label for="date">Date:</label>
        <input type="date" name="date" id="date">
    </p>
    <p>
        <label for="duration">Duration:</label>
        <input type="time" name="duration" id="duration">
    </p>
        <p>
        <label for="member_id">Member Name:</label>
        <input type="text" name="member_id" id="member_id">
    </p>
    <input type="submit" value="Submit">
</form>
</body>
</html>

行动:

<?php

// database connection
include 'pdo_config.php';

try {
// new pdo connection
$conn = new PDO($dsn, $user, $pass, $opt);

// prepare statement and bind parameters
$stmt = $conn->prepare("INSERT INTO games (date, duration, member_id)
  VALUES (:date, :duration, :member_id)");

$stmt->bindParam(':date', $date);
$stmt->bindParam(':duration', $duration);
$stmt->bindParam(':member_id', $member_id);

// post data
$date = $_POST['date'];
$duration = $_POST['duration'];
$member_id = $_POST['member_id'];

// execute statement
$stmt->execute();

// success or error message
echo "New record created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}

$conn = null;

?>

最佳答案

这应该有效。

要求用户在表单中输入成员名称而不是成员 ID。然后对数据库进行第一次查询,以从成员名称中获取成员 ID。

请记住,从名称中搜索成员 ID 并不是一个好主意,因为您可能有多个同名成员。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form</title>
</head>
<body>
<form action="action.php" method="post">
    <p>
        <label for="date">Date:</label>
        <input type="date" name="date" id="date">
    </p>
    <p>
        <label for="duration">Duration:</label>
        <input type="time" name="duration" id="duration">
    </p>
    <p>
        <label for="member_name">Member Name:</label>
        <input type="text" name="member_name" id="member_name">
    </p>
    <input type="submit" value="Submit">
</form>
</body>
</html>

<?php

// database connection
include 'pdo_config.php';

try {
// new pdo connection
$conn = new PDO($dsn, $user, $pass, $opt);

// post data
$date = $_POST['date'];
$duration = $_POST['duration'];

// Note that the explode only works well if user inputs one blank space to separate the name
// You can try to improve the separation method or better use two different inputs in the form

$nameArray = explode(" ", $_POST['member_name']);
$first_name = $nameArray[0];
$last_name = $nameArray[1];

$statement = $conn->prepare("SELECT member_id FROM members WHERE first_name = :first_name AND last_name = :last_name");
$statement->execute(array(':fisrt_name' => $first_name, ':last_name' => $last_name));
$row = $statement->fetch();

$member_id = $row['member_id'];


// prepare statement and bind parameters
$stmt = $conn->prepare("INSERT INTO games (date, duration, member_id)
  VALUES (:date, :duration, :member_id)");

$stmt->bindParam(':date', $date);
$stmt->bindParam(':duration', $duration);
$stmt->bindParam(':member_id', $member_id);

// execute statement
$stmt->execute();

// success or error message
echo "New record created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}

$conn = null;

?>

关于PHP MySQL - 用户输入名称并插入相应的ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40698375/

相关文章:

php - 分组并显示调查数据

php - echo mysql DATE(table_date) 未定义常量

php - 使用php在数据库中插入日期格式

php - 当重定向到页面时,css 文件、js、图像等未加载

php - 从日期时间列获取数据

php - Laravel 5.1 到 5.2 升级报错

php preg_match 从字符串中查找困难的 JSON

java - 如何在JAVA中将纪元转换为mySQL时间戳

php - 找不到检查用户是否已存在的方法

mysql - 失败 : SemanticException Column line_id Found in more than One Tables/Subqueries