live chat - undefined index line 5? why???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mythic Fr0st
Forum Contributor
Posts: 137
Joined: Sat Dec 02, 2006 3:23 am
Contact:

live chat - undefined index line 5? why???

Post by Mythic Fr0st »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, I need to know why this isnt working...

the PROBLEM is nothing happens, I type in 'hello' press send, and it just flashes, and nothing happens

Code: Select all

<html>
	<head>
		<script>
			var user_id = <?=$_GET['user_id']?>;
			var chat_id = <?=$_GET['chat_id']?>;
			var last_id = 0
			var timer = window.setInterval("doPageRequest('')", 2000);
			function getEl(id) {
				return document.getElementById(id)
			}
			
			function sendMsg() {
				var msg = getEl('msgBox').value;
				getEl('msgBox').value = "";
				doPageRequest(msg);
			}
			
			function doPageRequest(msg) {
				var url = "msgHandle.php?chat_id=" + chat_id + "&id=" + last_id;
				if (msg != '') {
					url = url + "&send&msg=" + msg + "&user_id=" + user_id;
				}
				// code for Mozilla, etc.
				if (window.XMLHttpRequest) {
					xmlhttp=new XMLHttpRequest();
					xmlhttp.onreadystatechange=showMsgs;
					xmlhttp.open("GET",url,true);
					xmlhttp.send(null);
				} else if (window.ActiveXObject) { // code for IE
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
						if (xmlhttp) {
							xmlhttp.onreadystatechange=showMsgs;
							xmlhttp.open("GET",url,true);
							xmlhttp.send();
					}
				}
			}

			function showMsgs() {
				var i = 0;
				var msgs = "";
				var msg;
				// if xmlhttp shows "loaded"
				if (xmlhttp.readyState==4) {
					// if "OK"
					if (xmlhttp.status==200) {
						eval(xmlhttp.responseText)
							if (msgs != "") {
								msg = msgs.split('#');
								for (i = 0; i < msg.length; i++) {
									msg[i] = msg[i].split('$');
									getEl('chatWindow').innerHTML += "[" + msg[i][0] + " - " + msg[i][1] + "] " + msg[i][2] + "<br />";
								}
							}
						getEl('chatWindow').scrollTop = getEl('chatWindow').scrollHeight;
					}
				}
			}
			
			doPageRequest('')
				
		</script>
		<meta http-equiv="content-type" content="text/html; charset=windows-1250">
		<title></title>
	</head>
	<body>  
		<div id="chatWindow" style="border: solid 1px #000; padding: 4px; width: 400px; height: 100px; overflow: auto;"></div>
                <div style='position: absolute; top:375; left:6; '>
		<form onSubmit="sendMsg(); return false;">
		<input type="text" id="msgBox" style="width: 300px;" />
		<input type="submit" id="send" value="Send" />
	</body>
</html>

Code: Select all

I get undefined index on the user_id thing for some reason o_O???

perhaps the problem is with msgHandle?

<?php
/* PHP Script that handles both inserting the data into the database and
 *  sending the new chat information to the client
 */
 
/* Check for id and chat information (required to do everything) */
if(isset($_GET['id']) && isset($_GET['chat_id'])) {
	/* Connect to database */
	$dbName = 'chat';
	$tblName = 'msgs';
	$conn = mysql_connect('localhost', 'root', '');
	mysql_select_db($dbName, $conn);
	/* Check chat table exists */
	if (!mysql_num_rows(mysql_query("SHOW TABLES LIKE '$tblName'"))) {
		$cmd = "CREATE TABLE $tblName (" .
		"id int(10) not null primary key auto_increment, " . 
		"chat_id int(5) not null, " .
		"user_id int(5) not null, time datetime not null, msg text not null)";
		mysql_query($cmd, $conn) or die("Error creating $tblName table:" . 
			mysql_error());
	}

  /* Check see if we have more information to insert */
	if(isset($_GET['send'])) {
		/* Check that we have both a message and a user_id */
		if(isset($_GET['msg']) && isset($_GET['user_id'])) {
			/* Insert msg into the database */
			$cmd = "INSERT INTO $tblName (chat_id, user_id, msg, time) VALUES " . 
				"('$_GET[chat_id]', '$_GET[user_id]', '$_GET[msg]', NOW())";
			mysql_query($cmd, $conn) or die("Error inserting msg:" . mysql_error());
		}
	}
	/* Get information to send back to page */
	$getCmd = "SELECT * FROM $tblName WHERE id > $_GET[id] AND " .
	"chat_id = $_GET[chat_id] ORDER BY id";
	$get = mysql_query($getCmd, $conn) or die("Error getting msgs:" . 
		mysql_error());
	$msgs = "";
	$last_id = $_GET['id'];
	while($msg = mysql_fetch_array($get)) {
		$msgs .= $msg['time'] . '$' . $msg['user_id'] . '$' . $msg['msg'] . '#';
		$last_id = $msg['id'];
	}
	echo 'last_id=' . $last_id . '; msgs="' . rtrim($msgs, '#') . '";';
}
I cant figure it out any help much appreciated


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by Mythic Fr0st on Sat Dec 16, 2006 6:30 pm, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Check your MySQL table - specifically that the field 'id' is indeed an index.

Cheers,
Kieran
Mythic Fr0st
Forum Contributor
Posts: 137
Joined: Sat Dec 02, 2006 3:23 am
Contact:

hmm

Post by Mythic Fr0st »

Thanks for replying

Well, its been fixed now, thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

There query parameters havn't been secured for usage in a sql query.
see (again) http://en.wikipedia.org/wiki/SQL_injection and http://de2.php.net/mysql_real_escape_string
Post Reply