The form that shows the editable fields has a hidden field named 'DID'
having the value of $zid.  In your updaterecord procedure, you will have to
use the variable $_GET[DID] instead of $zid.  You can of course just add one
line of code:

$zid = $_GET[DID];

to help you out.  One other trick is to echo out your query before actually
running it so that you don't update all of your records by mistake.

Ryan Marks

-----Original Message-----
From: Hull, Douglas D [mailto:[EMAIL PROTECTED]
Sent: Friday, July 18, 2003 10:40 AM
To: Note To php mysql List (E-mail)
Subject: [PHP-DB] Passing key field, id, from form to procedure

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" />
<?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 (
To unsubscribe, visit:

PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to