Jump to content

Recommended Posts

Hiya,

Okay well this is confusing me. This is the code I have right now but for reasons you can probably see it's not doing the job correctly

[code=php:0]
if ( $submit == "true" ) {
$cms_root_path = '../';
include($cms_root_path . 'config.php');
$dbh=mysql_connect ($dblocation, $dbusername, $dbpassword)
or die ("Sorry - I got lost and couldn't find the database. Please try back later.");
$rs = @mysql_select_db("$dbdatabase", $dbh)
or die("Sorry - the database decided to give me trouble. Please try back after I've put it in it's place.");
$execute=mysql_query("select * from users where username=$username")
or die (mysql_error());
}
[/code]

Basically I want the PHP script to check and make sure the username they are trying to submit is not already in use before adding it to the DB.

Any help on where I should go from here would be much appreciated!!
[code]<?php
if ( $submit == "true" ) {
$cms_root_path = '../';
include($cms_root_path . 'config.php');
$dbh=mysql_connect ($dblocation, $dbusername, $dbpassword)
or die ("Sorry - I got lost and couldn't find the database. Please try back later.");
$rs = @mysql_select_db("$dbdatabase", $dbh)
or die("Sorry - the database decided to give me trouble. Please try back after I've put it in it's place.");
$execute=mysql_query("select * from users where username=$username")
or die (mysql_error());
}
if (mysql_num_rows($execute)>0) {
  echo "That username is already used";
} else {
  echo "That is a new username";
}
?>[/code]
$execute=mysql_query("select * from users where username=$username")
or die (mysql_error());

That terminates the script though because it doesn't find anything if the person is a new user

And when it is a duplicate user I get this


Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/een/public_html/admin/admin_registeruser.php on line 53
Sorry - I messed up and didn't execute the SQL query. Please try back later.
Alright now it just keeps continuing down the line until it reaches the main SQL where it detects there is a duplicate primary key instead of stopping at the die; point. See if you can make any sense of this:

[code=php:0]
//This checks to make sure the username isn't already in use

if ( $submit == "true" ) {
$cms_root_path = '../';
include($cms_root_path . 'config.php');
$conn = mysql_connect ($dblocation, $dbusername, $dbpassword)
or die ("Sorry - I got lost and couldn't find the database. Please try back later.");
$result = @mysql_select_db("$dbdatabase", $conn)
or die("Sorry - the database decided to give me trouble. Please try back after I've put it in it's place.");
$query=@mysql_query("select * from users where username=$username");
$tempvar = @mysql_num_rows($execute);
if ( $tempvar >0 ){
$usernamedused = "used";
}
}

if ( $usernameused == "used" ){
$errortxt = "Sorry, that username is already in use.";
$cms_root_path = '././';
include($cms_root_path . 'admin_registerusers_fail.php');
die;
}
[/code]
$tempvar = @mysql_num_rows($execute);
should be
$tempvar = @mysql_num_rows($query);

This is because your query variable is "query" not "execute"


And personally, I don't think you should use the @ sign, it really helps in debugging. If you are able to completely debug your code, you shouldn't have to worry about these errors showing up anywhere ;)
A more effecient way of doing it.

[code=php:0]//This checks to make sure the username isn't already in use

if ($submit == 'true') {
include('../config.php');
$conn = @mysql_connect($dblocation, $dbusername, $dbpassword)
or die ('Sorry - I got lost and couldn\'t find the database. Please try back later.');
$result = @mysql_select_db($dbdatabase, $conn)
or die('Sorry - the database decided to give me trouble. Please try back after I\'ve put it in it\'s place.');
$found = mysql_result(mysql_query('SELECT count(0) FROM `users` WHERE `username` = "'.$username.'" LIMIT 1'), 0, 0);

if ($found > 0){
$errortxt = 'Sorry, that username is already in use.';
include('././admin_registerusers_fail.php');
die;
}
}[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.