CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
Tutorial - Updating the data already in your database
Well, we can now view our information, and add new information into our database. How do we change the information already there?
<html>
<body>
<?php
$db = pg_connect('dbname=friends');
$query = 'SELECT * FROM friends';
$result = pg_exec($db, $query);
if (!$result)
$numrows = pg_numrows($result);
$row=0;
printf ('<table border=1>');
printf ('<tr><td>ID</td><td>First Name</td><td>Surname</td></tr>');
do
while ($row < $numrows);
printf ('</table><br>');
pg_close();
?>
<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.
This is our 'update' script -
<html>
<body>
<?php
$db = pg_connect('dbname=friends');
$query = 'UPDATE friends SET firstname='$firstname', surname='$surname' where id='$id'';
$result = pg_exec($db, $query);
if (!$result)
printf ('These values were updated in the database - %s %s %s',$id, $firstname, $surname);
pg_close();
?>
</body>
</html>
Again, we'll break this down again for understanding.
$db = pg_connect('dbname=friends');
This connects to our database, as in every script we've written so far.
$query = 'UPDATE friends SET firstname='$firstname', surname='$surname' where id='$id'';
This updates the firstname and the surname for the ID number we type in.
The syntax for the command is -
UPDATE database SET table1=value1, table2=value2, table3=value3, where tableX=valueX
So, in the database 'friends', we're setting the table names with the new information we put in. As long as we keep one
value in our table the same, and have another one to refer to, it doesn't matter how many fields are in the rest of the database,
we can update them all at once. That's why we're also using an ID field at the start. If we didn't use the ID field, we would
only be able to update either the firstname or the surname each time, not both at the same time.
$result = pg_exec($db, $query);
This updates the database with our new information.
if (!$result)
If it doesn't work, it prints 'ERROR' and exits the script so nothing else happens. This will be explained in a bit
more detail in the error section of our tutorial.
printf ('These values were updated in the database - %s %s %s',$id, $firstname, $surname);
If it works, it displays the information we entered just to make sure we got it right. Normally, we'd have it display a message
such as 'successfully updated'. For our purposes, just displaying this message is sufficient.
pg_close();
?>
</body>
</html>
We have the usual end of script, to close the database connection,
end the PHP script, and finish the HTML code.
This is a very simple example of what we can do. If we ran the script that shows all of the data in the database again, we'd see
that we've updated the firstname and the surname for the ID that we put in. The data comes in at the bottom of the list.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 825
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved