Can someone please review this shortcode?

The row is supposed to be inserted into the table, then this row id is selected by matching the first and last names of that particular record.

The returned id 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. Thank you for teaching me a few important points.

-2


a source to share


4 answers


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 these mysql_real_escape_string () are really important .

+11


a source


You should use mysql_insert_id

instead of making a selection to get the ID.



+5


a source


Are you trying to reinvent the wheel here?

Why not use

$result = mysql_query( "INSERT ... " );
$new_id = mysql_insert_id();

      

+1


a source


// sleep in order for query to finish

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

      

I like it as if the $ result was indeed false, the script would just stop

+1


a source







All Articles