CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
Tutorial - Adding Data to your database
To do this with PHP, we need to use forms. Here is a basic form that we can use to do what we want.
<html>
<body>
<form action='add.php' method='post'>
First Name : <input type='text' name='firstname' size='40' length='40'
value='First Name'><BR>
Surname : <input type='text' name='surname' size='40' length='40'
value='Surname'><BR>
<input type='submit' name='submit' value='Submit'>
<input type='reset' name='reset' value='Clear It'>
</form>
</body>
</html>
<form action='add.php' method='post'>
This line 'posts' the information to the PHP script script named in 'action', so change the action to the
name of the script you save it as.
As I said, this is a very basic form. If you want some help with forms and HTML, let me know and I'll explain this part more.
This is our 'action' script -
<html>
<body>
<?php
$db = pg_connect('dbname=friends');
$query = 'INSERT INTO friends (id, firstname, surname) values (nextval('friends_id_seq'), '$firstname', '$surname')';
$result = pg_exec($db, $query);
if (!$result)
printf ('These values were inserted into the database - %s %s', $firstname, $surname);
pg_close();
?>
</body>
</html>
Now for what we're doing. We'll break it down into parts again.
$db = pg_connect('dbname=friends');
This makes our usual connection to the database.
$query = 'INSERT INTO friends (id, firstname, surname) values (nextval('friends_id_seq'), '$firstname', '$surname')';
$result = pg_exec($db, $query);
This is the part that actually does the work. The format of the command is -
INSERT INTO table (column1, column2 .. columnX) values ('value1', 'value2' .. 'valueX');
Where do $firstname and $surname come from? In the form where we type all the information in to, we 'name' the fields
First Name : <input type='text' name='firstname'
Surname : <input type='text' name='surname'
We can actually name these anything we want that isn't already used
somewhere else in the form, but for ease of use, we'll name them the same
as our database fields. It just makes it easier later on in our 'add'
script. As before, variables are recognised with '$'. So our variables become $firstname and $surname.
The nextval command is used by the serial datatype. When we first created the tables, we didn't give this table a name, so by
default PostgreSQL gives it a name of 'databasename_id_seq', meaning the database name, it's an ID field, and it's also
a sequence. So, we need the next value in this sequence.
if (!$result)
If the INSERT command doesn't work, then we print 'ERROR', and exit the script as we have done previously. This will be
explained in a bit more detail further on in our error section.
printf ('These values were inserted into the database - %s %s' ,$firstname, $surname);
If it works, it displays the information we have put in. It won't display the ID number though, to see it we need to display the
whole database again. This is just so we can see what was put in, in a real database, we'd put a message saying
'successful' and then a link back to another page, or something similiar.
If you run the HTML page, depending on what version of PostgreSQL you're running, it will give different results.
The output might look like this:
Warning: PostgreSQL query failed: ERROR: friends_id_seq.nextval: Permission denied. in add.php on line 7 ERROR
This is because we haven't given the webserver access to the ID sequence table. With PostgreSQL V7 or later, we have to give
users access to all the different tables. Before V7, we didn't need to do this. How do we give it access to the ID sequence
table? Put this into a file called access2:
GRANT all ON friends_id_seq TO wwwrun;
Then run this command:
postgres@designmagick:/usr/local/pgsql > psql -d friends -f access2
It should return something like this:
CHANGE
If we ran the HTML page again, and added something to our database, it would work this time and print out what we entered for the
firstname and surname.
pg_close();
?>
</body>
</html>
We have the usual end of script, to close the PHP script, and finish the HTML code.
Done! Its pretty simple isn't it? You can do all this in one code, but I thought it would be easier to explain doing it this way.
In a more advanced tutorial, I might show you how to do it all in one script.
Give it a try. Add some information into the database. Now run the script from before which gets all the information from the
database, and you'll see the new data at the end.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 710
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved