DELETE code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

DELETE code

Post by mohson »

Currently the code below allows me to update the record set on my edit page - clicking the submit button saves the changes now if I was to implement a delete button how would I go about doing this I thought I could repeat this post code and enter a new query which would be

delete from people where person_id =$person_id (along those line)

but this obviously isnt the way (I think) anyone got any advice on what to do?

Code: Select all

if(isset($_GET['person_id'])){
			$person_id=$_GET['person_id'];
		}

		if ($_POST['Submit'] == 'Save'){

			foreach ($_POST as $formName => $phpName){
				$$formName = $phpName;
			}

				 $sql ="UPDATE people SET "
					. " salutation='"	.$salutation."'," 
					. " firstname='"	.$firstname	."'," 
					. " surname='"		.$surname."'," 
					. " organisation='" .$organisation	."'," 
					. " role='"			.$role."'," 
					. " address1='"	.	$address1."'," 
					. " address2='" .$address2."'," 
					. " city='" .$city."'," 
					. " telephone='" .$telephone."'," 
					. " mobile='" .	$mobile	."'," 
					. " fax='" .$fax."'," 
					. " dateoflastcontact='" .$dateoflastcontact."',"
					. " datecontactagain='" .$datecontactagain."',"
					. " notes='" .$notes."',"
					. " email='" .$email."',"
					. " org_id='" .	$org_id	."'"
					. " WHERE person_id='" . $person_id."'";

/// form properties...etc


. "						<input type=submit name=Submit
				value=Save>\n"
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

post this at the top of your page

Code: Select all

if($action == "delete"){
$delete = mysql_query("DELETE FROM table WHERE column = '$variable'") or die(mysql_error()); }
then where you want your delete button

Code: Select all

&lt;form action=\&quote;page.php\&quote; method=\&quote;post\&quote;&gt;
&lt;input type=\&quote;hidden\&quote; name=\&quote;action\&quote; value=\&quote;delete\&quote;&gt;
&lt;input type=\&quote;submit\&quote; value=\&quote;Delete\&quote;&gt;
&lt;/form&gt;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_POST['action'] not $action.. :|
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

thanks but if I want to delete the whole record would I just do the smae code but with a

Code: Select all

if($_POST['action'] == "delete"){
$delete = mysql_query("DELETE FROM people WHERE person_id = '$person_id'") or die(mysql_error()); }
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Code: Select all

<form action=\&quote;page.php\&quote; method=\&quote;post\&quote;>
<input type=\&quote;hidden\&quote; name=\&quote;action\&quote; value=\&quote;delete\&quote;>
<input type=\&quote;submit\&quote; value=\&quote;Delete\&quote;>
</form>
Are you sure this works??? because its just created two text boxes which contain \"delete\" in one box and \"Delete\" in the other
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i think you will need to keep a reference to the person_id too.

so you could add it to the form target -> <form action="whatver?person_id=$person_id" method="post">

or use another hidden field

-> <input type="hidden" name="person_id" value="$person_id"/>
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Ok this is what ive tried to make my delete function work.

Ive put this in:

Code: Select all

if($_POST['action'] == "delete"){
$delete = mysql_query("DELETE FROM people WHERE person_id = '$person_id'") or die(mysql_error()); }
and Ive created this button

Code: Select all

&lt;form action=\&quote;editpeople.php\&quote; method=\&quote;post\&quote;&gt;
&lt;input type=\&quote;hidden\&quote; name=\&quote;action\&quote; value=\&quote;delete\&quote;&gt;
&lt;input type=\&quote;submit\&quote; value=\&quote;delete\&quote;&gt;
&lt;/form&gt;
this produces two text boxes which contain the words delete


can someone please advice me on where I am gogin wrong,

In short I can already update my records and save them - I thought I would be able to add a delete function to the same edit page.

Above is the advice I was given which isnt working.


here is my code in full

lines 39-40 and 456 to 459 are the key bits of code

Code: Select all

<?php
		
/* Connecting, selecting database */
$link = mysql_connect("*****", "*****", "******")
   or die("Could not connect : " . mysql_error());
echo "";
mysql_select_db("contact_management_system") or die("Could not select database");
	
		if(isset($_GET['person_id'])){
			$person_id=$_GET['person_id'];
		}

		if ($_POST['Submit'] == 'Save'){

			foreach ($_POST as $formName => $phpName){
				$$formName = $phpName;
			}
			
					$sql ="UPDATE people SET "
					. " salutation='"	.$salutation."'," 
					. " firstname='"	.$firstname	."'," 
					. " surname='"		.$surname."'," 
					. " organisation='" .$organisation	."'," 
					. " role='"			.$role."'," 
					. " address1='"	.	$address1."'," 
					. " address2='" .$address2."'," 
					. " city='" .$city."'," 
					. " telephone='" .$telephone."'," 
					. " mobile='" .	$mobile	."'," 
					. " fax='" .$fax."'," 
					. " dateoflastcontact='" .$dateoflastcontact."',"
					. " datecontactagain='" .$datecontactagain."',"
					. " notes='" .$notes."',"
					. " email='" .$email."',"
					. " org_id='" .	$org_id	."'"
					. " WHERE person_id='" . $person_id."'";


			if ($_POST['action'] == "delete"){
$delete = mysql_query("DELETE FROM people WHERE person_id = '$person_id'") or die(mysql_error()); }

			
			echo "<br><div align=center>" . $sql . "<br>";
			mysql_query($sql) or die('could not insert' . mysql_error());

			echo  "		<div align=left><table width=70% border=1 cellpadding=10 align=center>"
				. "			<tr>"
				. "				<td width=40%>"
				. "					<font face=arial size=2><b>"
				. "					person_id"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $person_id . " </font>"
				. "				</td>"
				. "			</tr>"
				. "		<tr>"
				. "			<td>"
				. "					<font face=arial size=2><b>"
				. "					salutation"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $salutation . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					firstname"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $firstname . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					surname"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $surname . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					organisation"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $organisation . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					role"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $role . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					address1"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $address2 . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					city"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $city . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					postcode"
				. "					</font>"
				. "				</td>"
				. "					<td>"
				. "					<font face=arial size=2>"
				. "					" . $postcode . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "						telephone"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $telephone . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					mobile"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $mobile . "</font>"
				. "				</td>"
				. "			</tr>"
				."			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					fax"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $fax . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					dateoflastcontact"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $dateoflastcontact . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					datecontactagain"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $datecontactagain . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					notes"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $notes . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					email"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $email . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					org_id"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $org_id . "</font>"
				. "				</td>"
				. "			</tr>"
				. "		</table>"; 
		}
		
		$sql = "SELECT * FROM people WHERE person_id='" . $person_id . "'";
			echo "<br><div align=center>" . $sql . "<br>";
			$memb = mysql_query($sql) or die(mysql_error());
			$row = mysql_fetch_array($memb);

			echo  "\n"
				. "		</div>\n"
				. "		<form name=form2 method=post action=editpeople.html?person_id=" . $person_id . ">\n"
				. "			<table width=70% border=1 cellpadding=10 align=center>\n"
				. "				<tr>\n"
				. "					<td width=40%>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Person ID\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=person_id type=text id=person_id value='" . $row['person_id'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n" 
				. "				<tr>\n"
				. "					<td width=40%>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Salutation\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=salutation type=text id=salutation value='" . $row['salutation'] . "'></font>\n"
				. "				<tr>\n"
				. "					<td width=40%>\n"
				. "						<font face=arial size=2><b>\n"
				. "						First Name\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=firstname type=text id=firstname value='" . $row['firstname'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Surame\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=surname type=text id=surname value='" . $row['surname'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Organisation\n"
				. "						</font>\n"
				. "						</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=organisation type=text id=organsation value='" . $row['organisation'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Role \n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=role type=text id=role value='" . 
				$row['role']		. "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Address 1\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=address1 type=text id=address1 value='" . $row['address1'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Address2\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=address2 type=text id=address2 value='" . $row['address2'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						City\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=city type=text id=city value='" . 
				$row['city']	. "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Telephone\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=telephone type=text id=telephone value='" . $row['telephone'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Mobile\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=mobile type=text id=mobile value='" . 
				$row['mobile'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Fax\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=fax type=text id=fax value='" . 
				$row['fax'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Last Contact\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=dateoflastcontact type=text id=dateoflastcontact 								value='" . 
				$row['dateoflastcontact'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Contact Again\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=datecontactagain type=text id=datecontactagain 									value='" . 
				$row['datecontactagain'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Notes\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=notes type=text id=notes value='" . 
				$row['notes'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Email\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=email type=text id=email value='" . 
				$row['email'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						OID\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=org_id type=text id=org_id value='" . 
				$row['org_id'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td colspan=2>\n"
				. "						&nbsp\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td colspan=2 align=center>\n"
				. "						<input type=reset name=Reset value=Reset>\n"
				. "						<input type=submit name=Submit
				value=Save>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "			</table>\n"
				. "		</form>\n";
	//	}
		?>
<form action=\"editpeople.php\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"delete\">
<input type=\"submit\" value=\"delete\">
</form>
		</td>

<p>
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

anymore advice on my question anyone??
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

timvw wrote:i think you will need to keep a reference to the person_id too.

so you could add it to the form target -> <form action="whatver?person_id=$person_id" method="post">

or use another hidden field

-> <input type="hidden" name="person_id" value="$person_id"/>
has some useful advice..
Cronikeys
Forum Commoner
Posts: 35
Joined: Sun Jan 16, 2005 9:14 am

Post by Cronikeys »

Change

Code: Select all

?>
<form action=\"editpeople.php\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"delete\">
<input type=\"submit\" value=\"delete\">
</form>
        </td>
 
<p>
to

Code: Select all

echo "<form action=\"editpeople.php\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"delete\">
<input type=\"submit\" value=\"delete\">
</form>
        </td><p>";
 ?>
Post Reply