gpt4 book ai didi

php - 将数据添加到两个以id连接的表中

转载 作者:行者123 更新时间:2023-11-29 12:54:50 24 4
gpt4 key购买 nike

嗨,我有这段代码将数据插入 mysql 数据库到表 radnici 中:

 try {        
$STH = $db->prepare("INSERT INTO radnici (ime_prezime, jmbg, grad, ulica, posta, telefon, uloga, email, user_id) VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9)");

$STH->bindParam(':1', $_POST['ime']);
$STH->bindParam(':2', $_POST['jmbg']);
$STH->bindParam(':3', $_POST['grad']);
$STH->bindParam(':4', $_POST['ulica']);
$STH->bindParam(':5', $_POST['posta']);
$STH->bindParam(':6', $_POST['telefon']);
$STH->bindParam(':7', $_POST['pozicija']);
$STH->bindParam(':8', $_POST['email']);
$STH->bindParam(':9', $user_id);

//HERE I NEED TO INSERT DATA TO TABLE WORKHOURS BUT CONNECTED WITH JUST ADDEDED ID
//SO here I NEED query something like this // INSERT INTO radnici (ID, ID_radnici, ime_prezime, jmbg,... here ID_radnici is =with ID from table radnici of added data


$STH->execute();

} catch (PDOException $e) {
echo $e->getMessage();
}
echo "<p>Data submitted successfully</p>";

所以现在当我添加这个时,我需要添加到这个表workhours数据中,但与刚刚添加的数据的ID连接......

所以在第二个表中我有:ID,ID_radnici(这里我必须添加ID_radnici与表radnici上的ID相同的值),value,user_id

我写了这个但不起作用:更新:

 try {        
$STH = $db->prepare("INSERT INTO radnici (ime_prezime, jmbg, grad, ulica, posta, telefon, uloga, email, user_id) VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9)");

$STH->bindParam(':1', $_POST['ime']);
$STH->bindParam(':2', $_POST['jmbg']);
$STH->bindParam(':3', $_POST['grad']);
$STH->bindParam(':4', $_POST['ulica']);
$STH->bindParam(':5', $_POST['posta']);
$STH->bindParam(':6', $_POST['telefon']);
$STH->bindParam(':7', $_POST['pozicija']);
$STH->bindParam(':8', $_POST['email']);
$STH->bindParam(':9', $user_id);

$orderID = $db -> lastInsertId();

$STH1 = $db->prepare("INSERT INTO workhours (ID_radnika) values($orderID)"); // where $value is the value you want to insert...
$STH->execute();
$STH1->execute();






} catch (PDOException $e) {
echo $e->getMessage();
}
echo "<p>Data submitted successfully</p>";

最佳答案

使用lastInsertId将执行您想要的操作:

$orderID = $db -> lastInsertId();

关于php - 将数据添加到两个以id连接的表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24233907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com