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.
$dealership_name = ($_POST['dealership_name']);
$roll_up = $row_get_agents['user_name'];
$privilege = "1";
// 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]];
}
// Add the user.
$query = "INSERT INTO users (user_name, dealership_name, password, privilege, roll_up, registration_date) VALUES ('$user_name', '$dealership_name', SHA('$user_name'), '$privilege', '$roll_up', NOW() )";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
if (mysql_affected_rows() == 1) { // If it ran OK.
// Finish the page.
echo '<p>Dealer has been added.</p><br />';
?>
<h1>Add Dealer </h1>
<form action="add_dealer.php" method="post">
<table width="30%" border="0">
<tr>
<th width="23%" scope="col"><div align="left"><b>Dealership Name:</b></div></th>
<th width="77%" scope="col"><div align="left"><b>Agent Name:</b></div></th>
</tr>
<tr>
<td><input type="text" name="dealership_name" size="30" maxlength="40" value="<?php if (isset($_POST['dealership_name'])); ?>" /></td>
<td><select name="roll_up" size="1">
<option value="" <?php if (!(strcmp("", $row_get_agents['name']))) {echo "selected=\"selected\"";} ?>>Agent</option>
<?php
do {
?>
<option value="<?php echo $row_get_agents['user_name']?>"<?php if (!(strcmp($row_get_agents['user_name'], $row_get_agents['name']))) {echo "selected=\"selected\"";} ?>><?php echo $row_get_agents['name']?></option>
<?php
} while ($row_get_agents = mysql_fetch_assoc($get_agents));
$rows = mysql_num_rows($get_agents);
if($rows > 0) {
mysql_data_seek($get_agents, 0);
$row_get_agents = mysql_fetch_assoc($get_agents);
}
?>
</select>
</td>
</tr>
</table>
<p><input type="submit" name="submit" value="Add Dealer" />
</p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<br />
<?php
include ('./includes/footer.php'); // Include the HTML footer.
exit();
} else { // If one of the data tests failed.
echo '<p><font color="red" size="+1">Please try again.</font></p>';
}
mysql_close(); // Close the database connection.
}
} // End of the main Submit conditional.
?>
<h1>Add Dealer </h1>
<form action="add_dealer.php" method="post">
<table width="30%" border="0">
<tr>
<th width="23%" scope="col"><div align="left"><b>Dealership Name:</b></div></th>
<th width="77%" scope="col"><div align="left"><b>Agent Name:</b></div></th>
</tr>
<tr>
<td><input type="text" name="dealership_name" size="30" maxlength="40" value="<?php if (isset($_POST['dealership_name'])) echo $_POST['dealership_name']; ?>" /></td>
<td><select name="roll_up" size="1">
<option value="" <?php if (!(strcmp("", $row_get_agents['name']))) {echo "selected=\"selected\"";} ?>>Agent</option>
<?php
do {
?>
<option value="<?php echo $row_get_agents['user_name']?>"<?php if (!(strcmp($row_get_agents['user_name'], $row_get_agents['name']))) {echo "selected=\"selected\"";} ?>><?php echo $row_get_agents['name']?></option>
<?php
} while ($row_get_agents = mysql_fetch_assoc($get_agents));
$rows = mysql_num_rows($get_agents);
if($rows > 0) {
mysql_data_seek($get_agents, 0);
$row_get_agents = mysql_fetch_assoc($get_agents);
}
}
?>
</select></td>
</tr>
</table>
<p><input type="submit" name="submit" value="Add Dealer" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<br />
<?php // Include the HTML footer.
include ('./includes/footer.php');
mysql_free_result($get_agents);
?>
Let me know what is wrong.
Sonny