如何插入两个单独的表并使用相同的ID链接它们?

时间:2015-01-14 15:36:10

标签: php html mysql

我有两个表(客户端和药物),我已经创建了一个表单,用于插入用户在这些表中输入的信息。客户可以使用许多以前的药物(缩短为药物),因此我为多种药物使用了一个单独的表格,我现在需要将它们联系起来。 client_id是自动递增的,因此是drug_id,但是我的drug_client_id不是,我需要它与client_id相同(否则他们不会链接)。我不太确定如何做到这一点,如果有人可以帮助我,那么我将非常感激 我无法让它在一个php文件上工作,所以我不得不将客户端和药物插入语句放在两个不同的文件中;所以,如果你能帮助我,也许只让他们进入一个,那么我会更加感激哈哈。
请看下面的代码并帮助我,非常感谢。

<html>
<head>
<title> Access Community Trust </title>
</head>
<body>

<form action="addclientwo.html" method="post">

First name: <input type="text" name="first_name">
<br>

Drug use: <input type="text" name="drug1">
<input type="text" name="drug2">
<input type="text" name="drug3">

<input type="submit" value="Submit">
</form>
</body>
</html>


<?php
Require("dbconnect.php");
//establishes connected to database

$first_name = $_POST['first_name'];

$stmt = $dbh->prepare("INSERT INTO client (first_name) VALUES (:first_name)");

$stmt->bindParam(':first_name', $first_name);

$stmt->execute();
?>

<?php
Require("dbconnect.php");

$drug1 = $_POST['drug1'];
$drug2 = $_POST['drug2'];
$drug3 = $_POST['drug3'];

$stmt = $dbh->prepare("INSERT INTO drug (drug1, drug2, drug3) VALUES (:drug1, :drug2, :drug3)");

$stmt->bindParam(':drug1', $drug1);
$stmt->bindParam(':drug2', $drug2);
$stmt->bindParam(':drug3', $drug3);

$stmt->execute();
?>
是的,我正在为一个无家可归的宿舍做这件事,所以这些客户中的一些人过去可能吸毒(他们需要记录在案);我没有做某种药品经销店哈哈

3 个答案:

答案 0 :(得分:3)

当你在数据库中插入一些东西时,你可以得到它的ID:

$lastId = $dbh->lastInsertId();

所以你可以这样做:

$first_name = $_POST['first_name'];
$stmt = $dbh->prepare("INSERT INTO client (first_name) VALUES (:first_name)");
$stmt->bindParam(':first_name', $first_name);

$stmt->execute();

$drug_client_id = $dbh->lastInsertId();

$stmt = $dbh->prepare("INSERT INTO drug (drug1, drug2, drug3, drug_client_id) VALUES (:drug1, :drug2, :drug3, :drug_client_id)");

$stmt->bindParam(':drug1', $drug1);
$stmt->bindParam(':drug2', $drug2);
$stmt->bindParam(':drug3', $drug3);
$stmt->bindParam(':drug_client_id', $drug_client_id);

$stmt->execute();

你需要做Require(“dbconnect.php”);只有一次在顶部

答案 1 :(得分:0)

看起来你没有将drug_client_id与药物1,2,3一起传递到第二张表中。

因此,在第一次插入客户端表后,您必须检索此客户端的client_id,例如$ drug_client_id。 然后你必须使用这个值$ drug_client_id来设置你的secoind语句(当然在检查之后,第一次保存是成功的)

这样的事情:

$stmt = $dbh->prepare("INSERT INTO drug (drug1, drug2, drug3, drug_client_id) VALUES (:drug1, :drug2, :drug3, :drug_client_id)");

$stmt->bindParam(':drug1', $drug1);
$stmt->bindParam(':drug2', $drug2);
$stmt->bindParam(':drug3', $drug3);
$stmt->bindParam(':drug_client_id', $drug_client_id);

$stmt->execute();

如果希望这有帮助。

答案 2 :(得分:0)

首先,您需要阐明您的数据库模型:

表架构

--- USER ---
PK id_user <smallint> <autoincrement>
   name ... -- and others fields needed


--- DRUG ---
PK id_drug <tinyint> <autoincrement>
   name    <varchar>

--- USER_USE_DRUG ---
FK id_user <smallint>
FK id_drug <tinyint>
   usage   <datetime>

PHP端(PDO)

1 - 当用户使用药物时,您必须将其插入表user_use_drug中,例如:

// You know id_user & id_drug
$req = $db->prepare("INSERT INTO user_use_drug(id_user, id_drug, usage) 
VALUES (:id_user, :id_drug, NOW())");
$req->bindParam('id_user', $id_user, PDO::PARAM_INT);
$req->bindParam('id_drug', $id_drug, PDO::PARAM_INT);
$req->execute();


// You know user name (unique) but you know id_drug
$req = $db->prepare("INSERT INTO user_use_drug(id_user, id_drug, usage) 
VALUES (
    (SELECT id_user FROM user WHERE name = :uname), 
    :id_drug, 
    NOW()
)");
$req->bindParam('id_user', $id_user, PDO::PARAM_INT);
$req->bindParam('id_drug', $id_drug, PDO::PARAM_INT);
$req->bindParam('uname', $uname, PDO::PARAM_STR);
$req->execute();


// You know user name (unique) and drug name (unique too)
$req = $db->prepare("INSERT INTO user_use_drug(id_user, id_drug, usage) 
VALUES (
    (SELECT id_user FROM user WHERE name = :uname), 
    (SELECT id_drug FROM drug WHERE name = :dname), 
    NOW()
)");
$req->bindParam('id_user', $id_user, PDO::PARAM_INT);
$req->bindParam('id_drug', $id_drug, PDO::PARAM_INT);
$req->bindParam('uname', $uname, PDO::PARAM_STR);
$req->bindParam('dname', $dname, PDO::PARAM_STR);
$req->execute();

2 - 如果您想从用户那里获取信息,可以举一些例子:

// Get list of drugs used if you know the id_user 
// + retrieve his informations too 
// + retrieve datas on drug
$req = $db->prepare("SELECT * FROM user_use_drug 
LEFT JOIN user ON user.id_user = user_use_drug.id_user
LEFT JOIN drug ON drug.id = user_use_drug.id_drug
WHERE user_use_drug.id_user = :id_user");
$req->bindParam('id_user', $id_user, PDO::PARAM_INT);
$req->execute();

// show every drug used
while($drug = $req->fetch(PDO::FETCH_ASSOC)){
    echo $drug['name'];
}

// Variant to above
$req = $db->prepare("SELECT * FROM user_use_drug 
NATURAL JOIN user
NATURAL JOIN drug
WHERE user_use_drug.id_user = :id_user");
$req->bindParam('id_user', $id_user, PDO::PARAM_INT);
$req->execute();


// Count the number of drugs used if you know the id_user
$req = $db->prepare("SELECT COUNT(*) as nbr FROM user_use_drug 
WHERE id_user = :id_user");
$req->bindParam('id_user', $id_user, PDO::PARAM_INT);
$req->execute();

$rep = $req->fetch();
echo $rep['nbr'];


// Request to get the list of drugs used if you don't 
// know the id_user but his name (unique)
$req = $db->prepare("SELECT * FROM user_use_drug 
WHERE id_user = (SELECT id_user FROM user WHERE name = :name");
$req->bindParam('id_user', $id_user, PDO::PARAM_INT);
$req->bindParam('name', $name, PDO::PARAM_STR);
$req->execute();

// show every drug used
while($drug = $req->fetch(PDO::FETCH_ASSOC)){
    echo $drug['name'];
}

3 - 删除客户端

您应该在数据库中定义外键/约束,并在删除用户时添加ON DELETE / ON UPDATE操作以置于user_use_drug中的行的自动删除。否则你必须手动处理它:删除id_user所在的所有行,然后删除用户。