应该发生的是将一行插入表中,然后通过比较该特定条目的名称和姓氏来选择该行的id。
然后将返回的id插入另一个表中:
$result = false;
$result = mysql_query("INSERT INTO `reunie` (`naam`, `van`,`nooiensvan`, `selfoon`, `email`, `bywoon`, `metgesel_naam`, `spesifieke_onderwysers`, `spesifieke_musiek`, `bydrae`, `voorstelle`) VALUES ('$naam','$van','$nooiensvan','$selfoon','$email','$bywoon','$metgesel','$spesifiekeOnderwysers','$musiek','$bydrae','$voorstelle')") or die(mysql_error());
// sleep in order for query to finish
while ($result === false) {
sleep(1);
}
$result = mysql_query("SELECT `id` FROM `reunie` WHERE `naam` = '$naam' AND `van` = '$van' ORDER BY `id` DESC LIMIT 1") or die(mysql_error());
//编辑
谢谢你们的重要评论。感谢教给我一些重点。
答案 0 :(得分:11)
尝试:
$name = mysql_real_escape_string($name);
$van = mysql_real_escape_string($van);
$nooiensvan = mysql_real_escape_string($nooiensvan);
$selfoon = mysql_real_escape_string($selfoon);
$email = mysql_real_escape_string($email);
$bywoon = mysql_real_escape_string($bywoon);
$metgesel = mysql_real_escape_string($metgesel);
$spesifiekeOnderwysers = mysql_real_escape_string($spesifiekeOnderwysers);
$musiek = mysql_real_escape_string($musiek);
$bydrae = mysql_real_escape_string($bydrae);
$voorstelle = mysql_real_escape_string($voorstelle);
$query = <<<END
INSERT INTO reunie
(naam, van, nooiensvan, selfoon, email, bywoon, metgesel_naam,
spesifieke_onderwysers, spesifieke_musiek, bydrae, voorstelle)
VALUES
('$naam','$van','$nooiensvan','$selfoon','$email','$bywoon','$metgesel',
'$spesifiekeOnderwysers','$musiek','$bydrae','$voorstelle')
END;
$result = mysql_query($query) or die(mysql_error() . ' - ' . $query);
$id = mysql_insert_id();
是的,所有那些mysql_real_escape_string()非常重要。
答案 1 :(得分:5)
您应该使用mysql_insert_id
而不是选择获取ID。
答案 2 :(得分:1)
你是不是想在这里重新发明轮子?
为什么不使用
$result = mysql_query( "INSERT ... " );
$new_id = mysql_insert_id();
答案 3 :(得分:1)
// sleep in order for query to finish
while ($result === false) {
sleep(1);
}
我喜欢如果$ result真的是假的,脚本就会停止