Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Primary Key Duplicate Entry - User Message (mysqli.errno)

Featured Replies

Hello!

 

I am new around here and learning PHP. I am making a website with a database for a project I am working on for fun which I made using MySQL but since reading up on MySQLi I have translated the code which I believe is right but please correct me but my problem I need help with is the code below I need to have a message appear on screen to the user letting them know that the gamertag already exists if they try and add one which is already in the database, I already have the gamertag as the primary key so I currently get "Duplicate entry 'xbox-gamertag' for key 1" what code would I use and where would it go from research I have found mysqli.errno but I can't see how to implement it.

 

Thanks in advance

<?php
$conn = ($GLOBALS["___mysqli_ston"] = mysqli_connect("HOST", "USER-NAME", "PASSWORD!"));
$db = ((bool)mysqli_query($conn, "USE DATEBASE NAME"));
?>

<?php

$yourname = $_POST['user-name'];
$xboxgamertag = $_POST['xbox-gamertag'];
$game1 = $_POST['game1'];
$game2 = $_POST['game2'];
$game3 = $_POST['game3'];


$sql = "INSERT into xbox values('$yourname','$xboxgamertag','$game1','$game2','$game1')";
$qury = mysqli_query($GLOBALS["___mysqli_ston"], $sql);

if(!$qury) 
echo ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) :  (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) ;
else
{
echo "Success, $yourname!  Your now added to GamerTag Finder with the gamertag $xboxgamertag and want people to add you for $game1, $game2 and $game3.";
echo "<a href='xboxgamertags.php'>View Xbox GamerTags</a>";
}

?> 


 

Ah, sorry.. i read the post wrong slightly.. okay...

 

<?php
$conn = ($GLOBALS["___mysqli_ston"] = mysqli_connect("HOST", "USER-NAME", "PASSWORD!"));
$db = ((bool)mysqli_query($conn, "USE DATEBASE NAME"));
?>

<?php

$yourname = mysql_real_escape_string($_POST['user-name']);
$xboxgamertag = mysql_real_escape_string($_POST['xbox-gamertag']);
$game1 = mysql_real_escape_string($_POST['game1']);
$game2 = mysql_real_escape_string($_POST['game2']);
$game3 = mysql_real_escape_string($_POST['game3']);

$check = "SELECT xboxgamertag FROM yourtable WHERE xboxgamertag = $xboxgamertag";
$res = mysqli_query($GLOBALS["___mysqli_ston"], $check;
$count = mysqli_num_rows($res);
if ($count=>1){
echo 'Sorry, this gamertag already exists';
}
else {
$sql = "INSERT into xbox values('$yourname','$xboxgamertag','$game1','$game2','$game1')";
$qury = mysqli_query($GLOBALS["___mysqli_ston"], $sql);

if(!$qury) 
echo ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) :  (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) ;
else
{
echo "Success, $yourname!  Your now added to GamerTag Finder with the gamertag $xboxgamertag and want people to add you for $game1, $game2 and $game3.";
echo "<a href='xboxgamertags.php'>View Xbox GamerTags</a>";
}
}

?> 

Not tested i am afriad but you get the general idea. just do a query on the database for the new username being added and if the amount of rows is more than 1, do nothing.. if its not.. then add it in. :)

  • Author

Ah, sorry.. i read the post wrong slightly.. okay...

 

<?php
$conn = ($GLOBALS["___mysqli_ston"] = mysqli_connect("HOST", "USER-NAME", "PASSWORD!"));
$db = ((bool)mysqli_query($conn, "USE DATEBASE NAME"));
?>

<?php

$yourname = mysql_real_escape_string($_POST['user-name']);
$xboxgamertag = mysql_real_escape_string($_POST['xbox-gamertag']);
$game1 = mysql_real_escape_string($_POST['game1']);
$game2 = mysql_real_escape_string($_POST['game2']);
$game3 = mysql_real_escape_string($_POST['game3']);

$check = "SELECT xboxgamertag FROM yourtable WHERE xboxgamertag = $xboxgamertag";
$res = mysqli_query($GLOBALS["___mysqli_ston"], $check;
$count = mysqli_num_rows($res);
if ($count=>1){
echo 'Sorry, this gamertag already exists';
}
else {
$sql = "INSERT into xbox values('$yourname','$xboxgamertag','$game1','$game2','$game1')";
$qury = mysqli_query($GLOBALS["___mysqli_ston"], $sql);

if(!$qury) 
echo ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) :  (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) ;
else
{
echo "Success, $yourname!  Your now added to GamerTag Finder with the gamertag $xboxgamertag and want people to add you for $game1, $game2 and $game3.";
echo "<a href='xboxgamertags.php'>View Xbox GamerTags</a>";
}
}

?> 

Not tested i am afriad but you get the general idea. just do a query on the database for the new username being added and if the amount of rows is more than 1, do nothing.. if its not.. then add it in. :)

 

Thanks for your help, I have adapted the code but have an error:

 

$res = mysqli_query($GLOBALS["___mysqli_ston"], $check;
$count = mysqli_num_rows($res);
if ($count=>1){
echo 'Sorry, this gamertag already exists';
}
else {

The code above is all of the errors, in dreamweaver it flags up as line 1, 3 and 6 with the code above.

  • Author

I am still looking for help if anyone is able to help, I will post here if I fix it myself but been trying for a while now with no luck...

$res = mysqli_query($GLOBALS["___mysqli_ston"], $check);
$count = mysqli_num_rows($res);
if ($count>1){
echo 'Sorry, this gamertag already exists';
}

Missed a bracket on line 1. Unexpected double arrow on line 3. This now works on my system as the code is. (Not tested the whole function)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.