Page 1 of 1

Editing Records - updating deletes a firstname field

Posted: Wed Mar 16, 2005 10:44 am
by mohson
Ok, I have finally managed to sort out my edit records page I only have two problems.

1. When I click the submit button on a record to update, it updates the record but deletes the first name of the updated record

2. For some reason the edit people page prints out the query which is running i.e it will print the select and update statements on the web page.


Could someone please have a look at my code and tell me why the two above points are occuring.

p.s I submitted this on another thread but for some reason it was marked as solved when it wasnt - dont know why?


OK heres the 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'] == 'Submit'){

			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."'";
 
			
			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>"
				. "					" . $address1 . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					Address 2"
				. "					</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 . "'";
			
			$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"
				. "						Surname\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=Submit>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "			</table>\n"
				. "		</form>\n";
	//	}
		
?>


		</td>

<p>

Posted: Wed Mar 16, 2005 8:04 pm
by cbrian
1. When I click the submit button on a record to update, it updates the record but deletes the first name of the updated record
This is because of

Code: Select all

$sql ="UPDATE people SET // .... and so on
You should be using INSERT. UPDATE changes the entry, INSERT will enter something new.[/quote]

Posted: Fri Mar 18, 2005 4:38 am
by mohson
The reason why I am using UPDATE and not INSERT is because this is an UPDATE query on my edit page , I dont want to add something new I want to update the original data.

OK can someone have a look at my code and perhaps help with my original question which is:

when I update a record I.e add some more data to a particular record - it updates fine but for some reason it deletes the firstname field - can some one see where I am going wrong???

Posted: Fri Mar 18, 2005 5:11 am
by timvw
you could start with:

- echo the query before you use it...
- using mysql(i)_real_escape_string
- generating valid html..
- stop writing 10+ times the same code...

Posted: Fri Mar 18, 2005 5:24 am
by mohson
so why is it deleting the firstname?

Posted: Fri Mar 18, 2005 6:10 am
by mohson
simple mistake - on the update statement I had spelt the variable incorrectly i.e firstname = $firsName but it should have been $firstname capital not needed.

You know sometimes you just dont see your own errors because you have been looking at the screen so long, those are the times when it helps to post your code on a forum and someone elses eye may spot where youve gone wrong - not a technical issue but just a simple typo.

Thanks for your extremely friendly and ever so useful advice though timvw, your reply made the world of difference