0

hi can any one suggest why this code doesnt work

if(mysql_num_rows(mysql_query("SELECT * FROM users WHERE username = '".$username."'")) == 1)
     {
     	echo "<h1>Error</h1>";
        echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
     }
     else
     {
         
         
     
    $registerquery = mysql_query("INSERT INTO users (fname,lname,age,email,phone,address,username,password,
        amount,plan,benefit,prem1,prem2) VALUES
        ('".$fname."', '".$lname."', '".$age."', '".$email."', '".$phone."', '".$address."', '".$username."', '".$password."', '".
        $amount."', '".$plan."', '".$benefit."', '".$prem1."', '".$prem2."')");
    if($registerquery)
        {
        	echo "<h3>Success</h3>";
        	echo "<p>Your account was successfully created. Please <a href=\"index.html\">click here to login</a>.</p>";
        }
        else
        {
     		echo "<h3>Error</h3>";
        	echo "<p>Sorry, your registration failed. Please go back and try again.</p>";
        }
3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by fobos
0

look at your code here

'".$fname."', '".$lname."', '".$age."', '".$email."', '".$phone."', '".$address."', '".$username."', '".$password."', '".
        $amount."', '".$plan."', '".$benefit."', '".$prem1."', '".$prem2."')");

if you already declare this variable for example you use post method in your form

$fname = $_POST;
$lname = $_POST;
$age = $_POST;
then the rest of the variable

your values should like this

('$fname', '$lname', '$age', '$email' BLA BLA BLA)

if you don't want to declare it just add $_POST in values

('".$_POST."', '".$_POST."', ETC)

0

im familiar with that but the thing is the script always results in

echo "<h3>Error</h3>";
        	echo "<p>Sorry, your registration failed. Please go back and try again.</p>";

some how the querr fails.
anyidea why?

0

If this is a registration form, try checking it against the database using a different method.

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT username FROM users");

while($row = mysql_fetch_array($result)) {
    //if the username matches any username in the database, return it false. Else return insert code.
    if($row['username'] == $_POST['username']) {
        echo "Username Already In Use";
    } else {
        // Insert Code
}

mysql_close($con);
?> 

I havent used PHP in a while, but something like this should work.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.