Web Analytics Made Easy -
StatCounter Using a Form to Filter which record to edit - CodingForum

Announcement

Collapse
No announcement yet.

Using a Form to Filter which record to edit

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using a Form to Filter which record to edit

    I am trying to create a form so that it has a pull down menu of all the users in my mysql database. Then I want the form to send the selected user and send it to an update form on another php page. I have created an update record page before but I don't know how to get the id from a form from another page. Hopefully I am being clear about my request. Thanks

    Darren

  • #2
    PHP Code:
    <?php
    // connect to db here

    $result mysql_query('SELECT user_id, username FROM users') or exit(mysql_error());
    if (
    mysql_num_rows($result))
    {
        echo 
    '
        <form action="update.php" method="POST">
        select user:<br>
        <select name="user_id">
        '
    ;
        
        while (
    $row mysql_fetch_assoc($result)) {echo '<option value="' $row['user_id'] . '">' $row['user_name'];}
        
        echo 
    '
        </select>
        <input type="submit" name="submit" value="submit">
        </form>
        '
    ;
    }
    ?>
    then in update.php you will have a variable called $_POST['user_id'] which you can use to pull that particular record and display your update form (which you mentioned that you already knowhow to do).

    Comment


    • #3
      Thanks for the code. I am going to try it out. I have one more question. I am not sure how to write the databse connection to my mysql databse as I normally get dreamweaver mx to do that for me. Sorry if this is a really dense question. Thanks.

      Darren

      Comment


      • #4
        I used this code but it did not work. The page just comes up blank.

        Code:
        <?php 
        include("conn.php")
        $strDB=mysql_connect($strServer,$struser,$strPwd);
        $database=mysql_select_db("$strDatabase",$strDB); 
        
        // Code for database manipulation
        mysql_close($strDB);
        
        $result = mysql_query('SELECT userid, username FROM users') or exit(mysql_error()); 
        if (mysql_num_rows($result)) 
        { 
            echo ' 
            <form action="delete.php" method="POST"> 
            select user:<br> 
            <select name="userid"> 
            '; 
             
            while ($row = mysql_fetch_assoc($result)) {echo '<option value="' . $row['userid'] . '">' . $row['username'];} 
             
            echo ' 
            </select> 
            <input type="submit" name="submit" value="submit"> 
            </form> 
            '; 
        } 
        ?>
        Not sure what I have done wrong.

        Darren

        Comment


        • #5
          you are closing the connection right before you run the query

          Comment


          • #6
            I can't get it to work. Probably because I am an idiot. However, this is what I did. I inserted the php code into my page.

            Code:
            <?php 
            include("conn.php")
            $strDB=mysql_connect($strServer,$struser,$strPwd);
            $database=mysql_select_db("$strDatabase",$strDB); 
            
            $result = mysql_query('SELECT userid, username FROM users') or exit(mysql_error()); 
            if (mysql_num_rows($result)) 
            { 
                echo ' 
                <form action="update.php" method="POST"> 
                select user:<br> 
                <select name="userid"> 
                '; 
                 
                while ($row = mysql_fetch_assoc($result)) {echo '<option value="' . $row['userid'] . '">' . $row['username'];} 
                 
                echo ' 
                </select> 
                <input type="submit" name="submit" value="submit"> 
                </form> 
                '; 
            } 
            ?>
            The connections php page looks like this

            Code:
            <?php
            $strServer="www.mysite.com"; // Server IP Address 'or' Name
            $strDatabase="mydatabase"; // Database Name
            $strUser="myusername"; // User ID
            $strPwd="mypassword"; // Password
            ?>
            Now the page simply displays a blank page. When the php code is removed from the origianl page, the rest of the page displays normally. Go figure.

            Now I would ask you about the update form too but I have to get the code working for the selecting form first. Thanks for the help.

            Darren
            Last edited by dforbes; Aug 4, 2005, 12:58 AM.

            Comment


            • #7
              OK, I got it to work but the update record form is not picking up the selection. I am using Dreamweaver MX to generate the update record form so maybe something is not correct. What should the update form code be for the update.php? While I am at it...how about delete.php? Thanks.

              Darren

              Comment


              • #8
                I figured it out for both updating the record and deleting the record. I am an idiot.

                Darren

                Comment

                Working...
                X