|
I am trying to use php as a kind of servlet to act as a middle man
between a java applet and mysql. I know java has jdbc but it's flakey
and painful. php access to mysql is much nicer. So I have:
1. An html page that holds the applet.
2. a php page that accepts data submitted to it by the applet via the
$_POST array and writes it to the mysql database. This page never
makes it to the browser window.
3. a simple Thank you page that shows up after the applet sends the
data.
Trouble is, nothing makes it into the mysql database. I've tested each
component seperately. I know they work. I know the applet data is
posted cuz I tested it with the usual query.php. But when I put them
together nothing shows up in the database.
What did I do wrong???
Heres the java applet. The page that holds it has nothing but this
applet.
import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.*;
public class CGISQLPost extends Applet
{
public void init()
{
try
{
URL url = new URL("http://localhost/tmp/CGISQLPost.php");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
PrintWriter out = new
PrintWriter(connection.getOutputStream());
out.print("id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n");
out.close();
} catch(IOException e)
{
System.out.println("Oops"+e); return;
}
repaint();
try
{
getAppletContext().showDocument(new
URL("http://localhost/tmp/thanks.html"));
} catch (MalformedURLException e)
{
System.out.println("Oops"+e); return;
}
}
public void paint(Graphics g)
{
g.drawString("Sending",1,14);
}
}
Here is the php page that processes the data.
<html>
<head>
<title>CGISQLPost</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
</head>
<body>
<?php
$id = $_POST[id];
$name = $_POST[name];
$title = $_POST[title];
$conn = mysql_connect("localhost","web","webster");
mysql_select_db("test", $conn);
$str = "insert into mytest values ($id,'$name','$title')";
mysql_query($str, $conn) or die(mysql_error());
mysql_close($conn);
?>
</body>
</html> | |
Share:
|
Hi,
this is quite an interesting setup you got there. Personally, I have
never tried this using an Java Applet but did create environments
where JDBC, PHP and
a database were involved.
Have you tried creating an ODBC Trace in order to see how far the
applet gets when accessing the database ?
Also, have you tried this data entry using non-unicode data ?
MySQL only recently started supporting Unicode so I assume you
are using the latest Version and it might be worth running a test
using non-unicode data (and a non-unicode DB).
Also, which Version of PHP are you working with at this stage ?
You said that you tested all components seperately, so I would imagine
that
you can successfully enter data into MySQL using PHP without the
applet as
a front-end. If this is the case the problem must be inbetween the
applet
and PHP itself. Either way, an ODBC Trace and doublechecking the PHP
make
should shed more light on the cause of this problem. Regarding the
PHP-ODBC
interface you might want to have a look at http://www.iodbc.org.
I hope you will find this to be of help.
Kind Regards,
Jan Csisko
Professional Services Consultant
OpenLink Software Web: http://www.openlinksw.com
Product Weblogs: Virtuoso: http://www.openlinksw.com/weblogs/virtuoso
UDA: http://www.openlinksw.com/weblogs/uda
Universal Data Access & Virtual Database Technology Providers ap****@yahoo.com (apchar) wrote in message news:<29*************************@posting.google.c om>... I am trying to use php as a kind of servlet to act as a middle man between a java applet and mysql. I know java has jdbc but it's flakey and painful. php access to mysql is much nicer. So I have: 1. An html page that holds the applet. 2. a php page that accepts data submitted to it by the applet via the $_POST array and writes it to the mysql database. This page never makes it to the browser window. 3. a simple Thank you page that shows up after the applet sends the data. Trouble is, nothing makes it into the mysql database. I've tested each component seperately. I know they work. I know the applet data is posted cuz I tested it with the usual query.php. But when I put them together nothing shows up in the database. What did I do wrong??? Heres the java applet. The page that holds it has nothing but this applet.
import java.awt.*; import java.net.*; import java.io.*; import java.applet.*;
public class CGISQLPost extends Applet { public void init() { try { URL url = new URL("http://localhost/tmp/CGISQLPost.php"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); out.print("id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n"); out.close(); } catch(IOException e) { System.out.println("Oops"+e); return; } repaint(); try { getAppletContext().showDocument(new URL("http://localhost/tmp/thanks.html")); } catch (MalformedURLException e) { System.out.println("Oops"+e); return; }
}
public void paint(Graphics g) { g.drawString("Sending",1,14); } }
Here is the php page that processes the data.
<html> <head> <title>CGISQLPost</title> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> </head> <body> <?php $id = $_POST[id]; $name = $_POST[name]; $title = $_POST[title]; $conn = mysql_connect("localhost","web","webster"); mysql_select_db("test", $conn); $str = "insert into mytest values ($id,'$name','$title')"; mysql_query($str, $conn) or die(mysql_error()); mysql_close($conn); ?> </body> </html> | | |
On 2004-02-27, apchar <ap****@yahoo.com> wrote:
With the code you provided, you will always try to call a php script,
and then set the appletContext to thanks.html. I suggest that you set
the appletContext to the CGISQLPost.php page.
In your CGISQLPost.php you process the data, and then output an
appropriate page...
[snip applet code]
public void init() {
try {
String location = "http://localhost/tmp/CGISQLPost.php");
location.append("?id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n");
URL url = new URL(location);
getAppletContext().showDocument(url);
} catch (MalformedURLException e) {
System.out.println("Oops"+e);
}
}
[snip php code]
<?php
$id = $_POST['id'];
$name = $_POST['name'];
$title = $_POST['title'];
$conn = mysql_connect("localhost","web","webster");
mysql_select_db("test", $conn);
$str = "insert into mytest values ($id,'$name','$title')";
mysql_query($str, $conn) or die(mysql_error());
mysql_close($conn);
$fp = fopen('thanks.html');
while ($line = fgets($fp,4096)) == TRUE) {
echo $line;
}
fclose(fp);
?>
-- http://home.mysth.be/~timvw | | |
I just installed the odbc stuff for linux but I have no idea howw to
do a trace. I didn't find much except advertising on the iodbc site.
How do I do a trace? Where does the output go?
I tried it without UTF-8 encoding. Same story. BTW, why do the values
require encoding while the names of the variables passed dont??
I am using PHP v4.3.3 MySQL v4.0.15.
I did indeed test the php file without the applet with a simple html
form. It worked fine. It's only when data is passed via an applet.
Even stranger, I know the values I'm passing are getting to the php
file because I can echo them back to the java applet and display them.
I just cant feed them to mysql.
Thanks.
Art. jc*****@openlinksw.co.uk (Jan Csisko) wrote in message news:<99**************************@posting.google. com>... Hi,
this is quite an interesting setup you got there. Personally, I have never tried this using an Java Applet but did create environments where JDBC, PHP and a database were involved.
Have you tried creating an ODBC Trace in order to see how far the applet gets when accessing the database ?
Also, have you tried this data entry using non-unicode data ? MySQL only recently started supporting Unicode so I assume you are using the latest Version and it might be worth running a test using non-unicode data (and a non-unicode DB).
Also, which Version of PHP are you working with at this stage ?
You said that you tested all components seperately, so I would imagine that you can successfully enter data into MySQL using PHP without the applet as a front-end. If this is the case the problem must be inbetween the applet and PHP itself. Either way, an ODBC Trace and doublechecking the PHP make should shed more light on the cause of this problem. Regarding the PHP-ODBC interface you might want to have a look at http://www.iodbc.org.
I hope you will find this to be of help.
Kind Regards,
Jan Csisko Professional Services Consultant OpenLink Software Web: http://www.openlinksw.com Product Weblogs: Virtuoso: http://www.openlinksw.com/weblogs/virtuoso UDA: http://www.openlinksw.com/weblogs/uda Universal Data Access & Virtual Database Technology Providers ap****@yahoo.com (apchar) wrote in message news:<29*************************@posting.google.c om>... I am trying to use php as a kind of servlet to act as a middle man between a java applet and mysql. I know java has jdbc but it's flakey and painful. php access to mysql is much nicer. So I have: 1. An html page that holds the applet. 2. a php page that accepts data submitted to it by the applet via the $_POST array and writes it to the mysql database. This page never makes it to the browser window. 3. a simple Thank you page that shows up after the applet sends the data. Trouble is, nothing makes it into the mysql database. I've tested each component seperately. I know they work. I know the applet data is posted cuz I tested it with the usual query.php. But when I put them together nothing shows up in the database. What did I do wrong??? Heres the java applet. The page that holds it has nothing but this applet.
import java.awt.*; import java.net.*; import java.io.*; import java.applet.*;
public class CGISQLPost extends Applet { public void init() { try { URL url = new URL("http://localhost/tmp/CGISQLPost.php"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); out.print("id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n"); out.close(); } catch(IOException e) { System.out.println("Oops"+e); return; } repaint(); try { getAppletContext().showDocument(new URL("http://localhost/tmp/thanks.html")); } catch (MalformedURLException e) { System.out.println("Oops"+e); return; }
}
public void paint(Graphics g) { g.drawString("Sending",1,14); } }
Here is the php page that processes the data.
<html> <head> <title>CGISQLPost</title> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> </head> <body> <?php $id = $_POST[id]; $name = $_POST[name]; $title = $_POST[title]; $conn = mysql_connect("localhost","web","webster"); mysql_select_db("test", $conn); $str = "insert into mytest values ($id,'$name','$title')"; mysql_query($str, $conn) or die(mysql_error()); mysql_close($conn); ?> </body> </html> | | |
I thought data passed in the url was only passed to the $_GET array. I
cant use GET to post the data since some of the values are way too
big. GET is limited to I dont remember how many bytes. Note that what
I've shown here is just a skeleton of the actual code.
Do you think the problem is that I never display the php page?
Thanks,
Tim Van Wassenhove <eu**@pi.be> wrote in message news:<c1*************@ID-188825.news.uni-berlin.de>... On 2004-02-27, apchar <ap****@yahoo.com> wrote:
With the code you provided, you will always try to call a php script, and then set the appletContext to thanks.html. I suggest that you set the appletContext to the CGISQLPost.php page.
In your CGISQLPost.php you process the data, and then output an appropriate page...
[snip applet code]
public void init() { try { String location = "http://localhost/tmp/CGISQLPost.php"); location.append("?id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n"); URL url = new URL(location); getAppletContext().showDocument(url); } catch (MalformedURLException e) { System.out.println("Oops"+e); } }
[snip php code]
<?php $id = $_POST['id']; $name = $_POST['name']; $title = $_POST['title']; $conn = mysql_connect("localhost","web","webster"); mysql_select_db("test", $conn); $str = "insert into mytest values ($id,'$name','$title')"; mysql_query($str, $conn) or die(mysql_error()); mysql_close($conn);
$fp = fopen('thanks.html'); while ($line = fgets($fp,4096)) == TRUE) { echo $line; } fclose(fp); ?> | | |
Just to complete the thread for the archives in case anyone else runs
into this problem, the solution is to not only write the data to the
php page but to read back the response. Apparently PHP wont do
anything at all unless it can spit back something to whatever called
it. I didn't even have to display the response, I just had to read it
in. Below is the updated java and php code. Thanks to all who replied!
-----------------------------------java code ------------------
import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.*;
public class CGISQLPost extends Applet
{
int numLines=0;
String[] lines = new String[60];
public void init()
{
try
{
URL url = new URL("http://localhost/tmp/CGISQLPost.php");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print("id=13&name="+URLEncoder.encode("ralph
malph","UTF-8")+"&title="+URLEncoder.encode("goof
ball","UTF-8")+"goofball\n");
out.flush();
out.close();
// read response
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
while ((lines[numLines] = in.readLine()) != null)
{
lines[numLines] = lines[numLines].trim();
if ((lines[numLines].length()) > 0) numLines++;
}
in.close();
} catch(IOException e)
{
System.out.println("Oops"+e); return;
}
repaint();
try
{
getAppletContext().showDocument(new
URL("http://localhost/tmp/thanks.html"));
} catch (MalformedURLException e)
{
System.out.println("Oops"+e); return;
}
}
public void paint(Graphics g)
{
g.drawString("Sending",1,14);
}
}
-----------------------------php code -----------------------
<html>
<head>
<title>CGISQLPost</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
</head>
<body>
<?php
$id = (integer) $_POST[id];
$name = $_POST[name];
$title = rtrim($_POST[title]);
print "id=$id<br>name=$name<br>title=$title<br>\n";
$conn = mysql_connect("localhost","blah","blahblah");
mysql_select_db("test", $conn);
$str = "insert into mytest values ($id,'$name','$title')";
print $str;
mysql_query($str, $conn) or die(mysql_error());
mysql_close($conn);
?>
</body>
</html> | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
reply
views
Thread by cwho.work |
last post: by
| | | | | |
66 posts
views
Thread by flarosa |
last post: by
| | | | | | | | | | | | |