PDA

View Full Version : PHP & MySQL Problem



InNeedofHelp
06-16-2006, 03:00 PM
Hey DD,

I have a problem inserting information from a post into my MySQL database. I don't really know what the problem is or how to fix it, so i'm hoping one of you guys can help me. Here's the code:


function showMessages() {
if (!empty($_REQUEST['message']) and !$_REQUEST['message']=="") {
$chatText = $_REQUEST['message'];
$chatName = $_REQUEST['username'];
mysql_query("insert into Chat VALUES (0, '$chatText', '$chatName')",$con) or die('Problem sending message: ' . mysql_error());
echo $_REQUEST['message'];
}
}

I've located the problem somewhere in that function, but I don't know what the problem is. The error message i recieve when this function is called is this:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\web\htdocs\chatroom.php on line 98
Problem sending message:

Anyway, I really need help on this one, so if anyone can help, I would greatly appreciate it.

Thanks.

Twey
06-16-2006, 03:22 PM
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\web\htdocs\chatroom.php on line 98$con is never defined. If it was defined outside the function, you need to explicitly
global $con;before using it. A better idea would be to pass it as an argument. I personally would write that function like so:
function showMessages($con = false) {
if($con === false) global $con;
if (empty($_REQUEST['message'])) return;

$chatText = mysql_real_escape_string($_REQUEST['message']);
$chatName = mysql_real_escape_string($_REQUEST['username']);

mysql_query("insert into Chat VALUES (0, '$chatText', '$chatName');") or die('Problem sending message: ' . mysql_error());
echo $_REQUEST['message'];
}

InNeedofHelp
06-16-2006, 03:27 PM
Great. Thanks Twey. :D

Chaban
08-13-2007, 09:01 AM
Thanx for the posting of the problem and for answering it, I had almost the same unluck, so used the info

Twey
08-13-2007, 08:11 PM
Readers of this thread should note that in he code I posted $con was entirely redundant anyway, since it was never passed to either of the mysql_* functions so they did the default of simply using the last connection opened.