Jump to content

[SOLVED] checking the database for a username that already exists??????


cluce

Recommended Posts

I recieve these errors when checking the database for a username that already exists.

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\insert.php on line 20

 

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\insert.php:20) in C:\wamp\www\insert.php on line 33

 

Can someone tell me what I am doing wrong? This is all I have left to my registration page. I would appreciate it.

 

<?php

//checks for identical passwords
if($_POST["password"]!==$_POST["confirmpassword"]){
        header("Location: registration.html");
}  else {
$mysqli = mysqli_connect("localhost", "root", "", "test");
}
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}

$usercheck = $_POST['username'];
$check = ("SELECT username FROM auth_users WHERE username = '$usercheck'");
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
echo ('Sorry, the username '.$_POST['username'].' is already in use.');

}  else  {

$sql = "INSERT INTO auth_users (username, password, ConfirmPassword, f_name, L_name, address, city, state, zip, phoneNumber, AltphoneNumber, email, company) VALUES ('".$_POST['username']."',PASSWORD('".$_POST["password"]."'),PASSWORD('".$_POST["confirmpassword"]."'),'".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['address']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['alternate']."','".$_POST['email']."','".$_POST['company']."')";
$res = mysqli_query($mysqli, $sql);
}
if ($res === TRUE) {
	header("Location: registered.html");
	} else {
	printf("Could not insert record: %s\n", mysqli_error($mysqli));
}
mysqli_close($mysqli);

?>

Link to comment
Share on other sites

<?php
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM auth_users WHERE username = '$usercheck'"); // needed mysql_query here
$check2 = mysql_num_rows($check);
?>

 

You needed the mysql_query to get a valid resource.

 

Link to comment
Share on other sites

OK added the mySQL but now I get database.  I guess I am not connected to the database but I cant see why? Here are some new errors.

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\insert.php on line 19

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\insert.php on line 19

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\insert.php on line 20

 

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\insert.php:19) in C:\wamp\www\insert.php on line 32

Link to comment
Share on other sites

OK I simplyfied my code a bit but I still have the warnings........

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\insert.php on line 14

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\insert.php on line 14

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\insert.php on line 16

Sorry, the username jdoe is already in use.Could not insert record:

 

here is the new code

<?php

//checks for identical passwords
if($_POST["password"]!==$_POST["confirmpassword"]){
        header("Location: registration.html");
}  else {
$mysqli = mysqli_connect("localhost", "root", "", "test");
}
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// checks if the username exists
$result = mysql_query("SELECT username FROM auth_users WHERE username = '$username'");

if(mysql_num_rows($result) !== 0){
   echo ('Sorry, the username '.$_POST['username'].' is already in use.');

}  else  {

$sql = "INSERT INTO auth_users (username, password, ConfirmPassword, f_name, L_name, address, city, state, zip, phoneNumber, AltphoneNumber, email, company) VALUES ('".$_POST['username']."',PASSWORD('".$_POST["password"]."'),PASSWORD('".$_POST["confirmpassword"]."'),'".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['address']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['alternate']."','".$_POST['email']."','".$_POST['company']."')";
$res = mysqli_query($mysqli, $sql);
}
if ($res === TRUE) {
	header("Location: registered.html");
	} else {
	printf("Could not insert record: %s\n", mysqli_error($mysqli));
}
mysqli_close($mysqli);

?>

Link to comment
Share on other sites

OK I changed the mysql to mysqli to be consistent but I still recieve two warnings..  ALthough I see the proper message too

 

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\insert.php on line 14

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\insert.php on line 16

Sorry, the username jdoe is already in use.Could not insert record

 

<?php

//checks for identical passwords
if($_POST["password"]!==$_POST["confirmpassword"]){
        header("Location: registration.html");
}  else {
$mysqli = mysqli_connect("localhost", "root", "", "test");
}
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// checks if the username exists
$result = mysqli_query("SELECT username FROM auth_users WHERE username = '$username'");

if(mysqli_num_rows($result) !== 0){
    //$_SESSION['userExists'] 
echo ('Sorry, the username '.$_POST['username'].' is already in use.');
//header ("Location: registration.php");

}  else  {

$sql = "INSERT INTO auth_users (username, password, ConfirmPassword, f_name, L_name, address, city, state, zip, phoneNumber, AltphoneNumber, email, company) VALUES ('".$_POST['username']."',PASSWORD('".$_POST["password"]."'),PASSWORD('".$_POST["confirmpassword"]."'),'".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['address']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['alternate']."','".$_POST['email']."','".$_POST['company']."')";
$res = mysqli_query($mysqli, $sql);
}
if ($res === TRUE) {
	header("Location: registered.html");
	} else {
	printf("Could not insert record: %s\n", mysqli_error($mysqli));
}
mysqli_close($mysqli);

?>

Link to comment
Share on other sites

mysqli_query($mysqli, "SELECT username FROM auth_users WHERE username = '$username'");

 

You have to remember to follow the function guide lines.

 

www.php.net/mysqli_query

 

It requires a connection string first and statement after.

Link to comment
Share on other sites

mysql functions do not require the connection string

mysqli functions require the connection as the first parameter.

 

mysqli is the "new and improved" version of the mysql functions.

 

Either way, php is way better than ASP. =)

Link to comment
Share on other sites

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.