PostgreSQL & PHP Tutorials - Updating entries in your database

PHP »  Updating your data
PHP »  Starting Out »  Updating your data
PostgreSQL »  Updating your data
PostgreSQL »  Starting Out »  Updating your data

Posted By Chris Smith Posted on 12 Feb 2006, 05:41 AM
Viewing page 1 of 2
« | Back | Next | »

How do we update information already in our database? Here's a look at it.

We'll use the fetch page as a starting point:


<html>
    <body>
        <table border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td>
                    Friend ID
                </td>
                <td>
                    First Name
                </td>
                <td>
                    Surname
                </td>
                <td>
                    Email Address
                </td>
            </tr>
        <?php
        $db 
pg_connect('host=localhost dbname=contacts user=contacts password=firstphp');

        
$query "SELECT * FROM friends";

        
$result pg_query($query);
        if (!
$result) {
            echo 
"Problem with query " $query "<br/>";
            echo 
pg_last_error();
            exit();
        }

        while(
$myrow pg_fetch_assoc($result)) {
            
printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>"$myrow['id'], htmlspecialchars($myrow['firstname']), htmlspecialchars($myrow['surname']), htmlspecialchars($myrow['emailaddress']));
        }
        
?>
        </table>
        <br/>
        <form action="update.php" method="post">
            ID to Update : <input type="text" name="id" size="4" length="4" value="ID"><br>
            First Name : <input type="text" name="firstname" size="40" length="40" value="First Name"><br>
            Surname to update : <input type="text" name="surname" size="40" length="40" value="Surname"><br>
            <input type="submit" name="submit" value="Update It">
            <input type="reset" name="reset" value="Clear It">
        </form>
    </body>
</html>


This is our form that "updates" the information.

In this example, we're updating the first name, and the surname for the ID that we type in.

*Note, we're displaying the database as well so that we can find what we want to update easily - this isn't normally done*.

It passes what we type in the form to the "update" script.
Avg Rating: 4
Vote Count: 10


              

  New Reply


great! amer 05 Jun 2006 Reply


Want to post a comment? Fill in the details below.

Your Name  : 
Your Email  : 
Your Website  : 
Spam Check! Please answer this question  : 1 + 1 =
Comment