0

I am modifying a web application that was built in PHP by another developer. Currently, if the user fills out a form and enters a username that is already being used, it displays the MySQL error Duplicate entry 'fred' for key 'username' on a new page. I want to make it so that an error message is displayed on the same page, so I'm trying to check if the username entered matches another username in the table, but I cannot get it to work. Any thoughts?

$username_entry = ($_POST['username']);
$username_match = mysql_query("SELECT * FROM business WHERE username='$username_entry'");

if (($_POST['username']) == $username_match ) {
    // do something
}
m-use
  • 363
  • 1
  • 4
  • 16
  • which part is not working? – Severino Lorilla Jr. Apr 13 '16 at 01:49
  • The best solution is the one that has already been implemented. If you get a duplicate key on entry result then you can display an appropriate error message. This ensures that you only ever make one round trip to the database. If you check if the user exists before making the insert this will result in 2 round trips which is more costly. The best solution I see for your problem is either: 1. Redirect from the error message page back to the calling page and show the appropriate error. 2. Use ajax to complete the request, and show the error on the calling form. – Chris Apr 13 '16 at 01:59
  • @Chris-MayhemSoftware I'm a PHP noob, so I'm not sure how I would go about displaying an error message in this manner. Could you elaborate? – m-use Apr 13 '16 at 02:02
  • 1
    Add something like this to the page that is inserting the user into mysql: mysql_query('INSERT INTO ...'); if (mysql_errno() == 1062) { header("Location: http://example.com/myCallingPage.php?ErrorDetail=DuplicateError"); die(); } Then in your calling page have a look at the request parameter ErrorDetail , if it contains the text "DuplicateError" then show the error message you want. – Chris Apr 13 '16 at 02:07
  • This worked, thanks! – m-use Apr 13 '16 at 02:23

1 Answers1

0

first of all you're not returning anything from your query to check against. I query the db to see if any rows return with that username. $cnt is the number of rows so if that is greater than zero you have a duplicate.

$username_entry = ($_POST['username']);
    $username_match = mysql_query("SELECT count(*) FROM business WHERE username='$username_entry'");
    $cnt = mysql_num_rows($username_match);

    if (($cnt > 0 ) {
        // do something
    }
Sam Orozco
  • 1,258
  • 1
  • 13
  • 27
  • Thanks for responding. I get the following error when I use your code... – m-use Apr 13 '16 at 01:57
  • Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/shop-local-weekly-root/arizona/anytown/_actions/addbssn.php on line 45 Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/shop-local-weekly-root/arizona/anytown/_actions/addbssn.php on line 46 – m-use Apr 13 '16 at 01:57
  • If you want to do this best to just use count(*) so you only ever get 1 row back. – Chris Apr 13 '16 at 02:12
  • @m-use try the new edit – Sam Orozco Apr 13 '16 at 02:23