LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2004, 03:10 PM   #1
Boffy
Member
 
Registered: Jul 2003
Location: Durham, UK
Distribution: Ubuntu 8.04
Posts: 419

Rep: Reputation: 30
Whats wrong with this simple code


Why isnt this working?

Code:
<?php
 $dbh=mysql_connect ("localhost", "firstli_michael", "****") or die ('I cannot connect to the database because:' . mysql_error());
mysql_select_db ("firstli_rock",$dbh);
#$sql = 'INSERT INTO messenger (Username,Password,IP,Online) VALUES ('.$_GET['Username'].', '.$_GET['Password'].', '.$_GET['IP'].', '.$_GET['Online'].')';
$sql = 'INSERT INTO messenger (Username,Password,IP,Online) VALUES ('.$_GET['Username'].','.$_GET['Password'].','.$_GET['IP'].','.$_GET['Online'].')';
$result = mysql_query($sql,$dbh);
mysql_close($dbh);
echo $sql.$result
?>
Boffy
 
Old 10-28-2004, 03:35 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Are you getting any errors or output at all?

If not then try adding die or print statements to check if the mysql_select_db and mysql_query statements fail. I quite often use this type of statement:
$query=mysql_query($sql) or mysql_failed(__FILE__, __LINE__);

Then have a function called mysql_failed:
Code:
function mysql_failed($file,$line){
Global $qstring, $sql, $sqlerror;

print "<hr>MySQL error on <b>line $line</b> of <b>$file</b>.<br>
Query was: <i>$sql</i><br>
Error was: <i>".mysql_error()."</i><hr>\n\n";
}
You can also add a mail section to the function if you want to
 
Old 10-29-2004, 05:08 PM   #3
Boffy
Member
 
Registered: Jul 2003
Location: Durham, UK
Distribution: Ubuntu 8.04
Posts: 419

Original Poster
Rep: Reputation: 30
Its an"ERROR 1054" and i get

Query was: INSERT INTO messenger (Username,Password,IP,Online) VALUES (Michael,hello,25,no)
Error was: Unknown column 'Michael' in 'field list'

Boffy
 
Old 10-29-2004, 05:30 PM   #4
Boffy
Member
 
Registered: Jul 2003
Location: Durham, UK
Distribution: Ubuntu 8.04
Posts: 419

Original Poster
Rep: Reputation: 30
I thnk its the $_GET part thats causeing the trouble but i dont know how to overcome it. Other sites reccomend things and they look link mine but it still wont work
 
Old 10-30-2004, 03:43 AM   #5
cbe
LQ Newbie
 
Registered: Jul 2001
Posts: 19

Rep: Reputation: 0
I believe this is what you are after:

This should solve your $_GET issues.


config.php
Code:
<?   
        // database server
        $DB_SERVER = "localhost";
        // database name
        $DB_NAME = "name";   
        // database user
        $DB_USER = "user";  
        // database password
        $DB_PASSWD = "password";  

?>
lq.php
Code:
<?   
/*
If you call the page as such: 
yourwebserver.com/lq.php?Username=Mike&Password=1234&IP=123.12.12.1&Online=no
*/

include("config.php");

 $dbh=mysql_connect ("$DB_SERVER", "$DB_USER", "$DB_PASSWD");                                                                       
mysql_select_db ($DB_NAME,$dbh);                        
$sql = "INSERT INTO messenger VALUES ('" . $_GET['Username'] . "','" . $_GET['Password'] . "','" . $_GET['IP'] . "','" . $_GET['Online'] . "')";

$result = mysql_query($sql,$dbh) OR DIE ("Bad query INSERT_1: " .mysql_error());;
mysql_close($dbh);
echo $sql.$result
?>

Hope I answered your question,
Take care
 
Old 10-30-2004, 09:54 AM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
che's sugestion should fix it. I missed it in your first post but you need to have quotes around each string you try to insert. It is also worth escaping these strings with addslashes to help protect agains sql injection attacks.
 
Old 10-31-2004, 01:16 PM   #7
Boffy
Member
 
Registered: Jul 2003
Location: Durham, UK
Distribution: Ubuntu 8.04
Posts: 419

Original Poster
Rep: Reputation: 30
Success, thanks a lot.

Boffy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
whats wrong with this code in grub? HmmLinux Linux - General 2 11-12-2004 03:50 AM
Whats wrong with this file? hermittlog Linux - Newbie 4 10-17-2004 08:07 PM
Whats wrong with this script??? Sammy2ooo Linux - Networking 2 08-24-2004 06:20 AM
whats wrong with this simple code?? laclac01 Programming 3 08-23-2004 03:52 PM
Whats wrong with this? OlRoy Programming 2 05-28-2003 01:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration