Jump to content

Check for existance


Escyll

Recommended Posts

WellI am struggling with my database.Now I have a database of students.I have a html form to delete a person.You have to fill in the name (voornaam in script), family naam (achternaam in script) and the studentID/studentkey (a unique number for a student) (leerlingnummer in script).This is the script I made:<?php$con = mysql_connect("localhost","root","appel");if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mysql", $con); mysql_query("DELETE FROM leerling WHERE Voornaam='$_POST[voornaam]' && Achternaam='$_POST[achternaam]' && Leerlingnummer='$_POST[leerlingnummer]'");print" Leerling succesvol verwijderd.";print"</br>";print" <a href=\"index.html\">Terug naar home.</a>";mysql_close($con);?>This works fine, but if i fill in a 'leerlingnummer' with another name and another family name, the page goes blank. Now i want to make a script that first check the database for existance of the record before trying to delete. If the data doesn't exist in a same record, i want an error, if it does i want the record to be deleted with a message.Thanks in advance (and i hope you understand it :))Greetz Jesper

Link to comment
Share on other sites

Well, I have solved a part of the problem, now i have this<?php$con = mysql_connect("localhost","root","appel");if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mysql", $con);$result = mysql_query("SELECT * FROM leerlingWHERE leerlingnummer=$_POST[leerlingnummer]");while($row = mysql_fetch_array($result)){ if ($_POST['voornaam']==$row['Voornaam'] && $_POST['achternaam'] ==$row['Achternaam']) { mysql_query("DELETE FROM leerling WHERE Leerlingnummer='$_POST[leerlingnummer]'"); print" Leerling succesvol verwijderd."; print"</br>"; print" <a href=\"index.html\">Terug naar home.</a>"; } else { print "Foutmelding!"; print "</br>"; print "</br>"; print "De gegevens die u invoerde konden niet worden terug gevonden als 1 persoon."; print "</br>"; print "Om zeker te zijn dat u alles goed invult kunt u de gegevens kopiƫren uit de selectie tabel om zo alles exact in te voeren."; }}mysql_close($con);?>There is one problem, if I select an studentkey that doesn't exist, I want the same error.Example:leerlingnummer Voornaam1 peter2 pan3 and4 someone5 elseIf I select leerlingnummer 6 (in my html form) I want an error, something like (this record does not exist)

Link to comment
Share on other sites

You don't need to check if it exists first, a delete statement will not cause an error if no records matched the where clause, it just won't delete anything. You can try to delete and then use the mysql_affected_rows function to determine if any records actually got deleted, and if they did show the success message or else show the error message.http://www.php.net/manual/en/function.mysq...fected-rows.php

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...