14 Replies - 388 Views - Last Post: 18 March 2011 - 01:22 PM Rate Topic: -----

#1 shaboury  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 30-May 09

Problem in SQL Query - PHP

Posted 18 March 2011 - 01:13 PM

that's a J2me code to get a data and redirect it to a php to insert it into data base
package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
import java.util.*;
import java.lang.*;

public class HelloMIDlet extends MIDlet implements CommandListener
{
	Display display;
	Command exit;
	Command login;
	Form screen;

	public TextField location;
	TextField outputfield;
	InputStream in = null;
	String url = new String();


	public HelloMIDlet()
	{

	   	display = Display.getDisplay(this);
		exit = new Command("Exit",Command.EXIT, 1);
		login = new Command("Insert", Command.SCREEN,2);

		location = new TextField("Enter the location:", "", 10, TextField.ANY);
		outputfield= new TextField("Status:", "", 256, TextField.UNEDITABLE);

		screen = new Form("Login Form In Project");

		screen.append(location);
		screen.append(outputfield);
		screen.addCommand(exit);
		screen.addCommand(login);
		screen.setCommandListener(this);

	}

	public void startApp()
	{

		display.setCurrent(screen);

	}

	public void pauseApp()
	{

	}

	public void destroyApp(boolean unconditional)
	{

	}

	public void update_action()
	{

		// Create a thread to actually connect to the webserver
		Thread t = new Thread()
		{
			public void run()
			{
				//open connecttion
				try
				{
					outputfield.setString("Attempting to Connect...");
			    		url = generateGetUrl("http://localhost/dirty/location.php",location.getString());
					System.out.print("\n" + url + "\n");
					if(updateSite(url)==1)
						outputfield.setString("Sucessfully Logined in website...");
					else
						outputfield.setString("Failed to sign in. =(");

				}
				catch(IOException e)
				{
					outputfield.setString("Failed to Connect.  =(");
				}
			}
		};

		t.start();
	}

	public void commandAction(Command c, Displayable s)
	{

		if(c == login)
		{
			update_action();
		}

		if(c == exit)
		{
			destroyApp(false);
			notifyDestroyed();
		}


	}

       public String generateGetUrl(String url, String text)
       {
		String newUrl = new String();
		StringBuffer c = new StringBuffer(text);

		for(int i=0; i<c.length(); i++)
		{
			if(c.charAt(i)==' ')
			{
				c = c.insert(i,"%20");
				c = c.deleteCharAt(i+3);
			}
		}
		
		newUrl = url + "?text=" + c.toString();

		return newUrl.toString();

       }


       public int updateSite(String url) throws IOException
	   {
		HttpConnection conn = null;
		DataOutputStream out = null;

		try
        	{
			conn = (HttpConnection)Connector.open(url);
			conn.setRequestMethod(HttpConnection.GET);
			conn.setRequestProperty("Connection", "close");


			if(conn.getResponseCode() == HttpConnection.HTTP_OK)
			{

				return 1;
			}
			else
			{

				return 0;
			}




		}
		catch(IOException e)
		{
			System.out.print("Error Connection to website.\n");
			return 0;
		}

	}

}

and thats my php code


<?php
  $dbservertype='mysql';
$servername='localhost';
$dbusername='root';
$dbpassword='';
$dbname='bts';

connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("bts",$link) or die ("could not open db".mysql_error());

	mysql_select_db('bts');


	
	 $loc = location
       



        mysql_query("insert into sheet1 (Location) values ('islam')");

	mysql_close($con);

}
?>




and when i run my php from browser i get this error

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\dirty\location.php on line 25


Is This A Good Question/Topic? 0
  • +

Replies To: Problem in SQL Query - PHP

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2899
  • View blog
  • Posts: 7,555
  • Joined: 08-June 10

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:14 PM

missing ; on line 20. although that leaves you with an undefined constant …

This post has been edited by Dormilich: 18 March 2011 - 01:15 PM

Was This Post Helpful? 1
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 514
  • View blog
  • Posts: 1,604
  • Joined: 08-November 08

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:14 PM

Missing semicolon at the end of line 20.
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1813
  • View blog
  • Posts: 20,232
  • Joined: 17-March 01

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:14 PM

Missing semi-colon here: $loc = location
Was This Post Helpful? 0
  • +
  • -

#5 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:15 PM

Where you have $loc = location you have missed the all important semi colon ; at the end of the line.


$loc = location;
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2899
  • View blog
  • Posts: 7,555
  • Joined: 08-June 10

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:15 PM

lol, just lol.

I might add that the global is unnecessary, too.

This post has been edited by Dormilich: 18 March 2011 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#7 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1813
  • View blog
  • Posts: 20,232
  • Joined: 17-March 01

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:16 PM

lmfao, that's what I'm talkin about!!!
Was This Post Helpful? 0
  • +
  • -

#8 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:17 PM

We all must be brilliant to think a like :D.
Was This Post Helpful? 0
  • +
  • -

#9 shaboury  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 30-May 09

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:17 PM

:innocent: lol :D
new error
Warning: mysql_close() expects parameter 1 to be resource, null given in C:\xampp\htdocs\dirty\location.php on line 27
Was This Post Helpful? 0
  • +
  • -

#10 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:18 PM

You have not defined $con anywhere.
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2899
  • View blog
  • Posts: 7,555
  • Joined: 08-June 10

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:19 PM

you named your connection $link, not $con.
Was This Post Helpful? 0
  • +
  • -

#12 shaboury  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 30-May 09

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:19 PM

yea i know that's there are unnecessary variable but am testing it becuz that s my first time with php ..
Was This Post Helpful? 0
  • +
  • -

#13 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 514
  • View blog
  • Posts: 1,604
  • Joined: 08-November 08

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:20 PM

That's because you never define $con. It's $link that you want.

EDIT: This post is getting help-massacred, lol.

This post has been edited by Valek: 18 March 2011 - 01:20 PM

Was This Post Helpful? 1
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2899
  • View blog
  • Posts: 7,555
  • Joined: 08-June 10

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:21 PM

then don’t bother with the mysql functions, they’re outdated. rather use mysqli or PDO from the start.
Was This Post Helpful? 1
  • +
  • -

#15 shaboury  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 30-May 09

Re: Problem in SQL Query - PHP

Posted 18 March 2011 - 01:22 PM

loool :D i love u guys !! how i am stupid !! u all helped so much thank u !!! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1