Reputation: 3
<?php
include 'config.php';
$lat=$_GET["latitudee"];
$lon=$_GET["longitude"];
$sql="INSERT INTO coordinates(latitude,longitude)
VALUES ('$_POST[$lat]','$_POST[$lon]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
?>
This is the table i have in my database :
CREATE TABLE IF NOT EXISTS `coordinates` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`latitude` float NOT NULL,
`longitude` float NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
http://localhost.com/input.php?latitudee=3.14159&longitude=4.14159
values are not being inserted. what is the problem?
Upvotes: 0
Views: 240
Reputation: 993
first you have to use $lat at the place of $_POST[$lat].since you have already assign $lat=$_GET["latitudee"]; so why are you using $_POST[$lat].please use below code.
<?php
include 'config.php';
$lat=$_GET["latitudee"];
$lon=$_GET["longitude"];
$sql="INSERT INTO coordinates(latitude,longitude)
VALUES ('$lat','$lon')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
?>
Upvotes: 0
Reputation: 1
```
<?php
include 'config.php';
//--
echo isset($_GET["latitudee"]) ?: 'GET array element with index latitudee is not set';
echo isset($_GET["longitude"]) ?: 'GET array element with index longitude is not set';
//--
$lat=$_GET["latitudee"];
$lon=$_GET["longitude"];
//--
echo isset($_POST[$lat]) ?: 'POST array element with index ' . $lat . ' is not set';
echo isset($_POST[$lon]) ?: 'POST array element with index ' . $lon . ' is not set';
//--
$sql="INSERT INTO coordinates(latitude,longitude)
VALUES
('$_POST[$lat]','$_POST[$lon]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
Upvotes: 0
Reputation: 134
Use this
<?php
include 'config.php';
$lat=$_GET["latitudee"];
$lon=$_GET["longitude"];
$sql="INSERT INTO coordinates(latitude,longitude)
VALUES ('$lat','$lon')";
$In_qrr = mysqli_query($con,$sql);
if (!In_qrr)
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
?>
Upvotes: 0
Reputation: 3033
try this:
$sql="INSERT INTO coordinates(latitude,longitude)
VALUES
('$lat','$lon')";
bacause you stored get value in variable like this :
$lat=$_GET["latitudee"];
$lon=$_GET["longitude"];
and in your query you write this:
$_POST[$lat]','$_POST[$lon]
so change it like i suggest.
Side note: make sure what method you used: POST or GET
and escape
the variable to prevent sql injection.
Read this:How can I prevent SQL injection in PHP?
Upvotes: 5