-
So I am making progress slowly but surely (in a great part due to the advice received here, so thanks for that). I currently have some error checking working and the script is INSERT ing the data into my database. I need to make sure that duplicate data is not entered into the database as well and have been looking at these SQL syntax options to accomplish it (INSERT...IGNORE, INSERT...ON DUPLICATE KEY UPDATE, and REPLACE INTO) does it really matter which I use or is there a better way?
Here is my current code
PHP Code:
<?php
require_once('connectvars.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s \n", $mysqli->connect_error);
exit();
}
$player_name = 'itivae'; //$_POST['playername'];
$player_id = '10101038'; //$_POST['playerid'];
$guid = '4e72f140c35df55909ce551273c44007'; //$_POST['guid'];
/*if(!isset($_POST['playername']) ||
!isset($_POST['playerid']) ||
!isset($_POST['guid']) || {
die('We are sorry, but there appears to be a problem with the form you submitted.');
}*/
if(mb_strlen($guid)<32){
echo 'the guid checked is less than 32 characters';
}
if(mb_strlen($guid)>32){
echo 'the guid checked is more than 32 characters';
}
if(mb_strlen($player_id)<8){
echo ' the player_id you entered is less than 8 characters';
}
if(mb_strlen($player_id)>8){
echo ' the player_id you entered is more than 8 characters';
}
if(mb_strlen($guid)==32 && mb_strlen($player_id)==8){
$query = "SELECT * FROM player_bans WHERE banned_guid = '$guid'";
if ($result = $mysqli->query($query)){
while ($obj = $result->fetch_object()) {
printf ('<div class="bans">'."%s", $obj->banned_guid);
printf (' appears in the ban list' . '</div>');
$mysqli->close();
exit;
}
}
$q = "INSERT INTO player_data (player_name, player_id, guid)
VALUES ('$player_name', '$player_id', '$guid')";
//Should I duplicate check here?
if($r = $mysqli->query($q)) {
//or here?
echo 'Player: ' . $player_name . ' ' . $player_id . ' ' . $guid . ' has been added to the whitelist.';
}
else {
printf("error: %s\n", mysqli_error($mysqli));
}
}
$mysqli->close();
?>
Thanks
-
I used this
PHP Code:
$duplicateResult=$mysqli->query("SELECT `player_name`,`player_id`,`guid` FROM `player_data` WHERE `player_name`='$player_name' OR `player_id`='$player_id' OR `guid`='$guid'");
if($duplicateResult->num_rows>=1) {
echo 'Some of the information already exists in the database';
$mysqli->close();
exit;
}
but I am having trouble if the duplicate message is triggered
PHP Code:
<?php
require_once('connectvars.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s \n", $mysqli->connect_error);
exit();
}
$player_name = 'itivae'; //$_POST['playername'];
$player_id = '10101038'; //$_POST['playerid'];
$guid = '4e72f140c35df55909ce551273c44007'; //$_POST['guid'];
/*if(!isset($_POST['playername']) ||
!isset($_POST['playerid']) ||
!isset($_POST['guid']) || {
die('We are sorry, but there appears to be a problem with the form you submitted.');
}*/
if(mb_strlen($guid)<32){
echo 'the guid checked is less than 32 characters';
}
if(mb_strlen($guid)>32){
echo 'the guid checked is more than 32 characters';
}
if(mb_strlen($player_id)<8){
echo ' the player_id you entered is less than 8 characters';
}
if(mb_strlen($player_id)>8){
echo ' the player_id you entered is more than 8 characters';
}
if(mb_strlen($guid)==32 && mb_strlen($player_id)==8){
$query = "SELECT * FROM player_bans WHERE banned_guid = '$guid'";
if ($result = $mysqli->query($query)){
while ($obj = $result->fetch_object()) {
printf ('<div class="bans">'."%s", $obj->banned_guid);
printf (' appears in the ban list' . '</div>');
$mysqli->close();
exit;
}
}
$duplicateResult=$mysqli->query("SELECT `player_name`,`player_id`,`guid` FROM `player_data` WHERE `player_name`='$player_name' AND `player_id`='$player_id' AND `guid`='$guid'");
if($duplicateResult->num_rows>=1) {
echo 'Some of the information already exists in the database';
$mysqli->close();
exit;
}
$q = "INSERT INTO player_data (player_name, player_id, guid)
VALUES ('$player_name', '$player_id', '$guid')";
if($r = $mysqli->query($q)) {
echo 'Player: ' . $player_name . ' ' . $player_id . ' ' . $guid . ' has been added to the whitelist.';
}
}
$mysqli->close();
?>