Transfer data from webpage form to database
Pete.....
helten0007 at yahoo.com
Tue Nov 23 15:34:51 EST 2004
Hi again and thanks.
I think I understand the meaning of string formatting operators, I also
understand the meaning with % (form['name'].value, form['surname'].value,
form['username'].value)) And that it is a dictionary that I use.
The thing I find really hard to understand is how I can send the data from
the webform to my postgresql database. I cant find the right commands, i
been searching the web for days now without luck.
PgSQL.query(''' INSERT into form Select persons.idpersons, person.username,
persons.surname, phone.phone (And so on ) ( its what the specific tables is
called in my db)
>From persons, phone( And so on )
Where persons.name = 'Name'%s
AND persons.surname ='Surname'%s
AND persons.username = 'Username'%s ''' (And so on with the rest)
I dont really know if I have to write Pgsql.query("INSERT into form
........................
or if I have to write something else.
I know that I have to take the input from my webform, and store each input
( with each input I mean what the user types in each field, could be the
name, the username etc) in a "variable" so that I can put it into the right
place in the db.
I can see that I do this by using % (form['name'].value,
form['surname'].value, form['username'].value))
But I dont know how I put the data form['name].value in to my database in
the table persons.name.
Sorry for all this questions, just getting really confused with this little
project of mine, thinks its fun, but I just cant figure it out anymore, and
I cant find any info I can use on the web....
Thanks for taking your time to read this....
"Jerry Sievers" <jerry at jerrysievers.com> wrote in message
news:m3u0rg49kx.fsf at prod01.jerrysievers.com...
> "Pete....." <helten0007 at yahoo.com> writes:
>
>> Hi, yeah It's try I want to change it to a python.cgi.script. And thanks
>> for
>> your input.....
>>
>> I use the api pgsql
>> So the first thing I do is to import the following and connect to the
>> server:
>>
>> #!/pack/python-2.3.2/bin/python2.3
>> from pyPgSQL import PgSQL
>> import cgi
>> import cgitb
>> cgitb.enable()
>>
>> form = cgi.FieldStorage() ( I added this )
>> connect = PgSQL.connect(user="vvvvv", password="zzzzz",
>> host="xxx.xxx.xx",
>> database="yyyyy")
>>
>> Do I then have to write:
>>
>> PgSQL.query(''' INSERT into form
>> Select persons.idpersons, person.username,
>> persons.surname, phone.phone (And so on ) ( its what the specific tables
>> are called in my database)
>> From persons, phone
>> (
>> And so on )
>> Where persons.name = 'Name'%s
>> AND persons.surname ='Surname'%s
>> AND persons.username = 'Username'%s '''
>> (And so on with the rest)
>> % (form['name'].value, form['surname'].value,
>> form['username'].value))
>>
>> I dont think I get it quite right, any help would be highly appreciated.
>> Thanks for all help...
>
> It looks to me like the part here that you don't understand is how to
> interpolate your form data into the string for the database query.
>
> You should study up on the % string formatting operator in the Python
> docs.
>
> I'll throw you a bone here;
>
> """
> %s %s %s
> """ % (form[key1].value, form[key2].value, form[keyN].value)
>
> Each of those %s tokens is going to be replaced with one of the values
> in the tuple right of the % operator.
>
> If you want to pile the form values into a dict and then use symbolic
> names in the string...
>
> dict = {}
> for key in form:
> dict[key] = form[key].value
>
> """
> %(key1)s %(key2)s...
> """ % dict
>
> Maybe there is a function in the cgi module that will make the dict
> for you? I am not sure.
>
> Have fun!
>
>
> --
> -------------------------------------------------------------------------------
> Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
> 305 321-1144 (mobile http://www.JerrySievers.com/
More information about the Python-list
mailing list