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.openConnect ion();
connection.setD oOutput(true);
PrintWriter out = new
PrintWriter(con nection.getOutp utStream());
out.print("id=" +URLEncoder.enc ode("4","UTF-8")+"&name="+UR LEncoder.encode ("Apchar","U TF-8")+"&title="+U RLEncoder.encod e("Chieftan","U TF-8")+"\n");
out.close();
} catch(IOExcepti on e)
{
System.out.prin tln("Oops"+e); return;
}
repaint();
try
{
getAppletContex t().showDocumen t(new
URL("http://localhost/tmp/thanks.html"));
} catch (MalformedURLEx ception e)
{
System.out.prin tln("Oops"+e); return;
}
}
public void paint(Graphics g)
{
g.drawString("S ending",1,14);
}
}
Here is the php page that processes the data.
<html>
<head>
<title>CGISQLPo st</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($st r, $conn) or die(mysql_error ());
mysql_close($co nn);
?>
</body>
</html>
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.openConnect ion();
connection.setD oOutput(true);
PrintWriter out = new
PrintWriter(con nection.getOutp utStream());
out.print("id=" +URLEncoder.enc ode("4","UTF-8")+"&name="+UR LEncoder.encode ("Apchar","U TF-8")+"&title="+U RLEncoder.encod e("Chieftan","U TF-8")+"\n");
out.close();
} catch(IOExcepti on e)
{
System.out.prin tln("Oops"+e); return;
}
repaint();
try
{
getAppletContex t().showDocumen t(new
URL("http://localhost/tmp/thanks.html"));
} catch (MalformedURLEx ception e)
{
System.out.prin tln("Oops"+e); return;
}
}
public void paint(Graphics g)
{
g.drawString("S ending",1,14);
}
}
Here is the php page that processes the data.
<html>
<head>
<title>CGISQLPo st</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($st r, $conn) or die(mysql_error ());
mysql_close($co nn);
?>
</body>
</html>
Comment