How to not have duplicates in mysql rows

the below code works fine does what i want it to. but i noticed it duplicated records and they are not unique how to i in mysqli not allow for dups and set the one at uid=(some number) and if if finds the number it updates the record.
$test_map_arrays = array
( array( "lat_lng_obtained" => "capture" , "uid" => $user_id , "lat_lng" => $lat_lng_obtained, "user_ip" => $_SERVER['REMOTE_ADDR'] )
)
;


// PREPARE A QUERY TO LOAD OUR DATA INTO THE TABLE
$sql = "INSERT INTO user_map_info_pref ( lat_lng_obtained, user_id, lat_lng, user_ip) VALUES ( ?,?,?,? )";
$ins = $mysqli->prepare($sql);
if (!$ins)
{
    $err
    = "QUERY PREPARE() FAIL: "
    . $sql
    . '<br>ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    ;
    trigger_error($err, E_USER_ERROR);
}

// NB: THESE VARIABLES MUST EXIST BEFORE THEY CAN BE BOUND!
$map_info = current($test_map_arrays);

// BIND INPUT VARIABLES TO THE QUERY
$ins->bind_param('ssss', $map_info['lat_lng_obtained'], $map_info['uid'] , $map_info['lat_lng'], $map_info['user_ip']);

// USE AN ITERATOR TO LOAD THE TABLE
foreach ($test_map_arrays as $map_info)
{
    // RUN THE QUERY TO INSERT THE ROW
    $ins->execute();

    // GET THE AUTO_INCREMENT ID FROM CONNECTION, NOT FROM STATEMENT
    $id  = $mysqli->insert_id;
    //echo "MySQLI INSERTED A ROW CONTAINING <b>{$map_info['user_ip']}, {$map_info['lat_lng_obtained']}, {$map_info['uid']}, {$map_info['lat_lng']}</b> WITH AUTO_INCREMENT ID = $id" . PHP_EOL;
    $_SESSION['how_lat_lng_obtained']=$map_info['lat_lng_obtained'];
    $_SESSION['current_lat_lng']=$map_info['lat_lng'];
    //$_SESSION['subdomain']=$subdomain;


}

Open in new window


i tried
$sql = "UPDATE user_map_info_pref SET lat_lng_obtained = ?,user_ip = ?, lat_lng = ? WHERE user_id = ?"; 

Open in new window


and absolutely nothing happened no errors nothing
im really starting to not like mysqli - yes its more powerful but its really hard to understand, and is way too complicated, it also most to the point with them changing it i may just stay with php version that allows mysql queries and not upgrade php any more. this really sucks forced redesign, and i have so many sites to do like this. its almost not worth it for conversion..php jerks have to change it leave both ways in place u dum screwballs. ok enough of the rant now. how do i fix my problem i never played with update (in mysql) and i cant find any direct examples for mysqli update that work. and trial and error not working here

thank you in advance for any code or help you may provide
Johnny
JohnnyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SStoryCommented:
I'm not sure I understand, but are you saying you need a WHERE clause to only update one record for that userid?

If so that can be added to the INSERT.

WHERE user_id=$user_id

IF not please show sample data of what you do want and do not want.
0
JohnnyAuthor Commented:
mind you im working with mysqli here ok. and im not getting this at all. i hate mysqli to hard to understand it its not apparent 'out of the box' and so different from mysql, sorry i just do not like this at all.

im trying to add a record via user id if its there update it and not make a new one like it is now with above code. i have never used update so i do not understand how it knows if its there or not and if it should be updated or injected. but id like to do it for each user_id as thats my save for the info its saving per user_id. tracked maybes a better word sorry its got me so flustered i cant think right

this does not work nothing happens no errors nothing yet again.
$sql = "INSERT INTO user_map_info_pref ( lat_lng_obtained, lat_lng, user_ip, WHERE user_id = $user_id) VALUES ( ?,?,?, ? )";

Open in new window

0
GaryCommented:
You are wanting to insert but if exists then update?

INSERT INTO table (col1,col2,col3) VALUES (?,?,?) ON DUPLICATE KEY UPDATE col2= VALUES(col2),col3=VALUES(col3)

Open in new window

This is assuming col1 is unique in the table
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Tomas Helgi JohannssonCommented:
Hi!

Does your user_map_info_pref table have any primary key or unique key ?
Inserting data into a table that is later "defined" as duplicate usually means that the table lacks either a  unique key or a primary key or if they exists then a better one.
The API, either mysqli or PDO, behave exactly the same when working with a table.

You can issue a INSERT .... DUPLICATE KEY .. statement to do a replace of data if hit a -803 (duplicate key).
http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html
http://dev.mysql.com/doc/refman/5.5/en/insert.html

But that is as I said before dependent on correct primary/ unique keys.

Regards,
    Tomas Helgi
0
JohnnyAuthor Commented:
CREATE TABLE IF NOT EXISTS `user_map_info_pref` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `user_id` mediumint(9) NOT NULL,
  `lat_lng` varchar(250) NOT NULL,
  `subdomain_lat_lng` varchar(250) NOT NULL,
  `user_lat_lng` varchar(250) NOT NULL,
  `lat_lng_obtained` enum('capture','subdomain','user_input','unknown') NOT NULL DEFAULT 'unknown',
  `user_device` varchar(250) NOT NULL,
  `user_city_st_zip` varchar(250) NOT NULL,
  `user_default_start_pont` varchar(250) NOT NULL,
  `user_map_radius` int(9) NOT NULL,
  `admin_notes` text NOT NULL,
  `user_ip` varchar(15) NOT NULL,
  `last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Open in new window


tried
$sql = "INSERT INTO user_map_info_pref ( lat_lng_obtained, user_id, lat_lng, user_ip) VALUES ( ?,?,?,? ) ON DUPLICATE KEY UPDATE lat_lng_obtained= VALUES('capture'),lat_lng=VALUES($lat_lng_obtained),user_ip= VALUES($user_ip)";

Open in new window

this is not working either
0
JohnnyAuthor Commented:
$sql = "INSERT INTO user_map_info_pref ( lat_lng_obtained, user_id, lat_lng, user_ip) VALUES ( ?,?,?,? ) ON DUPLICATE KEY UPDATE lat_lng_obtained= 'capture',lat_lng='$lat_lng_obtained',user_ip='$user_ip')";

Open in new window

no joy
0
GaryCommented:
The values part in the on update should be the column names i.e.
$lat_lng_obtained should be lat_lng_obtained etc

$sql = "INSERT INTO user_map_info_pref ( lat_lng_obtained, user_id, lat_lng, user_ip) VALUES ( ?,?,?,? ) ON DUPLICATE KEY UPDATE lat_lng_obtained= VALUES(lat_lng_obtained),lat_lng=VALUES(lat_lng),user_ip= VALUES(user_ip)"; 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnnyAuthor Commented:
that did it thank you so much
0
Tomas Helgi JohannssonCommented:
Hi!

Try creating a table without the AUTOINCREMENT column and try to insert into that table.

CREATE TABLE IF NOT EXISTS `user_map_info_pref_temp` (
  `user_id` mediumint(9) NOT NULL,
  `lat_lng` varchar(250) NOT NULL,
  `subdomain_lat_lng` varchar(250) NOT NULL,
  `user_lat_lng` varchar(250) NOT NULL,
  `lat_lng_obtained` enum('capture','subdomain','user_input','unknown') NOT NULL DEFAULT 'unknown',
  `user_device` varchar(250) NOT NULL,
  `user_city_st_zip` varchar(250) NOT NULL,
  `user_default_start_pont` varchar(250) NOT NULL,
  `user_map_radius` int(9) NOT NULL,
  `admin_notes` text NOT NULL,
  `user_ip` varchar(15) NOT NULL,
  `last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

Open in new window


Insert into table with AUTO_INCREMENT columns always generate a new row with new value.

{Removed irrelevant link- GaryC123}
http://bugs.mysql.com/bug.php?id=37560
http://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html

Regards,
    Tomas Helgi
0
JohnnyAuthor Commented:
@  Tomas Helgi thank you i dropped the id col
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.