ScriptBasic
May 20, 2013, 07:27:35 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: The ScriptBasic User, Developer and extension module guides are available at the WIKI.
 
   Home   Wiki Help Search Login Register  
Pages: [1]
  Print  
Author Topic: CGI -> MySQL  (Read 2591 times)
alej
Newbie
*
Posts: 3


« on: January 05, 2009, 04: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, 05:16:43 PM by alej » Logged
support
Administrator
*****
Posts: 544



WWW
« Reply #1 on: January 05, 2009, 09: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, 09:57:11 PM by support » Logged

ScriptBasic Project Manager
alej
Newbie
*
Posts: 3


« Reply #2 on: January 09, 2009, 04:26:51 PM »

Hi,

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

Code:
#! /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...  Huh Any hint
Logged
support
Administrator
*****
Posts: 544



WWW
« Reply #3 on: January 09, 2009, 06: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:

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, 02:27:06 AM by support » Logged

ScriptBasic Project Manager
alej
Newbie
*
Posts: 3


« Reply #4 on: January 13, 2009, 06: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

 Huh Huh Huh
Logged
support
Administrator
*****
Posts: 544



WWW
« Reply #5 on: January 13, 2009, 08: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, 04:37:41 PM by support » Logged

ScriptBasic Project Manager
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!