10 Replies - 919 Views - Last Post: 24 July 2009 - 07:39 AM Rate Topic: -----

#1 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

php and ajax not inserting into db as it should

Posted 23 July 2009 - 08:45 AM

right ok so i have this textbox that when i press enter runs some ajax code then i have a php page that adds details to the db however 1 column isnt inserting properly which is wierd becayse the rest are

its the receiver column


ajax,
function sendMessage(str, str2)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="getuser.php";
url=url+"?m="+str;
url=url+"&u="+str2;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}



php

<?php
session_start();

if ($_SESSION['loggedIn'] != "true") {
	 header("Location: default.php");
} 
$q=$_GET["q"];
$m = $_GET['m'];
$u = $_GET['u'];
if(isset($q)){
	require("conn.php");
	if(is_numeric($q)){
		$sql="SELECT * FROM members WHERE id = '".$q."'";
	}else{
		$sql="SELECT * FROM members WHERE username = '".$q."'";
	}
	$dbdata = mysql_query($sql);
	$result = mysql_fetch_array($dbdata);
	$newchat = rand(0, 999999);
	$_SESSION['chat'] = $newchat;
	if($result['online'] == 1){
		$u1 = $_SESSION['id'];
		$u2 = $result['id'];
		echo "This player is online. <a href='chat.php?chatid=".$newchat."&u1=".$u1."&u2=".$u2."'>Start chat</a>.";
	}else{
		echo "This player is currently offline and you can not chat with them.";
	}
}else{
	require("conn1.php"); 
	$query = "insert into single_chat(chatid, sender, receiver, message) values ('".$_SESSION['chat']."', '".$_SESSION['id']."', '$u', '$m')";
	$dbdata = mysql_query($query) or die (mysql_error());
	echo "added succesfully";
}
?>



i cant see why it is inserting into the db as receiver = 0 not what it should be

Is This A Good Question/Topic? 0
  • +

Replies To: php and ajax not inserting into db as it should

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: php and ajax not inserting into db as it should

Posted 23 July 2009 - 09:39 AM

What should it be? Can you provide with a fictional function call? :) and also you should escape the data before inserting it.

You might want to encodeURIComponent() the variables before sending them with ajax though! :)

View Postghqwerty, on 23 Jul, 2009 - 05:45 PM, said:

right ok so i have this textbox that when i press enter runs some ajax code then i have a php page that adds details to the db however 1 column isnt inserting properly which is wierd becayse the rest are

its the receiver column


ajax,
function sendMessage(str, str2)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="getuser.php";
url=url+"?m="+str;
url=url+"&u="+str2;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}



php

<?php
session_start();

if ($_SESSION['loggedIn'] != "true") {
	 header("Location: default.php");
} 
$q=$_GET["q"];
$m = $_GET['m'];
$u = $_GET['u'];
if(isset($q)){
	require("conn.php");
	if(is_numeric($q)){
		$sql="SELECT * FROM members WHERE id = '".$q."'";
	}else{
		$sql="SELECT * FROM members WHERE username = '".$q."'";
	}
	$dbdata = mysql_query($sql);
	$result = mysql_fetch_array($dbdata);
	$newchat = rand(0, 999999);
	$_SESSION['chat'] = $newchat;
	if($result['online'] == 1){
		$u1 = $_SESSION['id'];
		$u2 = $result['id'];
		echo "This player is online. <a href='chat.php?chatid=".$newchat."&u1=".$u1."&u2=".$u2."'>Start chat</a>.";
	}else{
		echo "This player is currently offline and you can not chat with them.";
	}
}else{
	require("conn1.php"); 
	$query = "insert into single_chat(chatid, sender, receiver, message) values ('".$_SESSION['chat']."', '".$_SESSION['id']."', '$u', '$m')";
	$dbdata = mysql_query($query) or die (mysql_error());
	echo "added succesfully";
}
?>



i cant see why it is inserting into the db as receiver = 0 not what it should be

Was This Post Helpful? 0
  • +
  • -

#3 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: php and ajax not inserting into db as it should

Posted 23 July 2009 - 10:41 AM

here is where it is called


<script TYPE="text/javascript">
function submitenter(myfield,e)
{
var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13)
	   {
	   if(message.value != ""){
			sendMessage(message.value, <?php echo $_SESSION['u2'];?>);			
			return false;
		}
	   }
	else
	   return true;
}
</SCRIPT>


and after viewing the source it is putting in the correct number, wil try encodeuri, where would i put it though ? before i call the function ?
Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: php and ajax not inserting into db as it should

Posted 23 July 2009 - 11:19 AM

Something like this should be fine:
function sendMessage(str, str2)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="getuser.php";
url=url+"?m="+encodeURIComponent(str);
url=url+"&u="+encodeURIComponent(str2);
url=url+"&sid="+encodeURIComponent(Math.random());
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}


:)
Was This Post Helpful? 0
  • +
  • -

#5 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: php and ajax not inserting into db as it should

Posted 24 July 2009 - 12:23 AM

nope still entering 0 :(
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3070
  • View blog
  • Posts: 10,768
  • Joined: 08-August 08

Re: php and ajax not inserting into db as it should

Posted 24 July 2009 - 04:35 AM

Have you tried printing the query to see what it is?

echo $query."<br>";


You might try it this way:

	$query = "insert into single_chat(chatid, sender, receiver, message) values ('".$_SESSION['chat']."', '".$_SESSION['id']."', '".$u."', '".$m."')";


Was This Post Helpful? 0
  • +
  • -

#7 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: php and ajax not inserting into db as it should

Posted 24 July 2009 - 05:25 AM

yes ive tried that and it just has 'single' quotes ther with nothing inside,

when i run the page manually eg

getuser.php?m=hello&u=1&sid=6 it works fine, so it makes me think it is something to do with how it is sending the data from sendMessage or how i am calling sendMessage
Was This Post Helpful? 0
  • +
  • -

#8 izrafel  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 96
  • Joined: 24-July 09

Re: php and ajax not inserting into db as it should

Posted 24 July 2009 - 06:14 AM

ok obviously the problem is in javascrpt.you can download Firefox (if you arent using that for development) and add to it FireBug. In firebug you can see the ajax requests in real time. check to see what you are sending. + delete your browsers cache before testing.when talking about cache,html,css,javascript there is a very helpful addon for FF called web developer. You will love it.:)
Ok this is for my first post. :)

This post has been edited by izrafel: 24 July 2009 - 06:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: php and ajax not inserting into db as it should

Posted 24 July 2009 - 07:10 AM

how does it work, i cant seem to figure it out
Was This Post Helpful? 0
  • +
  • -

#10 izrafel  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 96
  • Joined: 24-July 09

Re: php and ajax not inserting into db as it should

Posted 24 July 2009 - 07:21 AM

View Postghqwerty, on 24 Jul, 2009 - 06:10 AM, said:

how does it work, i cant seem to figure it out

how does what work?firebug?by default it is turned off, because it has some problems with google maps (it tries to analyze the javascrit code).
after you install firebug a small bug (beetle) appears in the right corner of the browser.click on it and it should open a window and start. after that select Net from the upper menu, and then All from the lower menu.this shows all the requests the browser makes (post and get).and when you click on a given request it gives you info about the request.what parameters have you sent (post and get)(this is what you need), the headers, the response , html(the response executed as html).
Was This Post Helpful? 0
  • +
  • -

#11 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: php and ajax not inserting into db as it should

Posted 24 July 2009 - 07:39 AM

arrrrr im such an idiot, there was a function below it named the same, hence the confusion.

sorry
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1