PDA

View Full Version : php $query help


Paul Ferrie
01-06-2003, 11:48 AM
Well guys i finally made into Scripting and Backend I never thought i would be posting in here for help lol

Anyways here is my problem

I have created a user login system (flash+php+mysql)
And it just about works

Here's the prob when i go to register as a new user the code wont properly check to see if the username already exsist's in the DB, I am pretty sure the problem lies in my first $query



PHP:--------------------------------------------------------------------------------
$query = "SELECT name FROM contacts WHERE name='$name'";
if ($name == $name) {
print "loginT=UserName in Use";
}else{
$query = "INSERT INTO contacts (name, pass, email, id) VALUES ('$name', '$pass', '$email', NULL);";
}
$result = mysql_query($query);
// Gets the number of rows affected by the query as a check.
$numR = mysql_affected_rows($Connect);
if ($numR == 0) {
// Sends output to flash
print "loginT=Error - Register Again";
}
else if ($numR == 1) {
// Sends output to flash
print "loginT=Success&checklog=1";
}

--------------------------------------------------------------------------------


If i take out
PHP:--------------------------------------------------------------------------------
$query = "SELECT name FROM contacts WHERE name='$name'";
if ($name == $name) {
print "loginT=UserName in Use";

--------------------------------------------------------------------------------

THe login system works fine but dupilcate entries can be submitted to the database.

Please help i would love to get this up and running

Cheers

jimburton
01-08-2003, 12:50 PM
try:

// presuming you already have a connection called $c
$q = "SELECT name FROM contacts WHERE name='".$name."'";
$r = mysql_query($q,$c);
if($row = mysql_fetch_array($r)) {
echo "loginT=Login already in use";
} else {
$q2 = "INSERT INTO contacts (name, pass, email, id) VALUES ('".$name."', '".$pass."', '".$email."', NULL);";
$r2 = mysql_query($q2,$c);
if($row = mysql_fetch_array($r2)) {
echo "loginT=Successful login";
} else {
echo "loginT=Error - could not create account";
}
}

Paul Ferrie
01-08-2003, 02:25 PM
cheers for replying m8 but this one was sorted on Monday
:)

jimburton
01-08-2003, 02:33 PM
well don't double-post then :mad: :rolleyes: :mad:

Paul Ferrie
01-08-2003, 06:58 PM
double post?

jimburton
01-08-2003, 07:05 PM
whoops, sorry mate I thought you meant you posted it twice and one got answered, cos the same thing has happened to me several times lately and it's annoying....sorry, glad you got it sorted,:)