Weborum Webmaster Forum > Failing to write to my database (mysql)
Help - Search - Members - Calendar
Full Version: Failing to write to my database (mysql)
Weborum Webmaster Forum > Web Page Design > PHP
cordob
Hi everybody (I am new here0

I have a website which uses php and mysql to extract news items and other info from a database.

I want to add something which will register raw visitor data to my database so I can do my own analysis.

So I added a table called visitors with structure(starting simple):
QUOTE
Time    timestamp(14)     
IPaddress  varchar(16) 
Browser  varchar(16) 


Then I try in my access routine to add soemthing to write into this table, but soehow this hangs. maybe somebody here has an idea why:

The code looks like:
QUOTE
echo "before insert ";
$sql= "INSERT INTO visitors SET
  Time = NULL,
  IPaddress = 'IP1234567',
  Browser = 'Netscape7.0'";

// perform the insert
if(!@msql_query($sql)) echo ('There is a problem now with that...' . mysql_error());


I just see the text: "before insert" and it then hangs.

I know the database is open all right, so at least I had expected an error message out of the last statement.

Any ideas/

tia

Cor
Joe
PHP
$db = mysql_connect("localhost","username","password");
mysql_select_db(database_name, $db);
$requete = "INSERT INTO visitors Time, IPAddress, Browser VALUES ('$time', '$ipaddress', '$browser');
$result = mysql_query ($requete,$db);


Should work ...
Josh
isn't it
PHP

$db = mysql_connect("localhost","username","password");
mysql_select_db(database_name, $db);
$requete = "INSERT INTO visitors(Time, IPAddress, Browser) VALUES ('$time', '$ipaddress', '$browser');
$result = mysql_query ($requete,$db);

cordob
Both guys thanks for the help, got it working.

Indeed needed the extra brackets around teh fields of Josh. (and also an extra quote at the end ). PPHP/Mysql is so fininicky with the double quotes, single quotes, back quotes etc it is somethines a struggle.

So in the end it became:

QUOTE
$sql= "INSERT INTO visitors (Time , IPaddress , Browser) VALUES ('$time','$ipaddress','$browser')";


Thanks anyway, got me there quicker the help.

Do you also happen to know where I find the variables one can pick up such as IPaddress, from where the page was referred, browser etc in PHP? There must be a list somewhere...

tia
Cor
bassrek
Josh, is that really you? smile.gif

This ought to help, cordob:
http://us2.php.net/reserved.variables#rese...ariables.server

In your sepcific case, you're looking for REMOTE_ADDR and HTTP_REFERER.
cordob
Fantastic, thanks a lot.

Cheers
Cor
cordob
Varuables like referrer are workign OK, but I don't seem to b able to get a time and date.

I thought variables like CURTIME() should work but they don't give me any value apparently.

Cheers
Cor
cordob
Got it all working with date() and time() fundctions of PHP, great.

Cheers
Cor
Joe
$date() and $time() should work.
xanderman
$sql= "INSERT INTO visitors (Time , IPaddress , Browser) VALUES ('$time','$ipaddress','$browser')";

should be this....but hey if it ain't broke dont fix it lol

$sql= "INSERT INTO `visitors` (`Time` , `IPaddress` , `Browser`) VALUES ('$time','$ipaddress','$browser')";
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.