Current location: Hot Scripts Forums » Programming Languages » PHP » Undefined index error. Need help.


Undefined index error. Need help.

Reply
  #1 (permalink)  
Old 03-15-06, 02:15 PM
sondogg's Avatar
sondogg sondogg is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Undefined index error. Need help.

Hey guys,

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:

user_id
user_name
first_name
last_name
dealership_name
password
privilege
roll_up
registration_date

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.
    
        
$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_arr2); 
        
        
// get 3 random keys for num array 
        
$rand_num_keys array_rand($num_arr3); 
        
        
// 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>'
            }    
        
        
            
// 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="first_name" size="15" maxlength="15" value="<?php if (isset($_POST['dealership_name'])); ?>" /></td>
                        <td><select name="select" 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_agents0);
      
$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="first_name" size="15" maxlength="15" value="<?php if (isset($_POST['dealership_name'])) echo $_POST['dealership_name']; ?>" /></td>
        <td><select name="select2" 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_agents0);
      
$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);
?>
Here is the error:

An error occurred in script '/Users/sscrogg/Sites/add_dealer.php' on line 20:
Undefined index: dealership_name

Thanks for your help in advance.

Sonny

Last edited by sondogg; 03-15-06 at 02:21 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-15-06, 02:56 PM
sondogg's Avatar
sondogg sondogg is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
I figured it out...nevermind.

Sonny
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-15-06, 07:10 PM
sondogg's Avatar
sondogg sondogg is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
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_arr2); 
        
        
// get 3 random keys for num array 
        
$rand_num_keys array_rand($num_arr3); 
        
        
// 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_arr2); 
            
            
// get 3 random keys for num array 
            
$rand_num_keys array_rand($num_arr3); 
            
            
// 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_agents0);
      
$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_agents0);
      
$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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-15-06, 08:14 PM
sondogg's Avatar
sondogg sondogg is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Ok...I fixed that too (with a little help from dead-poetic)

I changed:

$row_get_agents['user_name']

to

($_POST['roll_up']);

and it works correctly now.

Sonny
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems getting PHP-Nuke setup correctly TravisT PHP 2 12-17-05 08:54 PM
Undefined index: a - help andrewvideo PHP 5 05-17-05 04:33 PM
NOTICE: Undefined index jozin PHP 6 04-29-05 05:19 AM
Create an index with Asp.net-vb code kathryn ASP.NET 0 01-04-05 09:39 AM
Link to index (PR5) or the forum (PR6) iKwak HTML/XHTML/XML 0 08-26-04 04:35 AM


All times are GMT -5. The time now is 07:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.