Page 1 of 1

Code Error: Variable not being picked up?

Posted: Mon Jul 14, 2003 1:12 pm
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]

Posted: Tue Jul 15, 2003 6:31 am
by twigletmac
Where does username come from?

Mac

Posted: Tue Jul 15, 2003 6:32 am
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)

Posted: Tue Jul 15, 2003 8:20 am
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.

Posted: Tue Jul 15, 2003 9:47 am
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

Posted: Tue Jul 15, 2003 1:58 pm
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']