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>";
        }

Recommended Answers

All 3 Replies

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)

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?

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.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.