tags:

views:

212

answers:

4

What is supposed to happen is that a row gets inserted into a table, then that row's id is selected by means of comparison of the name and surname of that particular entry.

The id returned is then inserted into another table:

$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());

// Edit

Thanks guys for the important comments. Thanks for teaching me a few important points.

+5  A: 

You should be using mysql_insert_id instead of doing a select to get the ID.

Michael Haren
Thanks, Michael, I'll do that now.
Anriëtte Combrink
+1  A: 

Are you not trying to re-invent the wheel here?

Why not use

$result = mysql_query( "INSERT ... " );
$new_id = mysql_insert_id();
meouw
Thank you, meouw, I was not aware of that function.
Anriëtte Combrink
+11  A: 

Try:

$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();

And yes all those mysql_real_escape_string() are really important.

cletus
Or, use mysqli to avoid the death by a thousand manual escapes.
Sii
+1: comprehensive and safer solution
Michael Haren
mysqli unfortunately is unstable and I've given up using it. It doesn't work on LONGTEXT columns, which it took the PHP team 3 years to even acknowledge despite repeated attempts. And sometimes I find it just segfaulting on me where mysql_* works just fine.
cletus
@cletus: You've just described the development process for the whole PHP project. ;-)
Bill Karwin
+1  A: 

// sleep in order for query to finish

while ($result === false) {
   sleep(1);
}

i love how if $result really is false, the script will just stop

Actually if it is false, then the script stops immediately. ...ae','$voorstelle')") or die(mysql_error());So the sleep function completely useless.
Nick Whaley
Thanks Nick and nameht, I was not aware that this forum was there to break people down. I was hoping for some constructive comments.
Anriëtte Combrink
anriette, wow could you be any more rude?
You started it by demeaning my question (how inferior it may be to yours), I was asking for constructive comments, not sarcasm... And that was what I got from you, correct me if I'm wrong?
Anriëtte Combrink