Jump to content

sticky forms


mindapolis

Recommended Posts

hi, I have a form in which some information is required but the username is optional however if it is left blank it says that username is taken please choose another one. how can I make it accept it if a username isn't entered? 

<?php

if (!isset($_SESSION)){
	session_start();
}

$fname="";
$lname="";
$username="";
$password="";
$reenterPassword="";
$address="";
$state="";
$city="";
$zip="";
$phone="";
$fax="";
$email ="";
$re_enter_email ="";
$payment = ""; 
$paypal_username="";
$ship_first_name="";
$ship_last_name="";
$ship_address="";
$ship_city="";
$ship_state="";
$ship_zip="";
$ship_email="";
$ship_re_email="";
$pet_name="";
$age=1;
$breed="";
    $pet_name2="";
$age2=1;
$breed2="";

// Logout

if(isset($_POST['logout'])){
	unset($_SESSION['user']);			
}

// Loading captcha file

require_once('recaptchalib.php');
$privatekey = "6LdxwsoSAAAAAO4CWYDjkrGQsf5GXuIopJcC6SkE";
$publickey = "6LdxwsoSAAAAAFrZTLjBoDvLLjk9_NR_ubncL-24";

require_once("functions.php");

DatabaseConnection();  

$state = $_POST['state'];
$payment = $_POST['payment_Method']; 
$breed = $_POST['breed'];
$ship_state=$_POST['Shipstate']; 


if((isset($_REQUEST["submit"]) && $_REQUEST["submit"]=="Log in") || $_SESSION['user']){

if(!$_SESSION['user']){
	$login_username = isset($_REQUEST["username"])?$_REQUEST["username"]:"";
	$login_password = isset($_REQUEST["password"])?$_REQUEST["password"]:"";

	$msg = array();
	if ($login_username == "")
		$login_msg['username_empty'] = "Please enter your name";
	if ($login_password == "")
		$login_msg['password_empty'] = "Please enter your password";			

	$query = "SELECT * FROM customers WHERE username = '$login_username' AND password = '".$login_password."'";
}else{
	$customer_id = $_SESSION['user'];
	$query = "SELECT * FROM customers WHERE customer_id='$customer_id'";
}

if (!$login_msg){

	$login_process = true;

	$result = mysql_query($query) or die("Database error: " . mysql_error());

	if (mysql_num_rows($result) > 0){
		$row = mysql_fetch_object($result);
		$customer_id = $row->customer_id;
		$_SESSION['user'] = $customer_id; 
		$fname = $row->fname;
		$lname = $row->lname;
		$username=$row->username;
		$address=$row->address;
		$state=$row->state;
		$city=$row->city;
		$zip=$row->zip;
		$phone=$row->phone;
		$fax=$row->fax;
		$email =$row->email;
		$payment = $row->payment_method;
		$paypal_username=$row->paypal_username;
		$ship_first_name=$row->ship_first_name;
		$ship_last_name=$row->ship_last_name;
		$ship_address=$row->ship_address;
		$ship_city=$row->ship_city;
		$ship_state=$row->ship_state;
		$ship_zip=$row->ship_zip;
		$ship_email=$row->ship_email;
		$pet_name=$row->pet_name;
		$age=$row->age;
		$age_month=$row->age_month;
		$breed=$row->breed;
            $pet_name2=$row->pet_name2;
		$age2=$row->age2;
		$age_month2=$row->age_month2;
		$breed2=$row->breed2;

	}else{
		$login_msg['login_fail'] = "Username, password combination is not valid.";
	}
}
}
if(isset($_REQUEST["submit"]) && $_REQUEST["submit"]=="submit"){
$msg=array();
$fname=isset($_REQUEST["fname"])?$_REQUEST["fname"]:"";
$lname=isset($_REQUEST["lname"])?$_REQUEST["lname"]:"";
$username=isset($_REQUEST["lname"])?$_REQUEST["username"]:""; 
$password=isset($_REQUEST["lname"])?$_REQUEST["password"]:"";
$reenterPassword=isset($_REQUEST["lname"])?$_REQUEST["reenterPassword"]:"";
$address=isset($_REQUEST["address"])?$_REQUEST["address"]:"";
$state=isset($_REQUEST["state"])?$_REQUEST["state"]:"";
$city=isset($_REQUEST["city"])?$_REQUEST["city"]:"";
$zip=isset($_REQUEST["zipcode"])?$_REQUEST["zipcode"]:"";
$phone=isset($_REQUEST["phone"])?$_REQUEST["phone"]:"";
$fax=isset($_REQUEST["fax"])?$_REQUEST["fax"]:"";
$email =isset($_REQUEST["email"])?$_REQUEST["email"]:"";
$re_enter_email =isset($_REQUEST["re_email"])?$_REQUEST["re_email"]:"";
$re_enter_email= $email; 
$payment = isset($_POST['payment_Method'])?$_POST['payment_Method']:""; 
$paypal_username=isset($_REQUEST["PayPal_username"])?$_REQUEST["PayPal_username"]:"";
$ship_first_name=isset($_REQUEST["Shipfname"])?$_REQUEST["Shipfname"]:"";
$ship_last_name=isset($_REQUEST["Shiplname"])?$_REQUEST["Shiplname"]:"";
$ship_address=isset($_REQUEST["Shipaddress"])?$_REQUEST["Shipaddress"]:"";
$ship_city=isset($_REQUEST["Shipcity"])?$_REQUEST["Shipcity"]:"";
$ship_state=isset($_REQUEST["Shipstate"])?$_REQUEST["Shipstate"]:"";
$ship_zip=isset($_REQUEST["Shipzipcode"])?$_REQUEST["Shipzipcode"]:"";
$ship_email=isset($_REQUEST["Shipemail"])?$_REQUEST["Shipemail"]:"";
$ship_re_email=$ship_email;

    $pet_name=isset($_REQUEST["petName"])?$_REQUEST["petName"]:"";
$age=isset($_REQUEST["age"])?$_REQUEST["age"]:'';
$age=isset($_REQUEST["age_month"])?$_REQUEST["age_month"]:1;
$breed=isset($_REQUEST["breed"])?$_REQUEST["breed"]:"";
$nutritional_needs=isset($_REQUEST["nutritionalNeeds"])?$_REQUEST["nutritionalNeeds"]:"";
    $special_instructions=isset($_REQUEST["specialInstructions"])?$_REQUEST["specialInstructions"]:"";

    $pet_name2=isset($_REQUEST["petName2"])?$_REQUEST["petName2"]:"";
$age2=isset($_REQUEST["age2"])?$_REQUEST["age2"]:"";
$age2=isset($_REQUEST["age_month2"])?$_REQUEST["age_month2"]:1;
$breed2=isset($_REQUEST["breed2"])?$_REQUEST["breed2"]:"";
$nutritional_needs2=isset($_REQUEST["nutritionalNeeds2"])?$_REQUEST["nutritionalNeeds2"]:"";
$special_instructions2=isset($_REQUEST["specialInstructions2"])?$_REQUEST["specialInstructions2"]:"";

// Products information

$product_name=isset($_REQUEST["productname"])?$_REQUEST["productname"]:"";
$quantity=isset($_REQUEST["quantity"])?$_REQUEST["quantity"]:0;
$price=isset($_REQUEST["price"])?$_REQUEST["price"]:0;
$quantity_diff=isset($_REQUEST["quantity_diff"])?$_REQUEST["quantity_diff"]:0;
$price_diff=isset($_REQUEST["price_diff"])?$_REQUEST["price_diff"]:0;
$sales_tax=isset($_REQUEST["salestax"])?$_REQUEST["salestax"]:0; 
$subtotal=isset($_REQUEST["subtotal"])?$_REQUEST["subtotal"]:0;
$shipping_cost=isset($_REQUEST["shippingcost"])?$_REQUEST["shippingcost"]:0;
$total=isset($_REQUEST["total"])?$_REQUEST["total"]:0;


// check for login

if(isset($_POST['customer_id'])){
	$customer_id = $_POST['customer_id'];
	$query = "SELECT * FROM customers WHERE customer_id='$customer_id'";
	$result = mysql_query($query) or die("Database error: " . mysql_error());
	if (mysql_num_rows($result) > 0){
		$login_process = true;
		$_SESSION['user'] = $customer_id;
		$row = mysql_fetch_object($result);
		$fname = $row->fname;
		$lname = $row->lname;
		$username=$row->username;
	}
}

// Is username taken ?

if(!$login_process){

	$query = "SELECT * FROM customers WHERE username = '$username'";
	$result = mysql_query($query) or die("Database error: " . mysql_error());
	if (mysql_num_rows($result) > 0){
		array_push($msg,"This username is already taken, please choose another one.");
	}
}
if($fname==""){
	array_push($msg,"Please Enter First Name.");
}
if($lname==""){
	array_push($msg,"Please Enter Last Name.");
}
/*	if($username==""){
  		array_push($msg,"Please Enter User Name.");
}
if(!$login_process){
	if($password==""){
		array_push($msg,"Please Enter Password.");
	}
	if($reenterPassword==""){
		array_push($msg,"Please Enter Re-Enter Password.");
	}
	if($reenterPassword!=$password){
		array_push($msg,"Both passwords does not match.");
	}
}*/
if($address==""){
	array_push($msg,"Please Enter Address.");

}
if($city==""){
		array_push($msg,"Please Enter City.");
}
if($state=="Please choose a state"){
	array_push($msg,"Please Select State.");
}
if($zip==""){
	array_push($msg,"Please Enter Zip.");
} else {
	if(!is_numeric($zip)){
		array_push($msg,"Please Enter Digits In Zip.");		
	}
}
if($phone==""){
	array_push($msg,"Please Enter Phone.");
	} else {
if(!is_numeric($phone)){
	array_push($msg,"Please Enter Digits In Phone.");		
	}
}
if($email==""){
	array_push($msg,"Please Enter Email.");
}
if($payment==""){
	//array_push($msg,"Please Select Payment Method.");
}
if($paypal_username==""){
//		array_push($msg,"Please Enter Paypal User Name.");
}
if($ship_first_name==""){
	array_push($msg,"Please Enter Shipping First Name.");
}
if($ship_last_name==""){
	array_push($msg,"Please Enter Shipping Last Name.");
}
if($ship_address==""){
	array_push($msg,"Please Enter Shipping Address.");
}
if($ship_city==""){
	array_push($msg,"Please Enter Shipping City.");
}
if($ship_state==""){
	array_push($msg,"Please Select Shipping State.");
}
if($ship_zip==""){
	array_push($msg,"Please Enter Shipping Zip.");
} else {
	if(!is_numeric($ship_zip)){
		array_push($msg,"Please Enter Digits In Shipping Zip.");	
	}
}
if($ship_email==""){
//		array_push($msg,"Please Enter Shipping Email.");
}

// Check Captcha

$resp = recaptcha_check_answer ($privatekey,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
	array_push($msg,"Wrong Captcha, please try again.");
}

// Insert customer

if($login_process){

	$sql0 = "UPDATE customers SET address='$address',city='$city', state='$state', zip='$zip', phone='$phone', fax='$fax', email='$email', re_enter_email='$re_enter_email',  ship_first_name='$ship_first_name', ship_last_name='$ship_last_name',  ship_address='$ship_address', ship_city='$ship_city', ship_state='$ship_state', ship_zip='$ship_zip', ship_email='$ship_email', ship_re_enter_email='$ship_re_email', pet_name='$pet_name', age='$age', breed='$breed', pet_name2='$pet_name2', age2='$age2', breed2='$breed2', age_month='$age_month', age_month2='$age_month2' WHERE customer_id='$customer_id'";

	if(count($msg) == 0){
		$r=	mysql_query($sql0) or die("Query Error : " . mysql_error());
		$customer = $customer_id;
	}
}else{

	$sql0="INSERT INTO customers (fname, lname,username,password, address, city, state, zip, phone, fax, email, re_enter_email,  ship_first_name, ship_last_name, ship_address, ship_city, ship_state, ship_zip, ship_email, ship_re_enter_email , pet_name, age, breed, pet_name2, age2, breed2, age_month, age_month2) VALUES ('$fname', '$lname','$username','$password', '$address', '$city', '$state', '$zip', '$phone', '$fax', '$email', '$re_enter_email', '$ship_first_name', '$ship_last_name', '$ship_address', '$ship_city', '$ship_state', '$ship_zip', '$ship_email', '$ship_re_email', '$pet_name', '$age', '$breed', '$pet_name2', '$age2', '$breed2', '$age_month', '$age_month2')";

	if(count($msg) == 0){
		$r=	mysql_query($sql0) or die("Query Error : " . mysql_error());
		$customer = mysql_insert_id();
	}
}
// Insert Order

$sql1="INSERT INTO orders (product_name, customer, quantity, price, sales_tax, subtotal, shipping_cost, total, nutritional_needs, special_instructions, nutritional_needs2, special_instructions2)

VALUES ('$product_name', '$customer', '$quantity_diff', '$price_diff', '$sales_tax', '$subtotal', '$shipping_cost', '$total', '$nutritional_needs', '$special_instructions', '$nutritional_needs2', '$special_instructions2')";

if(count($msg) == 0){
	$m= mysql_query($sql1) or die("Query Error : " . mysql_error());
}


}

?>

Link to comment
Share on other sites

I don't know if it makes sense to allow empty usernames and I don't know what additional influence this will have on your project, but you can simply add this condition to the relevant if-clause:

 

	// Is username taken ?

if(!$login_process){

	$query = "SELECT * FROM customers WHERE username = '$username'";
	$result = mysql_query($query) or die("Database error: " . mysql_error());
	if (mysql_num_rows($result) > 0 && $username != ""){                              //// <---- add condition here!
		array_push($msg,"This username is already taken, please choose another one.");
	}
}

 

Link to comment
Share on other sites

the problem does not occur during the login process.  This is a order form to order dog treats.  there is an option to create an username and password but if someone doesn't want to create a username, they can still complete the order.  I have attached the entire code for reference. 

I don't know if it makes sense to allow empty usernames and I don't know what additional influence this will have on your project, but you can simply add this condition to the relevant if-clause:

 

	// Is username taken ?

if(!$login_process){

	$query = "SELECT * FROM customers WHERE username = '$username'";
	$result = mysql_query($query) or die("Database error: " . mysql_error());
	if (mysql_num_rows($result) > 0 && $username != ""){                              //// <---- add condition here!
		array_push($msg,"This username is already taken, please choose another one.");
	}
}

18404_.php

Link to comment
Share on other sites

Even in the entire code this is the only part where the given error message occurs.

 

Did you try to add the condition concerning the $username?

 

if (mysql_num_rows($result) > 0 && $username != ""){

 

(sorry, the coloring doen't work inside the code-Tags)

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.