Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:36:14+00:00 2026-06-12T21:36:14+00:00

Im not very experienced in PHP and would appreciate any help! Im am sending

  • 0

Im not very experienced in PHP and would appreciate any help! Im am sending a username, longitude and latitude to my server using HTTP Post. I first check if the user exists in the user_info table. If it does then i try to update the maps_user_location table with that username. What i want to happen is that if the query fails, due to the username not being found in maps_user_location table, then i want it to be added. I’ve tried to do it with the code below.

$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$username = $_POST['username'];

if ($latitude != 0 && $longitude != 0) {
    $query_userCheck = "SELECT * FROM user_info WHERE login_username = '".$username."';";
    if ( mysql_query($query_userCheck) ) {
        $query_updateLocation =  "UPDATE  `maps_user_location` SET  `lat` =  '".$latitude."',`long` =  '".$longitude."' WHERE  
        `login_username` = '".$username."';";
        if ( !mysql_query($query_updateLocation) ){
            $query_newLocation = "INSERT INTO `maps_user_location` ( `id` ,`login_username` ,`lat` ,`long` ,`track`)
            VALUES ( NULL ,  '".$username."',  '".$latitude."',  '".$longitude."',  '0');";

            mysql_query($query_newLocation);
        }

    }
}

When i execute the code, it goes through without a hitch. The problem is that a row is never inserted if the username does not exist already, the row does update if the username exists though. Additionally, when i add a row manually, the id column seems to increment after every request (the sequence will go 1,2,3,4,10,11,12…etc [the ids 5-9 do not exist]). I have a feeling that it has to do with the ‘WHERE’ in ‘$query_updateLocation’. Anyone have any clue as to why this happens?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:36:16+00:00Added an answer on June 12, 2026 at 9:36 pm

    You can simply do that in a single query, Use INSERT...ON DUPLICATE KEY UPDATE

    INSERT INTO  maps_user_location ( login_username , lat, long, track)
    VALUES ('".$username."',  '".$latitude."', '".$longitude."',  '0')
            ON DUPLICATE KEY UPDATE
                 lat    =  '".$latitude."' ,
                long  =  '".$longitude."'
    

    but before anything else, make sure that you have a UNIQUE CONSTRAINT on the column login_username. If you have not added, you can alter your table using this command

    ALTER TABLE maps_user_location ADD UNIQUE (login_username);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( Updated a little ) I'm not very experienced with internationalization using PHP, it
Question related to PHP memory-handling from someone not yet very experienced in PHP: If
I am not very experienced with PHP. I have a site I'm maintaining that
Living in 2012 I did not think a image upload using PHP would cause
I did some searching around, but I'm not very experienced in PHP, so I
I am not very experienced with database and php and I have a problem.
I'm not very experienced in ASP.Net, as I work mostly with PHP. I'm trying
I'm not very experienced in C#, done some small stuff with it, but it's
I'm not very experienced with JavaScript, but this code was similar to what I
I'm a long time developer but not very experienced with DNS. Here's my problem:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.