Support > General Discussions

CGI -> MySQL

(1/2) > >>

alej:
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

Support:
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

alej:
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>


--- End code ---

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

Support:
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") & "')"

--- End code ---


John

alej:
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

 ??? ??? ???

Navigation

[0] Message Index

[#] Next page

Go to full version