Jump to content

[SOLVED] Bulk adding to database


AdRock

Recommended Posts

I have a form with a number of fields for a given number of users i need to enter information for.

 

I have created the form in a form loop so each input field is in an array but how do i assign that array of inputs to a variable?

 

The plan is that I ask the admin how many users to bulk insert, display the same input fields for each user and insert into the database.

 

I know that I will have to do a for each loop on the insert function but i'm not sure about assigning all values to an array of usernames etc and validating each of them

 

	if($_POST && array_key_exists("action", $_POST)) {

	// CARRY OUT RELAVANT ACTION
	switch($_POST['action']) {
		case "back":
			header('Location: /soundoff/admin/users.php');
			break;
		case "save":
			//variables for checking the user's name
			$first_name = check_input(trim(ucwords($_POST['first_name'])));
			$last_name = check_input(trim(ucwords($_POST['last_name'])));

			//variables for checking the user's email
			$regEmail = check_input(trim($_POST['email']));
			$confemail = check_input(trim($_POST['confemail']));

			//varaibles for checking the user login credentials
			$regUser = check_input(trim($_POST['user']));
			$regPass = check_input(trim($_POST['pass']));
			$confpass = check_input(trim($_POST['confpass']));

			// A array to store errors
			$errors = array();

			// Collection of validators
			$validators = array();

			$validators[]=new ValidateName($first_name,'Forename');
			$validators[]=new ValidateName($last_name,'Surname');
			$validators[]=new ValidateEmails(array($regEmail,$confemail));
			$validators[]=new ValidateUsername($regUser);
			$validators[]=new ValidatePasswords(array($regPass,$confpass));

			// Iterate over the validators, validating as we go
			foreach($validators as $validator) {
			if (!$validator->isValid()) {
			while ( $error = $validator->fetch() ) {
						 $errors[]=$error;
					}
				}
			}
			/**
			* If there are no errors on the form, call the function to register a new account using the varaibles from the form.
			* If the new user details are successfully saved in the database, give the user a confirmation message
			* otherwise display an error for the user.
			*/
			if(empty($errors)){
				$result = user_register($first_name, $last_name, $regEmail, $regUser, $regPass, '1');
				if ($result == 'Correct') {
					header('Location: /soundoff/admin/users.php');
				}
				else {
					$register_error = $result;
				}
			}
			break;
	}
}

    // Include the header html
    require_once("header.inc.php");

echo ( "<h1>Bulk User Registration</h1>");

// If there was an error requesting a new password, display the error message.  Could be email address not found
    if (isset($register_error)) { 
echo "There was an error: ".$register_error;
    }

    /**
    * If there are errors and the number of errors is greater than zero,
    * display a warning message to the user with a list of errors
    */
    if ( isset($errors) && count($errors) > 0 ) {
    	echo ( "<h3 class='errorhead'>There has been an error:</h3><h4 class='error'><b>You forgot to enter the following field(s)</b></h4>" );
    	echo ( "<ul id='validation'>\n" );
    	foreach ( $errors as $error ) {
    	    echo ( "<li>".$error."</li>\n" );
    	}
echo ( "</ul>\n" );
    }
?>
<form method="post" name="adminform" id="adminform" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<p>
	<input type="button" class="back" id="backbutton" title="go back" onclick="performAction('back');" />
	<input type="button" class="saveuser" id="savebutton" title="save user" onclick="performAction('save');" />
</p>
<?php for($i=1;$i<3;$i++) { ?>
<div class="formcontent">
	<fieldset>
	<legend>About User #<?php echo $i ?></legend>
		<p><label for="first_name">Forename:</label>
		<input class="user" type="text" title="Please enter the first name" name="first_name[]" size="30" value="<?php echo $first_name; ?>" /></p>

		<p><label for="last_name">Surname:</label>
		<input class="user" type="text" title="Please enter the last name" name="last_name[]" size="30" value="<?php echo $last_name; ?>" /></p>

		<p class="hint">Please enter a valid email address for user.</p>
		<p><label for="email">Email address:</label>
		<input class="user" type="text" title="Please enter the email address" name="email[]" size="30" value="<?php echo $regEmail; ?>" /></p>

		<p><label for="confemail">Confirm Email:</label>
		<input class="user" type="text" title="Please enter the email address" name="confemail[]" size="30" value="<?php echo $confemail; ?>" /></p>
	</fieldset>
	<fieldset>
		<legend>User #<?php echo $i ?> Login Details</legend>
		<p class="hint">Please enter a username for the user account. Note that username should be between 4 and 30 characters.</p>
		<p><label for="user">Username:</label>
		<input class="user" type="text" title="Please enter a username" name="user[]" size="30" value="<?php echo $regUser; ?>" /></p>

		<p class="hint">Please enter a password for the user account. Note that passwords are case-sensitive.</p>
		<p><label for="pass">Password:</label>
		<input class="user" type="password" title="Please enter a password" name="pass[]" size="30" /></p>

		<p><label for="confpass">Verify password:</label>
		<input class="user" type="password" title="Please re-enter password" name="confpass[]" size="30" /></p>
	</fieldset>
	<?php } ?>
	<input type="hidden" id="action" name="action"  value="" />
</div>
</form>

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.