Code Error: Variable not being picked up?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
r0ssta
Forum Newbie
Posts: 3
Joined: Mon Jul 14, 2003 1:12 pm
Location: Charlottesville, VA

Code Error: Variable not being picked up?

Post by r0ssta »

Here is my code. The comments explain what I'm trying to do - but it wont' accept the value username.. instead it's treating it like it wants to insert a new user. I'm passing username to the function everytime, I don't know why it's not recognizing it.. username is a single variable, for example: r0ssta. Any ideaS?

Code: Select all

<?php
require_once('db_fns.php');

function display_user_form($username)
// This form can be used for inserting or editing information.
// To insert, don't pass any parameters.  This will set $edit
// to false, and the form will go to insert_user.php.
// To update, pass an array containing a username.  The
// form will be displayed with the old data and point to edit_user.php.
// It will also add a "Delete user" button.
{

  // if passed an existing book, proceed in "edit mode"
  $edit = is_array($username);

  // most of the form is in plain HTML with some
  // optional PHP bits throughout
?>
  <form method=post
        action="<?php echo $edit?'edit_user.php':'insert_user.php';?>">
  <table border=0>
  <tr>
    <td>Username:</td>
    <td><input type=text name=username
         value="<?php echo $edit?$username['username']:''; ?>"></td>
  </tr>
  <tr>
    <td>Title:</td>
    <td><input type=text name=title
         value="<?php echo $edit?$username['title']:''; ?>"></td>
  </tr>
  <tr>
    <td>First Name:</td>
    <td><input type=text name=firstName
         value="<?php echo $edit?$username['firstName']:''; ?>"></td>
  </tr>
  <tr>
    <td>Last Name:</td>
    <td><input type=text name=lastName
         value="<?php echo $edit?$username['lastName']:''; ?>"></td>
   </tr>
   <tr>
    <td>Phone:</td>
    <td><input type=text name=phone
         value="<?php echo $edit?$username['phone']:''; ?>"></td>
   </tr>
   <tr>
    <td>E-mail:</td>
    <td><input type=text name=email
         value="<?php echo $edit?$username['email']:''; ?>"></td>
   </tr>
    <tr>
      <td <?php if (!$edit) echo 'colspan=2'; ?> align=center>
         <?php
            if ($edit)
             echo '<input type=hidden name=username
                    value="'.$username['username'].'">';
         ?>
        <input type=submit
               value="<?php echo $edit?'Update':'Add'; ?> Username">
        </form></td>
        <?php
           if ($edit)
           {
             echo '<td>';
             echo '<form method=post action="delete_user.php">';
             echo '<input type=hidden name=username
                    value="'.$username['username'].'">';
             echo '<input type=submit
                    value="Delete user.">';
             echo '</form></td>';
            }
          ?>
         </td>
      </tr>
  </table>
  </form>
<?php
}
?>
[Admin Edit: Added PHP code tags]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Where does username come from?

Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

P.S - could you please use the tags to surround your code as it makes it a lot easier to read (check out the thread in my signature)
r0ssta
Forum Newbie
Posts: 3
Joined: Mon Jul 14, 2003 1:12 pm
Location: Charlottesville, VA

Post by r0ssta »

Sorry I thought I did use the

Code: Select all

tags.. oh well sorry it didn't work.

Username is coming from another file, but $username is the variable of the username the user logins in with.  It's passed to the function, say $username = r0ssta.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

r0ssta wrote:Sorry I thought I did use the

Code: Select all

tags.. oh well sorry it didn't work.[/quote]
You can edit your post to add them (like I did) so you can do something about it.  It really will increase the number of people helping you because they can quickly look at your code and scan for mistakes.

Mac
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

do you mean if not everything is submitted it acts like it's never seen you before? that probably has something to do with the fact that you're trying to get stuff from a multidimensional array that is never set.

to get what is given to you, use $_REQUEST['variable']

however, $_REQUEST[''] gives the last thing to set it in the way your things are passed. gpce epgc or however it's set. to get what they entered in that form, since it uses post, reguardless of if anything else has the same variable name, use $_POST['variable']
Post Reply