Jump to content

code help!!!


dalerex

Recommended Posts

hi there, php noob here. i was wondering if someone could help me out of a jam here. i have a script that checks my form if my fields are properly filled or not. while it does indeed check the form, it does not return the errors back to the user so that he/she knows what was wrong. all the script does is it resets the form without telling the user what happened. any ideas? any and all help will be greatly appreciated. the code is below. please tell me what i did wrong. thanks.

 

<?php
session_start();

require_once('config.php');

$errmsg_arr = array();

$errflag = false;

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}

function clean($str) {
	$str = @trim($str);
	if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
	}
	return mysql_real_escape_string($str);
}

$fname = clean($_POST['fname']);
$lname = clean($_POST['lname']);
$login = clean($_POST['login']);
$password = clean($_POST['password']);
$cpassword = clean($_POST['cpassword']);
$city = clean($_POST['city']);
$country = clean($_POST['country']);
$month = clean($_POST['month']);
$day = clean($_POST['day']);
$year = clean($_POST['year']);
$sex = clean($_POST['sex']);
$designers = clean($_POST['designers']);
$brands = clean($_POST['brands']);
$mags = clean($_POST['mags']);
$store = clean($_POST['store']);
$have = clean($_POST['have']);
$icon = clean($_POST['icon']);
$style = clean($_POST['style']);
$pants = clean($_POST['pants']);
$shirts = clean($_POST['shirts']);
$dress = clean($_POST['dress']);
$shoe = clean($_POST['shoe']);

if($fname == '') {
	$errmsg_arr[] = 'First name is missing';
	$errflag = true;
}
if($lname == '') {
	$errmsg_arr[] = 'Last name is missing';
	$errflag = true;
}
if($city == '') {
	$errmsg_arr[] = 'City is missing';
	$errflag = true;
}
if($country == '') {
	$errmsg_arr[] = 'Country is missing';
	$errflag = true;
}
if($sex == '') {
	$errmsg_arr[] = 'Sex is missing';
	$errflag = true;
}
if($month == '') {
	$errmsg_arr[] = 'Month is missing';
	$errflag = true;
}
if($day == '') {
	$errmsg_arr[] = 'Day is missing';
	$errflag = true;
}
if($year == '') {
	$errmsg_arr[] = 'Year is missing';
	$errflag = true;
}
if($designers == '') {
	$errmsg_arr[] = 'Designers missing';
	$errflag = true;
}
if($brands == '') {
	$errmsg_arr[] = 'Brands missing';
	$errflag = true;
}
if($mags == '') {
	$errmsg_arr[] = 'Magazines missing';
	$errflag = true;
}
if($store == '') {
	$errmsg_arr[] = 'Favorite store missing';
	$errflag = true;
}
if($have == '') {
	$errmsg_arr[] = 'Must Have missing';
	$errflag = true;
}
if($icon == '') {
	$errmsg_arr[] = 'Icon missing';
	$errflag = true;
}
if($style == '') {
	$errmsg_arr[] = 'Your Style missing';
	$errflag = true;
}
if($pants == '') {
	$errmsg_arr[] = 'Pants size missing';
	$errflag = true;
}
if($shirts == '') {
	$errmsg_arr[] = 'Shirt size missing';
	$errflag = true;
}
if($shoe == '') {
	$errmsg_arr[] = 'Shoe size missing';
	$errflag = true;
}
if($login == '') {
	$errmsg_arr[] = 'Login ID missing';
	$errflag = true;
}
if($password == '') {
	$errmsg_arr[] = 'Password missing';
	$errflag = true;
}
if($cpassword == '') {
	$errmsg_arr[] = 'Confirm password missing';
	$errflag = true;
}
if( strcmp($password, $cpassword) != 0 ) {
	$errmsg_arr[] = 'Passwords do not match';
	$errflag = true;
}

if($login != '') {
	$qry = "SELECT * FROM members WHERE login='$login'";
	$result = mysql_query($qry);
	if($result) {
		if(mysql_num_rows($result) > 0) {
			$errmsg_arr[] = 'Login ID already in use';
			$errflag = true;
		}
		@mysql_free_result($result);
	}
	else {
		die("Query failed");
	}
}

if($errflag) {
	$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
	session_write_close();
	header("location: register-form.php");
	exit();
}

$qry = "INSERT INTO members(login, passwd, firstname, lastname, month, day, year, sex, city, country, designers, brands, mags, store, have, icon, 

style, pants, shirts, dress, shoe) 

VALUES('$login','".md5($_POST['password'])."','$fname','$lname','$month','$day','$year','$sex','$city','$country','$designers','$brands','$mags','$store','$h

ave','$icon','$style','$pants','$shirts','$dress','$shoe')";
$result = @mysql_query($qry);

if($result) {
	header("location: register-success.php");
	exit();
}else {
	die("Query failed");
}
?>

 

added code tags ~ CV

Link to comment
Share on other sites

We probably need to see the register-form.php page.

 

Just a note:  instead of setting $errflag = true on each check, at the very end of your checking you could just:

if(sizeof($errmsg_arr) > 0) $errflag = true;

since you are starting with an empty array and filling it...

Link to comment
Share on other sites

posted code isn't really that efficient, but it looks functional, so what does register-form.php look like?  do you have session_start() in there?  Are you echoing $_SESSION['ERRMSG_ARR'] somewhere in it?

 

p.s.- Please use code tags when posting code.

 

Just a note:  instead of setting $errflag = true on each check, at the very end of your checking you could just:

if(sizeof($errmsg_arr) > 0) $errflag = true;

since you are starting with an empty array and filling it...

 

Or he could just remove the $errflag = true; in all his conditions and do

 

if($errmsg_arr) {

  // make session var and header

}

 

since the array is only created if there's an error.

Link to comment
Share on other sites

You do not have to explicitly use serialize for passing arrays or objects through a session, as of like, v4.2.  This:

 

page1.php

<?php
   session_start();
   $fruit = array('a' => 'apple','b' => 'orange','c' => 'banana');
   $_SESSION['blah'] = $fruit;
   print_r($_SESSION['blah']);
?>

<a href = 'test2.php'>page2.php</a>

 

page2.php

<?php  
   session_start();
   print_r($_SESSION['blah']);
?>

 

will output the expected result of:

Array ( [a] => apple [b] => orange [c] => banana )

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.