Web Analytics Made Easy -
StatCounter Trying to download table -edit- and upload - CodingForum

Announcement

Collapse
No announcement yet.

Trying to download table -edit- and upload

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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; Sep 12, 2011, 02:15 PM.

  • #2
    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");
    }
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment


    • #3
      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>
      Be yourself. No one else is as qualified.

      Comment


      • #4
        Thank you guys . Works perfect. I added the names, and I added the "hidden" variable. Thank you very much.

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎