Reputation: 11
I am making an Android application. Each time the application closes, it inserts a GCM Registration ID into the database, but there is no need for a single GCM Registration ID to be inserted more than one time.
How does one prevent duplicate data from being inserted into a MySQL database?
<?php
// response json
$json = array();
/**
* Registering a user device
* Store reg id in users table
*/
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["regId"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$gcm_regid = $_POST["regId"]; // GCM Registration ID
// Store user details in db
include_once './db_functions.php';
include_once './GCM.php';
$db = new DB_Functions();
$gcm = new GCM();
$res = $db->storeUser($name, $email, $gcm_regid);
$registatoin_ids = array($gcm_regid);
$message = array("product" => "shirt");
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
} else {
// user details missing
}
?>
Upvotes: 1
Views: 543
Reputation: 1020
I would use the MySQL Unique Constraint for this:
http://dev.mysql.com/doc/refman/en/constraint-primary-key.html
Otherwise, you can enact this programmatically by only inserting if an identity query produces an empty result.
Upvotes: 2
Reputation: 884
If you want prevent entering duplicated data into your columns, make that columns unique.
Upvotes: 0
Reputation: 1022
What I usually do is I find out wheter or not the row exists, if it does, I update it, otherwise I insert it.
Upvotes: 0