Jump to content

Delete a record from the database


Ulterior

Recommended Posts

Trying to write some code to add functions to database, just as a learning tool really. I got the add record to work but I can't delete a record from the page. The error message doesn't show for it either.

 <?phprequire_once 'login.php';$db_server = mysql_connect($db_hostname, $db_username, $db_password) or die("could not connect" . mysql_error());mysql_select_db($db_database, $db_server) or die("Couldn't select DB" . mysql_error());if (isset($_POST['Continent']) &&isset($_POST['Country']) &&isset($_POST['City'])){	$cont = get_post('Continent');	$count = get_post('Country');	$cit = get_post('City'); if (isset($_POST['delete']) && $cit != ""){	$query = "DELETE FROM test WHERE city='$cit'";	if(!mysql_query($query, $db_server))	echo "Delete failed: $query <br /> "	. mysql_error() . " <br />";}elseif (isset($_POST['add'])){$query = "INSERT INTO test VALUES" ."('$cont','$count','$cit')";	if(!mysql_query($query, $db_server))	echo "Add failed: $query <br /> "	. mysql_error() . " <br />"; }}echo <<<_END<form action='test.php' method='post' ><pre>Continent <input type='text' name='Continent' />Country <input type='text' name='Country' />City <input type='text' name='City' /><input type='hidden' name='add' value='yes' /><input type='submit' value='ADD RECORD' /></pre></form>_END; $query = "SELECT * FROM $db_database";$result = mysql_query($query); if (!$result) die("Database access failed" . mysql_error());$rows = mysql_num_rows($result);for ($p = 0 ; $p < $rows ; ++$p){$row = mysql_fetch_row($result); echo <<<_END<pre>Continent $row[0]Country $row[1]City $row[2]</pre><form action='test.php' method='post' /><input type='hidden' name='delete' value='yes' /><input type="hidden" name="City" value="$row[2]" /><input type ='submit' value='DELETE RECORD' /></form>_END;}mysql_close($db_server);function get_post($var){return mysql_real_escape_string($_POST[$var]);}?> 

Link to comment
Share on other sites

This if statement: if (isset($_POST['Continent']) &&isset($_POST['Country']) &&isset($_POST['City'])){ is not true when you submit the delete form, it doesn't contain all of those values.
Ah I thought about that briefly lol. So something like this below, is perfectly fine? Or is there a more efficient way of doing it?
 <?phprequire_once 'login.php';$db_server = mysql_connect($db_hostname, $db_username, $db_password) or die("could not connect" . mysql_error());mysql_select_db($db_database, $db_server) or die("Couldn't select DB" . mysql_error());if (isset($_POST['City']) &&    isset($_POST['delete']))    {        $cit = get_post('City');    if (isset($_POST['delete']) && $cit != ""){    $query = "DELETE FROM test WHERE city='$cit'";    if(!mysql_query($query, $db_server))     echo "Delete failed: $query <br /> "    . mysql_error() . " <br />";}}if (isset($_POST['Continent']) &&isset($_POST['Country']) &&isset($_POST['City'])){    $cont = get_post('Continent');    $count = get_post('Country');    $cit = get_post('City'); if (isset($_POST['add'])){$query = "INSERT INTO test VALUES" ."('$cont','$count','$cit')";    if(!mysql_query($query, $db_server))     echo "Add failed: $query <br /> "    . mysql_error() . " <br />"; }}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...