-2

I am using the Google Maps API for making routes. When I define a route, the value goes into the database but returns an empty value. The primary key in the database is incremented but other values are empty. Any idea?

<?php
session_start();

// connection to mysql
$latitude=$_POST['Lat'];
$longitude=$_POST['Long'];
$start_point=$_POST['s_point'];
$end_point=$_POST['e_point'];

$link = mysqli_connect("localhost","root","123456","db_mmw") or die ("Error" .mysqli_errno($link));

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO tbl_route (latitude,longitude,start_point,end_point)  VALUES ('".$latitude."','".$longitude."','".$start_point."','".$end_point."')";

if (!mysqli_query($link,$sql))
  {die('Error: ' . mysqli_error($link));
}
mysqli_close($link); ?>
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69

1 Answers1

2

What you posted in your (now deleted) comment is that your form contains name="long" and name="lat"

Those don't match your:

 $latitude=$_POST['Lat']; // uppercase L
 $longitude=$_POST['Long']; // uppercase L
 $start_point=$_POST['s_point'];
 $end_point=$_POST['e_point'];

Change it to:

 $latitude=$_POST['lat']; // lowercase l
 $longitude=$_POST['long']; // lowercase l
 $start_point=$_POST['s_point'];
 $end_point=$_POST['e_point'];
  • PHP variables are case-sensitive

Sidenote: Your present code is open to SQL injection. Use prepared statements, or PDO

Edit/Rewrite:

<?php
session_start();

$link = mysqli_connect("localhost","root","123456","db_mmw") or die ("Error" .mysqli_errno($link));

// connection to mysql
$latitude=$_POST['lat'];
$longitude=$_POST['long'];
$start_point=$_POST['s_point'];
$end_point=$_POST['e_point'];



// Check connection
if (mysqli_connect_errno())
    {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

$sql="INSERT INTO tbl_route (latitude,longitude,start_point,end_point)  VALUES ('".$latitude."','".$longitude."','".$start_point."','".$end_point."')";

if (!mysqli_query($link,$sql))
    { 
  die('Error: ' . mysqli_error($link));
    }
mysqli_close($link);

?>

This is another issue:

<input type="submit" name="Define Route" value="Define Route"

is outside your form which will prevent it from working, plus there was a missing > and should have read as:

<input type="submit" name="Define_Route" value="Define Route">

I changed it name="Define Route" to name="Define_Route" just in case.

<div class ="form"> 
<form method="post" action=""><br> <table border="0"> 
<tr> <td>Start Point</td><td> 
<input type="text" name="s_point"></td> </tr> <tr> 
<td>End Point</td><td> <input type="text" name="e_point"></td> </tr> 
<tr> <td> Latitude </td><td> <input type="text" name="lat"></td> </tr> 
<tr> <td> Longitude </td><td> <input type="text" name="long"></td> </tr> </table> 

<input type="submit" name="Define_Route" value="Define Route">

</form> 
<br><br> </div> 
</div>

Complete rewrite with form that worked for me.

Using VARCHAR(255) for all four columns.

(Use in one file)

<?php
session_start();

$link = mysqli_connect("localhost","root","123456","db_mmw") or die ("Error" .mysqli_errno($link));

if(isset($_POST['Define_Route'])){
// connection to mysql
$latitude=$_POST['lat'];
$longitude=$_POST['long'];
$start_point=$_POST['s_point'];
$end_point=$_POST['e_point'];


// Check connection
if (mysqli_connect_errno())
    {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

$sql="INSERT INTO tbl_route (latitude,longitude,start_point,end_point)  VALUES ('".$latitude."','".$longitude."','".$start_point."','".$end_point."')";

if (!mysqli_query($link,$sql))
    { 
  die('Error: ' . mysqli_error($link));
    }

} // brace for if(isset($_POST['Define_Route']))

mysqli_close($link);

?>

<div class ="form"> 
<form method="post" action=""><br> <table border="0"> 
<tr> <td>Start Point</td><td> 
<input type="text" name="s_point"></td> </tr> <tr> 
<td>End Point</td><td> <input type="text" name="e_point"></td> </tr> 
<tr> <td> Latitude </td><td> <input type="text" name="lat"></td> </tr> 
<tr> <td> Longitude </td><td> <input type="text" name="long"></td> </tr> </table> 

<input type="submit" name="Define_Route" value="Define Route">

</form> 
<br><br> </div> 
</div>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Make sure that all columns exist with proper column types and have no typos in it. You should post your form and DB schema in your question. @user3542393 This should work. – Funk Forty Niner Apr 16 '14 at 19:22
  • now its giving following errors. Undefined index: lat in C:\wamp\www\fyp\actions\new_route.php on line 5 Undefined index: long in C:\wamp\www\fyp\actions\new_route.php on line 6 Undefined index: s_point in C:\wamp\www\fyp\actions\new_route.php on line 7 Undefined index: e_point in C:\wamp\www\fyp\actions\new_route.php on line 8 – Mirza Ghazanfar Apr 16 '14 at 19:25
  • Show me the actual HTML form. @user3542393 Even if you have to post it here in a comment, I'll take care of it if you can't post it in your question. – Funk Forty Niner Apr 16 '14 at 19:26

  • Start Point
    End Point
    Latitude
    Longitude


    – Mirza Ghazanfar Apr 16 '14 at 19:30
  • i also echo the querry but the result of querry is: INSERT INTO tbl_route (latitude,longitude,start_point,end_point) VALUES ('','','','') – Mirza Ghazanfar Apr 16 '14 at 19:32
  • Theoretically, you shouldn't have a space inside `name="Define Route"` try with an underscore `name="Define_Route"` however I doubt that would make a difference. Reload my answer and look under "**Edit/Rewrite**" to make sure you did in fact copy it correctly. @user3542393 What are your column types for `latitude` and `longitude` and do they both in fact exist? – Funk Forty Niner Apr 16 '14 at 19:36
  • Wait, reload my answer again under "Edit/Rewrite". I placed your `$link...` DB connection first. Try it now. @user3542393 – Funk Forty Niner Apr 16 '14 at 19:37
  • plz give me ur skype id or gmail id i can send u my database and html too – Mirza Ghazanfar Apr 16 '14 at 19:42
  • Something else I noticed, `` and change it to `` there was a `>` missing @user3542393 – Funk Forty Niner Apr 16 '14 at 19:48
  • See under "**Complete rewrite with form that worked for me.**" (in my edit) that worked for me. @user3542393 I don't have Skype, sorry. – Funk Forty Niner Apr 16 '14 at 19:57
  • Great news, glad to hear it. You're welcome. @user3542393 Click on the **white gray outline checkmark/tick** next to my answer till it turns **Green** to accept as answered. cheers (It's right under the little up and down arrows over to the left of my answer). – Funk Forty Niner Apr 16 '14 at 19:58