August 30, 201312 yr 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>"; } ?>
August 31, 201312 yr Any chance you can post your mysql table structure? I suspect your primary key is an incorrect data type.
August 31, 201312 yr Author Any chance you can post your mysql table structure? I suspect your primary key is an incorrect data type. HelloTex0gen. Sure, no problem, here is the URL to the image: http://puu.sh/4ft7r.png
August 31, 201312 yr 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.
August 31, 201312 yr 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.
September 1, 201312 yr 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...
September 2, 201312 yr $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