Helpful Information
 
 
Category: MySQL and other databases
PHP & MySQL Problem

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.

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'];
}

Great. Thanks Twey. :D

Thanx for the posting of the problem and for answering it, I had almost the same unluck, so used the info

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.










privacy (GDPR)