Author Topic: CGI -> MySQL  (Read 13946 times)

alej

  • Guest
CGI -> MySQL
« on: January 05, 2009, 03:53:46 PM »
Hi,

how can I write data with the cgi modul to MySQL using forms... There is only
a sample how to read data with the cgi modul... (http://www.scriptbasic.org/cgi-bin/mysql_tutorial.bas)...

Thank you in advance
JK
« Last Edit: January 05, 2009, 04:16:43 PM by alej »

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: CGI -> MySQL
« Reply #1 on: January 05, 2009, 08:44:26 PM »
JK,

Welcome to the ScriptBasic forum.

Here is an example of using the MySQL extension module in a CGI environment.

http://www.scriptbasic.org/forum/index.php/topic,3.0.html

To "write" to a SQL database you have to use the INSERT or UPDATE SQL statements.

Let us know how it works out for you.

John
« Last Edit: January 05, 2009, 08:57:11 PM by support »

alej

  • Guest
Re: CGI -> MySQL
« Reply #2 on: January 09, 2009, 03:26:51 PM »
Hi,

i played a little bit with cgi and mysql and tried the following...

Code: [Select]
#! /usr/bin/scriba -c

include cgi.bas
include mysql.bas

dbh = mysql::RealConnect("localhost","root","mypassword","sbtestdb")

cgi::Header 200,"text/html"
cgi::FinishHeader

if cgi::RequestMethod() = "POST" then
SQL = "INSERT INTO contact (NAME) VALUES (" & TEXT-FIELD")"
end if

print """<head>
</head>
<title>mysql test</test>
<body>
<table><tr><td border=0 bgcolor="eeeee">
<form method="POST" ACTION="index.bas">
<input type ="TEXT" VALUE="TEST" NAME="TEXT-FIELD">
<input type ="SUBMIT" NAME="SUBMIT-BUTTON" VALUE="POST">
</form>
</td>
</body>
</html>


but this does not seem to work...  ??? Any hint

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: CGI -> MySQL
« Reply #3 on: January 09, 2009, 05:15:41 PM »
Hint:


mysql::query(dbh, SQL)

In your example you assign the variable SQL with the statement but that's it.

Now MySQL needs to process it with the query() function.

Nothing to fetch() as your not requesting a return of any data.

If the row already exists and has a unique 'key' (index) then you would get an error on the INSERT and would have to use a UPDATE instead.

You have an error in your SQL string as well.

Code: [Select]

SQL = "INSERT INTO contact (NAME) VALUES (" & TEXT-FIELD")"

should be:

SQL = "INSERT INTO contact (NAME) VALUES ('" & cgi::PostParam("TEXT-FIELD") & "')"


John
« Last Edit: January 10, 2009, 01:27:06 AM by support »

alej

  • Guest
Re: CGI -> MySQL
« Reply #4 on: January 13, 2009, 05:50:21 AM »
Hi John,

thank you for the hint, when i try to run the script (apache 2.2.9) I get an error

Premature end of script headers: index.bas

Error 500

 ??? ??? ???

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: CGI -> MySQL
« Reply #5 on: January 13, 2009, 07:04:41 AM »
I would first see if the echo.bas program runs from the cgi-bin correctly.

I would then create a console mode test program to make sure MySQL is working for you. (without CGI / HTML code)

scriba -d your_pgm.bas  will show you debug output for the extension module loading and tell you if it failed or not.

If everything looks good at this point and your still having problems running your script, repost your code so I can have a peek.

If your running under Windows, don't forget to change the first line.

# ! C:/scriptbasic/bin/scriba -c
« Last Edit: January 13, 2009, 03:37:41 PM by support »