I'm having trouble with my script. I keep getting an undefined index error but I don't know why. Here's a brief description of what I'm trying to accomplish with this script. I have a users table that stores the info for admins, agents, and dealers with the following columns:
This is a form that has an input for a dealership name and a pull down menu that dynamically loads the agents names into it. The agent drop down value (user_name) needs to be added to the roll_up column for the dealer. The dealership's username is automatically generated and the password should be set as the username as well. The privilege is set as the number 1.
Here is the script:
PHP Code:
<?php require_once('./Connections/dbc.php');
//Populate the pull down menu.
$query_get_agents = "SELECT user_name, CONCAT(last_name, ',', first_name) AS name FROM users WHERE privilege = '2' ORDER BY name ASC";
$get_agents = mysql_query($query_get_agents, $dbc) or die(mysql_error());
$row_get_agents = mysql_fetch_assoc($get_agents);
$totalRows_get_agents = mysql_num_rows($get_agents);
// Adds a Dealer to the db
// Include the configuration file for error management and such.
require_once ('./includes/config.inc.php');
// Set the page title and include the HTML header and nav.
$page_title = 'Add Dealer';
include ('./includes/header.php');
include ('./includes/nav.php');
if (isset($_POST['submitted'])) { // Handle the form.
// create arrays with char and num
$char_arr = range('a', 'g');
$num_arr = range('0', '9');
// randomize the arrays
shuffle($char_arr);
shuffle($num_arr);
// get 2 random keys for char array
$rand_char_keys = array_rand($char_arr, 2);
// get 3 random keys for num array
$rand_num_keys = array_rand($num_arr, 3);
// combine the 5 random characters into a string
$user_name = $char_arr[$rand_char_keys[0]].$char_arr[$rand_char_keys[1]].$num_arr[$rand_num_keys[0]].$num_arr[$rand_num_keys[1]].$num_arr[$rand_num_keys[2]];
$password = $user_name;
if ($dealership_name && $roll_up) { // If everything's OK.
// Make sure the user_name is available.
$query = "SELECT user_id FROM users WHERE user_name = '$user_name'";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
if (mysql_num_rows($result) == 0) { // Available.
} else { // If it did not run OK.
echo '<p><font color="red" size="+1">This dealer is already in the database.</font></p>';
}
Ok...I figured my original problem out, but now I have another problem. Whenever I add a dealer and selected the agent and submit...it submits correctly but it always adds the agent at the top of the list instead of the agent I select from the list.
Why is this?
Here is an example:
Dealership Name: Toyota's For Less
The pull down menu displays my agents (20 of them). Lets say I select the 4th agent. When I submit and then check the database it added everything correctly except for the agent. It is always the first agent in the pull down menu.
Here is my updated script:
PHP Code:
<?php require_once('./Connections/dbc.php');
//Populate the pull down menu.
$query_get_agents = "SELECT user_name, CONCAT(last_name, ', ', first_name) AS name FROM users WHERE privilege = '2' ORDER BY name ASC";
$get_agents = mysql_query($query_get_agents, $dbc) or die(mysql_error());
$row_get_agents = mysql_fetch_assoc($get_agents);
$totalRows_get_agents = mysql_num_rows($get_agents);
// Adds a Dealer to the db
// Include the configuration file for error management and such.
require_once ('./includes/config.inc.php');
// Set the page title and include the HTML header and nav.
$page_title = 'Add Dealer';
include ('./includes/header.php');
include ('./includes/nav.php');
//session_start();
if (!isset($_SESSION['privilege'])) {
echo '<h1><font color="#FF0000">You must be logged in to view this page.</font></h1><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />';
include ('./includes/footer.php');
exit();
}
if ($_SESSION['privilege'] != "3" ) {
echo '<h1><font color="#FF0000">You do not have the appropriate permissions to view this page.</font></h1><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />';
include('./includes/footer.php');
exit();
}else{
if (isset($_POST['submitted'])) { // Handle the form.
// create arrays with char and num
$char_arr = range('a', 'g');
$num_arr = range('0', '9');
// randomize the arrays
shuffle($char_arr);
shuffle($num_arr);
// get 2 random keys for char array
$rand_char_keys = array_rand($char_arr, 2);
// get 3 random keys for num array
$rand_num_keys = array_rand($num_arr, 3);
// combine the 5 random characters into a string
$user_name = $char_arr[$rand_char_keys[0]].$char_arr[$rand_char_keys[1]].$num_arr[$rand_num_keys[0]].$num_arr[$rand_num_keys[1]].$num_arr[$rand_num_keys[2]];
$password = $user_name;
if ($dealership_name && $roll_up) { // If everything's OK.
// Make sure the user_name is available.
$query = "SELECT user_id FROM users WHERE user_name = '$user_name'";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
if (mysql_num_rows($result) == 0) { // Available.
} else { // create arrays with char and num
$char_arr = range('a', 'g');
$num_arr = range('0', '9');
// randomize the arrays
shuffle($char_arr);
shuffle($num_arr);
// get 2 random keys for char array
$rand_char_keys = array_rand($char_arr, 2);
// get 3 random keys for num array
$rand_num_keys = array_rand($num_arr, 3);
// combine the 5 random characters into a string
$user_name = $char_arr[$rand_char_keys[0]].$char_arr[$rand_char_keys[1]].$num_arr[$rand_num_keys[0]].$num_arr[$rand_num_keys[1]].$num_arr[$rand_num_keys[2]];
}