Page 1 of 2

Code adding to sql

Posted: Thu Apr 06, 2006 9:46 am
by reecec
I have this code to add new users to database on mysql

it seems to not be accessing it i keepgeting my registration failed

and when i login it says the username and password are not in database i have the correct login details i know this for sure as if i change the it says it cant connect.

its like the sql is not letting me add or read the table

Code: Select all

<?php
require_once('setup.php');
// Connect to the database
mysql_connect($server,$dbuser,$dbpass) or die ("Could not establish connection"); // make connection
mysql_select_db($dbname); // select database

// convert posted info to easy to use variables
$user = $_REQUEST['username'];//get username from form
$pass = $_REQUEST['password'];//get password from form
$email = $_REQUEST['email'];// get email from form
$biography = $_REQUEST['biography'];// get biography from form

// strip away any dangerous tags
$user=strip_tags($user);
$pass=strip_tags($pass);
$email=strip_tags($email);
$biography=strip_tags($biography);

// remove spaces from variables
$user=str_replace(" ","",$user);
$pass=str_replace(" ","",$pass);
$email=str_replace(" ","",$email);

// remove escaped spaces
$user=str_replace("%20","",$user);
$pass=str_replace("%20","",$pass);
$email=str_replace("%20","",$email);

// add slashes to stop hacking
$user=addslashes($user);
$pass=addslashes($pass);
$email=addslashes($email);
$biography=addslashes($biography);

// minimum lengths
$minuser_len = 4; //username minimum length
$minpass_len = 4; //password minimum length

if(strlen($user) < $minuser_len || strlen($pass) < $minpass_len)
{
die("User/password was not long enough!");
}

// hash users password for security (32 chars random - md5)
$pass=md5($pass);

// create the SQL query to be executed
$request = "INSERT INTO login ( `userid` , `username` , `password`) VALUES ('', '$user', '$pass');";

// execute the query
$result = mysql_query($request);

// check if succesful registration
if($result){
echo "Registration was succesful<br><br><a href=\"showusers.php\">Click here to view user.</a>";
} else {
echo "Registration failed";
}
?>
hope someone can help


thanks in advance

reece

http://www.rjcdesigns.co.uk

Posted: Thu Apr 06, 2006 10:02 am
by feyd
have you checked what mysql_error() has to say?

hi

Posted: Thu Apr 06, 2006 10:07 am
by reecec
what do you mean there was no error

or do you mean a log or someting

thanks for helping 8O

Posted: Thu Apr 06, 2006 10:09 am
by feyd
if you're seeing "registration failed" mysql_query() is failing in some fashion.

hi

Posted: Thu Apr 06, 2006 10:18 am
by reecec
no i get registration was unsuccefull which is in my code if it dint work


thanks

Posted: Thu Apr 06, 2006 10:21 am
by Nathaniel
Change

$result = mysql_query($request);

to

$result = mysql_query($request) or die(mysql_error());

- Nathaniel

Posted: Thu Apr 06, 2006 10:28 am
by reecec
hi thanks i did that

i get this a blank page with just this: No database selected

No mysql error or anything like that

thanks for helping

Posted: Thu Apr 06, 2006 10:31 am
by feyd
make sure $dbname is set and correct.

hi

Posted: Thu Apr 06, 2006 10:32 am
by reecec
i dont know where No database selected came from as its not in the code and its not a msql warning

Re: hi

Posted: Thu Apr 06, 2006 10:33 am
by JayBird
reecec wrote:...and its not a msql warning
yes it is

hi

Posted: Thu Apr 06, 2006 10:35 am
by reecec
require_once('setup.php');
// Connect to the database
mysql_connect($server,$dbuser,$dbpass) or die ("Could not establish connection"); // make connection
mysql_select_db($dbname); // select database



my setup.php has my connect details in

Posted: Thu Apr 06, 2006 10:36 am
by feyd
just humor us and do it.

Posted: Thu Apr 06, 2006 10:43 am
by reecec
do what

Posted: Thu Apr 06, 2006 10:45 am
by feyd
feyd wrote:make sure $dbname is set and correct.
:roll:

Posted: Thu Apr 06, 2006 10:48 am
by reecec
o rite sorry yea it is though thats what u dont understand

name of database: reeceth_12
Username: reeceth_new

thats what my server tells me

i took out the setup and put it in one and still can find it look at my code its the write database

Code: Select all

<?php
$server = "localhost";
$dbuser = "reeceth_new";
$dbpass = "xxxxx";
$dbname = "reeceth_12";
$dbtable = "login";

// Connect to the database
mysql_connect($server,$dbuser,$dbpass) or die ("Could not establish connection"); // make connection
mysql_select_db($dbname); // select database

// convert posted info to easy to use variables
$user = $_REQUEST['username'];//get username from form
$pass = $_REQUEST['password'];//get password from form
$email = $_REQUEST['email'];// get email from form
$biography = $_REQUEST['biography'];// get biography from form

// strip away any dangerous tags
$user=strip_tags($user);
$pass=strip_tags($pass);
$email=strip_tags($email);
$biography=strip_tags($biography);

// remove spaces from variables
$user=str_replace(" ","",$user);
$pass=str_replace(" ","",$pass);
$email=str_replace(" ","",$email);

// remove escaped spaces
$user=str_replace("%20","",$user);
$pass=str_replace("%20","",$pass);
$email=str_replace("%20","",$email);

// add slashes to stop hacking
$user=addslashes($user);
$pass=addslashes($pass);
$email=addslashes($email);
$biography=addslashes($biography);

// minimum lengths
$minuser_len = 4; //username minimum length
$minpass_len = 4; //password minimum length

if(strlen($user) < $minuser_len || strlen($pass) < $minpass_len)
{
die("User/password was not long enough!");
}

// hash users password for security (32 chars random - md5)
$pass=md5($pass);
 
// create the SQL query to be executed
$request = "INSERT INTO login ( `userid` , `username` , `password` , `email` , `priv`) VALUES ('', '$user', '$pass', '$email', '$priv');";

// execute the query
$result = mysql_query($request) or die(mysql_error()); 

// check if succesful registration
if($result){
echo ("Registration of $user was succesful<br><br><a href=\"showusers.php\">Click to Continue.</a>");
} else {
echo "Registration failed of $user<br><br><a href=\"showusers.php\">Click to Continue.</a>";
}
?>