SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face table not showing in php code

    Hello everyone

    I have been at this code for a few hours and I am getting that I can't see the woods for the trees. The table is not showing up. Here is the code

    PHP Code:
    <html>
    <head>
    <title>Edit Users</title>
    </head>
    <body>
    <h1>Edit Users</h1>
    <?php
       
    if(!isset($_POST['submit']) && isset($_GET['User'])&& isset ($_GET['MyKey']) && isset($_GET['Password']))
       {
      echo 
    '<table align="center">';
       echo 
    '<form action="edit_user.php" method="POST">';
      echo 
    '<tr><td><input type="hidden" name="MyKey" value="'.$_GET['MyKey'].'"></td></tr>';
      echo 
    '<tr><td>User Login<input type="text" name="User" value="'.$_GET['User'].'" size="30"></td></tr>';
      echo 
    '<tr><td>Password<input type ="text" name="Password" value="'.$_GET['passwd'].'" siz="20"></td></tr>';
      echo 
    '<tr><td colspapan="2"><input type="submit" name="Save Changes"><br></td></tr>';
      echo 
    '</form>';
      echo 
    '</table>';
      
       }
      elseif (
    $_POST['submit'] == "Save Changes" && isset($_POST['MyKey']))
      {
        include(
    "connect.php");
         
    $query "UPDATE users SET MyKey='$MyKey', User='$User', Password='$passwd'";
          
    $result mysql_query ($query);
         if (
    mysql_errno() == 0){
             echo 
    'Saved Record';
         }
         else {
         echo 
    'Unable to Save Record';
         }
      }
    ?>
    <b><a href="display_user.php">display Users</a></b>
    </body>
    </html>
    I am also getting an error at this piece of code

    PHP Code:
    elseif ($_POST['submit'] == "Save Changes" && isset($_POST['MyKey'])) 
    error message is Undefined index: submit

    There is probably a simple answer for both of these but I can't seem to find it.

    thank you for looking

    Vonz

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    form bugs

    It looks like for the table to show you have to request the page like this
    http://www.domain.com/thepage.php?User=value&MyKey=value&Password=value
    Is that how you are doing it?
    The submit does not have the name "submit" and the value "Save Changes"

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so If I go name="save" value="Save Changes". that will get rid of my error?

    Sorry I am not very good with this at all.

    Many thanks
    Vonz

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    caught editing post

    Sorry, I was editing the post. The $_POST['submit'] var is looking for an input with the name "submit". If you name it "save" you would use $_POST['save']

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This all starts with the following code

    <html>
    <head></head>
    <body>
    <?php
    //Display's all users
    include ("connect.php");
    $query = "select * from users;";
    //echo "$query";
    $result = mysql_query($query)or die ("error");
    echo "<table border=1 align ='center'>";
    echo "<tr><td>Users</td><td>Action</td></tr>";
    echo '<form action = "display_user.php" mehtod = "GET">';
    echo '</form>';
    while ($row = mysql_fetch_array($result))
    {
    echo "<tr><td>".stripslashes($row['User'])."</td>";
    echo '<td><a href="edit_user.php?MyKey='.$row["UserKey"].'&User='.$row["User"].'">Edit </a>';
    echo '<a href="delete_user.php?MyKey='.$row["UserKey"].'&User='.$row["User"].'"> Delete</a></td></tr>';
    echo '<input type="hidden" name="submit" value="submit">';
    }
    echo "</table>";
    mysql_close();
    echo "<br><a href = 'manage_user.php'>Return to Manage User Page</a>";
    ?>
    </body>
    </html>

    When the user clicks on the edit link, what I am trying to do is have the info put into a table, allow the user to edit and save those changes then update the db table.

    Does this answer your question?

    Vonz

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    mark up

    The mark up is a bit of a mess. The form tags dont have any inputs in them, so they, and the hidden input are useless as it is now. The links will send the GET variables MyKey and User, but they don't have the "Password" variable in them. If you want to send a password, I would suggest using POST (put everything inside the form tags, and add "delete or edit" radio inputs) so the password won't be seen in the URL.

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm I sort of see what you mean I will have a play around with that.

    Thank you for your help

    Vonz

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face form bugs

    Hi

    As you can see i have been playing with this for most of the day. When I click on an edit link I should get a form showing up with the users login name and password, from here able to edit the info and save to the db table users. MyKey is the primary key. When I click on the edit link, all I get is the heading 'Edit Users' and the link to go and display the users table, which is at the bottom of my code.

    This is the current code that I am working with
    PHP Code:
    <html>
    <head>
    <title>Edit Users</title>
    </head>
    <body>
    <h3>Edit Users</h3>
    <?php
      $MyKey
    =$_POST['MyKey'];
      
    $submit=$_POST['submit'];
      
    $User=$_GET['MyKey'];
      
    $passwd=$_GET['Password'];
     include (
    "connect.php");
      if (!
    $submit)
      {
        
    $query "select * from users where MyKey = $MyKey";
        
    $result mysql_query($query);
        if (
    $result)
        {
          
    $row mysql_fetch_array($result);
          
    $MyKey=stripslashes($row['MyKey']);
          
    $User=stripslashes($row['User']);
          
    $passwd=stripslashes($row['Password']);
          echo 
    '<form action="edit_user.php" method="POST">';
          echo 
    '<table align="center">';
          echo 
    '<tr>';
          echo 
    '<td>Users Login<input type="hidden" name="MyKey" value="'.$MyKey.'"></td>';
          echo 
    '<td><input type="text" name="User" value="'.$User.'"></td>';
          echo 
    '</tr>';
          echo 
    '<tr>';
          echo 
    '<td>Password</td>';
          echo 
    '<td><input type="text" name="Password" value="'.$passwd.'"></td>';
          echo 
    '</tr>';
          echo 
    '</table>';
          echo 
    '</form>';
          
        }
      }
      else
      {
        
    $query "update users set MyKey='$MyKey', User='$User',Password='$passwd'";
        
    $result mysql_query($query);
        if (
    mysql_errno()==0)
        {
         echo 
    'Save User';
        }
        else
        {
          echo 
    'Unable to Save User';
        }
      }
    ?>
    <b><a href="display_user.php">display Users</a></b>
    </body>
    </html>
    I have left the user name and password as GET so I can see it going through and will change this later. Nothing between the php tags are showing in the browser.

    I have no idea what I am doing.

    Many thanks

    Vonz

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    form bugs

    Quote Originally Posted by vonz
    I have no idea what I am doing.
    Not to worry, you're doing fine. At least you indent your if sections so you can see the nesting more easily (I do mine a bit differently, but your style is fine too). Although you won't need tons of comments in your code once you get it straightened out, they can really help when you're developing it.
    Try this
    PHP Code:
    <!-- *** Beginning HTML stuff here *** -->
    <html>
    <head>
    <title>Edit Users</title>
    </head>
    <body>
    <h3>Edit Users</h3>
    <?php
    /* --- assign user supplied values to script variables ---
    ** these might be better if written in issets that assign
    ** default values if not set, atleast during development phase
    ** ie.(in "shorthand" if-else lines)
    **
    ** $MyKey = isset($_POST['MyKey'])?$_POST['MyKey']:<test POST MyKey value here>;
    ** $submit = isset($_POST['submit'])?$_POST['submit']:<test POST submit value here>;
    ** $User = isset($_GET['MyKey'])?$_GET['MyKey']:<test GET MyKey value here>;
    ** $passwd = isset($_GET['Password'])?$_GET['Password']:<test GET Password value here>;
    **
    ** assigning default test values will allow isolation of problems
    */
      
    $MyKey=$_POST['MyKey'];
      
    $submit=$_POST['submit'];
      
    $User=$_GET['MyKey'];
      
    $passwd=$_GET['Password'];
    include (
    "connect.php");
    /*
    ** to test this section of script un-comment the unset line
    ** to test when $submit is FALSE
    */
    //  unset($submit); // comment out to test when $submit is TRUE
      
    if (!$submit){
        
    $query "select * from users where MyKey = $MyKey";
        
    $result mysql_query($query);
        if (
    $result){
          
    $row mysql_fetch_array($result);
          
    $MyKey=stripslashes($row['MyKey']);
          
    $User=stripslashes($row['User']);
          
    $passwd=stripslashes($row['Password']);
    /*
    ** generate HTML form to be displayed
    */
          
    echo '<form action="edit_user.php" method="POST">';
          echo 
    '<table align="center">';
          echo 
    '<tr>';
          echo 
    '<td>Users Login<input type="hidden" name="MyKey" value="'.$MyKey.'"></td>';
          echo 
    '<td><input type="text" name="User" value="'.$User.'"></td>';
          echo 
    '</tr>';
          echo 
    '<tr>';
          echo 
    '<td>Password</td>';
          echo 
    '<td><input type="text" name="Password" value="'.$passwd.'"></td>';
          echo 
    '</tr>';
          echo 
    '</table>';
          echo 
    '</form>';
        }
      } else {
    /*
    ** this section of script will run when $submit is TRUE
    */
        
    $query "update users set MyKey='$MyKey', User='$User',Password='$passwd'";
        
    $result mysql_query($query);
        if (
    mysql_errno()==0){
         echo 
    'Save User';
        } else {
          echo 
    'Unable to Save User';
        }
      }
    ?>
    <!-- *** Ending HTML stuff here *** -->
    <b><a href="display_user.php">display Users</a></b>
    </body>
    </html>

  10. #10
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    debugging

    Thank you for your patience and your advice.

    So with this code
    PHP Code:
     $MyKey = isset($_POST['MyKey'])?$_POST['MyKey']:$MyKey='cat'
    I should see cat on the screen, would that be right?

    Vonz

  11. #11
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    ternary conditional operator

    Actually it would be written like this
    PHP Code:
     $MyKey = isset($_POST['MyKey'])?$_POST['MyKey']:'cat'
    $MyKey variable, if the $_POST['MyKey'] variable isset equals $_POST['MyKey'] variable, else equals 'cat'
    If the $_POST['MyKey'] variable is set then whatever it is should show in the table, otherwise 'cat' should show. (depending on whether or not there are other problems affecting the display of course) If you do this to all 4 of the variables, then you will see if the table displays properly as if the values were set OK, and then figure out why they're not, if they aren't.
    PHP Code:
      $MyKey = isset($_POST['MyKey'])?$_POST['MyKey']:'cat';
      
    $submit = isset($_POST['submit'])?$_POST['submit']:'submit';
      
    $User = isset($_GET['MyKey'])?$_GET['MyKey']:'cat';
      
    $passwd = isset($_GET['Password'])?$_GET['Password']:'test_password'
    but with the default values you want to test with

  12. #12
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    debugging

    Ok It sounds simple enough.

    I have un commented the line and added the variable cat

    $MyKey = isset($_POST['MyKey'])?$_POST['MyKey']:'cat';

    now when I run the program I should see either what has been put into $MyKey or cat? What I do see is 'Edit Users'
    which is the heading and a link that is at the bottom of the code. No Cat and no $MyKey variable. According to the url $MyKey is being picked up.

    So is this saying that there is a problem with the complete code?

    When I run the code on its own I am only getting the Edit Users heading and the link.

    Vonz

  13. #13
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    bug

    There may be a problem with the database. Make sure the $MyKey value is in one of the database's MyKey field and try this code
    PHP Code:
     <!-- *** Beginning HTML stuff here *** -->
    <html>
    <head>
    <title>Edit Users</title>
    </head>
    <body>
    <h3>Edit Users</h3>
    <?php
    /* --- assign user supplied values to script variables ---
    ** these might be better if written in issets that assign
    ** default values if not set, atleast during development phase
    ** ie.(in "shorthand" if-else lines)
    **
    ** $MyKey = isset($_POST['MyKey'])?$_POST['MyKey']:<test POST MyKey value here>;
    ** $submit = isset($_POST['submit'])?$_POST['submit']:<test POST submit value here>;
    ** $User = isset($_GET['MyKey'])?$_GET['MyKey']:<test GET MyKey value here>;
    ** $passwd = isset($_GET['Password'])?$_GET['Password']:<test GET Password value here>;
    **
    ** assigning default test values will allow isolation of problems
    */
    /* This is the Orignal script
      $MyKey=$_POST['MyKey'];
      $submit=$_POST['submit'];
      $User=$_GET['MyKey'];
      $passwd=$_GET['Password'];
    * the following is the replacement script
    */
      
    $MyKey = isset($_POST['MyKey'])?$_POST['MyKey']:'cat';
      
    $submit = isset($_POST['submit'])?$_POST['submit']:'submit';
      
    $User = isset($_GET['MyKey'])?$_GET['MyKey']:'cat';
      
    $passwd = isset($_GET['Password'])?$_GET['Password']:'test_password';
    /* end of replacement script */
    include ("connect.php");
    /*
    ** to test this section of script un-comment the unset line
    ** to test when $submit is FALSE
    */
      
    unset($submit); // comment out to test when $submit is TRUE
      
    if (!$submit){
        
    $query "select * from users where MyKey = $MyKey";
        
    $result mysql_query($query);
        if (
    $result){
          
    $row mysql_fetch_array($result);
          
    $MyKey=stripslashes($row['MyKey']);
          
    $User=stripslashes($row['User']);
          
    $passwd=stripslashes($row['Password']);
    /*
    ** generate HTML form to be displayed
    ** if $submit is NOT set AND there is a result
    */
          
    echo '<form action="edit_user.php" method="POST">';
          echo 
    '<table align="center">';
          echo 
    '<tr>';
          echo 
    '<td>Users Login<input type="hidden" name="MyKey" value="'.$MyKey.'"></td>';
          echo 
    '<td><input type="text" name="User" value="'.$User.'"></td>';
          echo 
    '</tr>';
          echo 
    '<tr>';
          echo 
    '<td>Password</td>';
          echo 
    '<td><input type="text" name="Password" value="'.$passwd.'"></td>';
          echo 
    '</tr>';
          echo 
    '</table>';
          echo 
    '</form>';
        }
    /* added script in case there was no $result */
          
    else {
            echo 
    'submit was not set AND there was NO result';
        }
    /* end if added script */
      
    } else {
    /*
    ** this section of script will run when $submit is TRUE
    */
        
    $query "update users set MyKey='$MyKey', User='$User',Password='$passwd'";
        
    $result mysql_query($query);
        if (
    mysql_errno()==0){
         echo 
    'Save User';
        } else {
          echo 
    'Unable to Save User';
        }
      }
    ?>
    <!-- *** Ending HTML stuff here *** -->
    <b><a href="display_user.php">display Users</a></b>
    </body>
    </html>

  14. #14
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    buggy code

    Hi Mittineague

    You are dead right about the database, somehow MyKey got changed to something else. Now changed back.

    It has also just dawned on me that in this particular form there is no submit button. So that upsets a good porportion of the code. I guess I have been at this too long. I have fiddled with the code and it is now looking like this.
    PHP Code:
    <html>
    <head>
    <title>Edit Users</title>
    </head>
    <body>
    <h3>Edit Users</h3>
    <?php
    //connect to the database
      
    include("connect.php");
     
    if (isset(
    $_GET['User']) && isset($_GET['MyKey'])&& isset($_GET['Password']))
      {
        echo 
    '$MyKey<br>';
        echo 
    '$User';
         
    $query "select * from users where MyKey = '$MyKey'";
         
    $result mysql_query($query);
         if (
    $result)
         {
            
    $row mysql_fetch_array($result);
            
    $MyKey=stripslashes($row['MyKey']);
            
    $User=stripslashes($row['User']);
            
    $passwd=stripslashes($row['Password']);
    ?>
          <form action="edit_user.php" method="POST">
          <table align="center">
          <tr>
          <td>Users Login<input type="hidden" name="MyKey" value="'.$MyKey.'"></td>
          <td><input type="text" name="User" value="'.$User.'"></td>
          </tr>
          <tr>
          <td>Password</td>
          <td><input type="text" name="Password" value="'.$passwd.'"></td>
          </tr>
          </table>
          </form>
    <?php
     
    }
     }
      else
      {
        
    $query "update users set User='$User', Password='$passwd'where (MyKey ='$MyKey')";
        
    $result mysql_query($query) or die ('Whoops did not work');
        if (
    mysql_errno()==0)
        {
         echo 
    'Saved<br>';
        }
        else
        {
          echo 
    'Unable to Save User';
        }
       }
    ?>
    <b><a href="display_user.php">Display Users</a></b>
    </body>
    </html>
    I don't think the first if statement is right. the echo 'Save' now shows up and the query has been refined. But I still can't get the form to show up.

    I am very thankful for all your help. Do you have skype on your computer? If you did and you were willing we could talk this through.

    Many thanks

    Vonz

  15. #15
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    form script

    No skype, sorry. Instead of ussig mysql_errno try replacing that line with
    PHP Code:
    if (mysql_affected_rows()==1

  16. #16
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mittineague

    Thank you for the suggestion. Made the change. Still no go. There is just something stopping the form from appearing.

    Thanks again

    vonz


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •