Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts

    Trying to download table -edit- and upload

    I am fairly new here and learning. I'm making a menu for a sub shop. I want to allow the client to edit prices. 1)I know how to pull data date from my table and display it in a form (allowing the price fields to be "editable"). 2)I know how to create a form and submit the entered values to my table in the database (as variables).

    My problem is that I do not know how to connect steps 1 & 2. Is there a way to re-assign the form cells (that I am displaying), as variables so I can then re-upload them to the table?

    Am I going about this all wrong? Is there a more common way to build a simple interface for the client, or open-source code? I hope I explained myself and I would sincerely appreciate any help or direction.
    Code:
    <html> 
    <head> 
            <title>View mymenu</title> 
    </head> 
    <body> 
    
    <h2>View mymenu</h2>
    <?php
    
    //Displays all data 'mymenu' table 
    
    //Connect to database
            require ('dbstuff.php');
            $db = connectDB();
     
            $result = mysql_query("SELECT * FROM mymenu")  
                    or die(mysql_error());
    				                  
    // Display data from mysql table "mymenu"
    ?>		
      <form action="" form name="editform" method="post"> 	
    <?	              
            echo "<table border='1' cellpadding='10'>"; 
            echo "<tr> <th>ID</th><th>type</th> <th>half</th><th>full</th></tr>"; 
    // loop 
            $count= 0;
    		while($row = mysql_fetch_array( $result )) { 
            ++$count;	  
    // echo out the contents of each row into a table 
    	             echo "<tr>"; 
    				 echo '<td name="ID">' . $row['ID'] . '</td>';  
    				 echo '<td>' . $row['type'] . '</td>' ;
    				 echo '<td><input type="text" value= ' . $row['half'] . '></td>';
    				 echo '<td><input type="text" value= ' . $row['full'] . '></td>';
     				 echo "</tr>";   
            }  
     
            // close table> 
            echo "</table>"; 
    		?>
     <input type="submit" name="submit" value="Submit">
     </form>
    </body> 
    </html>
    Last edited by Buffmin; 09-12-2011 at 07:15 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    After these lines:
    Code:
    require ('dbstuff.php');
    $db = connectDB();
    ADD this:
    Code:
    if(isset($_POST['submit']))
    {
    
    $half = $_POST["half"];
    $full = $_POST["full"];
    $type = $_POST["type"];
    $result = mysql_query("UPDATE mymenu SET half = $half, full = $full WHERE type = $type");
    }

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Umm...Sunfighter, that's not going to come close to working.

    Buffmin: For starters, you don't have any *NAMES* in your form fields in your <form>!!!

    So when you submit the <form> *NOTHING AT ALL* will be sent back to PHP for it to process.

    And Sunfighter's solution doesn't work, even if you give them names, because his answer will only handle *ONE SINGLE ITEM*. Well, it would, except he based the UPDATE on $type, whereas $type isn't a <form> field in your page. It should be based on the ID.

    So... With the understanding that I am *NOT* a PHP programmer, so well could be a typo or more here...
    Code:
    <html> 
    <head> 
            <title>View mymenu</title> 
    </head> 
    <body> 
    
    <h2>View mymenu</h2>
    <?php
    
    //Displays all data 'mymenu' table 
    
    //Connect to database
    require ('dbstuff.php');
    $db = connectDB();
    
    // if the form is posted back to this page, then process it:
    if ( isset($_POST["submit"]) )
    {
        $itemcount = $_POST["itemcount"];
        for ( $i = 1; $i <= $itemcount; ++$i )
        {
            $id = $_POST["id" . $i];
            $half = $_POST["half" . $i];
            $full = $_POST["full" . $i];
            $sql = "UPDATE mymenu SET half = $half, full = $full WHERE ID = $id";
            $result = mysql_query( $sql ) or die(mysql_error());
        }
    }
    // in any case, display items for update:
    $sql = "SELECT id, half, full, type FROM mymenu ORDER BY id";
    $result = mysql_query( $sql ) or die(mysql_error());
    				                  
    // Display data from mysql table "mymenu"
    ?>		
    <form action="" name="editform" method="post"> 	
    <table border='1' cellpadding='10'>
    <tr><th>ID</th><th>type</th> <th>half</th><th>full</th></tr>
    <?php
    // loop 
    $count= 0;
    while($row = mysql_fetch_array( $result )) 
    { 
        ++$count;	  
        $id = $row["id"];
        echo "<tr>\n"; 
        echo '<td><input type="hidden" name="id'. $count . '" value=". $id . "/>' . $id . "</td>\n";  
        echo '<td>' . $row['type'] . "</td>\n";
        echo '<td><input name="$half' . $count . '" value="' . $row['half'] . '"/>' . "</td>\n";
        echo '<td><input name="$full' . $count . '" value="' . $row['full'] . '"/>' . "</td>\n";
        echo "</tr>\n";   
    }  
    echo "</table>\n";
    echo "<input type="hidden" name="itemcount" value="' . $count . '"/>\n";
    ?>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body> 
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Buffmin (09-15-2011)

  • #4
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Thank you guys . Works perfect. I added the names, and I added the "hidden" variable. Thank you very much.


  •  

    Posting Permissions

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