Passing key field, id, from form to procedure

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am just starting to learn php and mysql.  I have a small php/html page and
database.  All it does when starting shows the name of each person with a
delete and edit link beside each name.  At the bottom of the list there is
an "Add Record" button.  If you select delete it takes out the name and
reloads the page.  If you select "Add Record" it brings up a form to add a
new name etc. with an "Add It" button under the form in which after
selecting INSERTs the new record and reloads the initial page.  The add and
delete parts work fine.

The edit record is the problem.  After selecting the edit beside the
appropriate name a form is brought up showing the current information for
that record with an "Update Record" button under the form.  This works fine
to here.  But when I select the "Update Record button it will go into the
"updaterecord" procedure of my php/html page but it has no id for it to
apply the UPDATE to.  So how can I have it pass the id to the "updaterecord"
procedure so it will apply the UPDATE to the appropriate record?  I tried
two different ways of getting the id for updaterecord as you can see below.
Following is my editrecord and updaterecord procedures:

if (isset($_GET['editrecord'])) { //edit record
	$zid = $_GET['editrecord'];
	$sql = "SELECT * FROM persinfo WHERE DID = $zid";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	$zid = $row["DID"];
	$zfname = $row["DFirst"];
	$zlname = $row["DLast"];
?>
	<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
	<input type=hidden name='DID' value="<? echo $zid; ?>">
	First name: <input type="text" name="zfname" 
		value="<? echo $zfname; ?>"/> <br>
	Last name: <input type="text" name="zlname" 
		value="<? echo $zlname; ?>"/> <br>
	<input type="submit" name="updaterecord" value="Update It" />
	</form>
<?php } ?>

<?php
if (isset($_GET['$updaterecord'])) {                      //update record
version 1
	$zid = $_GET['$updaterecord'];
		$sql = "UPDATE persinfo SET DFirst='$zfname',
		DLast = '$zlname' WHERE DID=$zid";
		$result = mysql_query($sql);
	}
}

//I also tried this

if ($updaterecord) {                                                //update
record version 2
	if ($zid) {
		$sql = "UPDATE persinfo SET DFirst='$zfname',
		DLast = '$zlname' WHERE DID=$zid";
		$result = mysql_query($sql);
	}
}
?>

If you want all of the code let me know.  I could send it to your personal
email address.  The whole thing is probably 130 lines of code.

Thanks for any help,
Doug Hull

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux