0

I cant get the value of the session variable to insert it into my DB. lat and lng are Doubles

$lat = mysql_real_escape_string($lat);
$lng = mysql_real_escape_string($lng);


$sql="INSERT INTO POINT (LocationLat,LocationLngme,UserName) VALUES (&lat,&lng,$usr)";
mysql_query($sql);

?>
Zhexa
  • 5
  • 3

2 Answers2

6

session_start() must be at the top of any page you wish to use sessions:

<?php
session_start();
$lat = $_REQUEST['lat'];
$lng = $_REQUEST['lng'];
$usr = "'".echo $_SESSION['username']."'";

$lat = mysql_real_escape_string($lat);
$lng = mysql_real_escape_string($lng);


$sql="INSERT INTO POINT (LocationLat,LocationLngme,UserName) VALUES ('&lat','&lng','$usr')";
mysql_query($sql);

?>

FYI, you are missing quotes around your string values in your query.

And please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

You are also wide open to SQL injections

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

@Jonh Conde's answers might work for you but it is better to do the following:

<?php
session_start();
$lat = mysql_real_escape_string( trim( $_REQUEST['lat'] ) );
$lng = mysql_real_escape_string( trim( $_REQUEST['lng'] ) );
$usr = mysql_real_escape_string( trim( $_SESSION['username'] ) );

$sql="INSERT INTO `POINT` (`LocationLat`, `LocationLngme`, `UserName`) VALUES ('$lat', '$lng', '$usr')"; 
// Putting '`' backticks around column and table names makes sure some mysql errors are prefented.
mysql_query($sql);

?>

Also take a look at mysqli_* or PDO since mysql_* is depracted

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
  • 1
    *"backticks aroung column and table names makes sure some mysql errors are prefented."* - Actually, backticks are used whenever an OP uses a reserved word, or a table/column contains a space or hyphen. – Funk Forty Niner Dec 17 '14 at 19:02
  • @Fred-ii- "*backticks are used whenever an OP uses a reserved word, or a table/column contains a space or hyphen*" Thats what I ment. But it usually is a good thing too to do / learn to do in general to prevent errors. – SuperDJ Dec 17 '14 at 19:06
  • @Zhexa As @ John Conde stated: "*That's their next question. ;) But I should mention it so this answer is not providing bad information.*" – SuperDJ Dec 17 '14 at 19:11