SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot waji's Avatar
    Join Date
    Dec 2004
    Location
    United Kingdom
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    moving data from one table to another

    Hello to Everyone,

    well, suppose, i have two tables table1 and table2 with same fields;

    now i want that when user submits data; so it should be sent to table2 and then after approval, the data moves to the table1;

    please guide me how to achieve this through php..

    thanks

  2. #2
    SitePoint Addict
    Join Date
    Jul 2005
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chceck out this Post: It will atleast give you some idea on moving data from Table1 to Table2

    http://www.sitepoint.com/forums/showthread.php?t=307876

  3. #3
    Maniacally depressed robot poncho's Avatar
    Join Date
    Dec 2004
    Location
    Belfast, N.Ireland
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have the first table set up? What way are you inserting the data, ie. are you using a db library or just straight mysql functions? I can show you how to do it with the answers to these questions.

    Cheers;
    Poncho
    Perfecting the art of breaking stuff.
    Check 'em: CakePHP | TextMate

  4. #4
    SitePoint Addict
    Join Date
    Sep 2002
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just add an extra column in your database that is called "approve" or something and have it set to a 1 diget int and have 0 = not approved and 1 = approved? It will save a lot of processing at very little cost in space.

  5. #5
    SitePoint Zealot waji's Avatar
    Join Date
    Dec 2004
    Location
    United Kingdom
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you for the replies,

    u r rite paulsjv; its good idea; i have used that but its for learning purposes,

    table1 and table2 structure, say it have three fields like ID,joke,title;

    and in the same time when the data is moving from table1 to table2 so that table1 records delete automatically,

  6. #6
    Maniacally depressed robot poncho's Avatar
    Join Date
    Dec 2004
    Location
    Belfast, N.Ireland
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this Waji,

    PHP Code:
    function confirm ($id) {
        
    $sql "SELECT * FROM table1 WHERE ID='".$id."'";
        
    $result mysql_query($sql);
        if(!
    $result) {
            return 
    false;
        } else {
            
    $row mysql_fetch_assoc($result);
        }
        
        
    $sql "INSERT INTO table2 SET 
                joke='"
    .$row['joke']."',
                title='"
    .$row['title']."'";
        
    $result mysql_query($sql);
        if (!
    $result) {
            return 
    false;
        } else {
            
    $sql "DELETE FROM table1 WHERE ID='".$id."'";
            
    $result mysql_query($sql);
            if(
    $result) {
                return 
    true;
            }
        }
    }

    $confirm confirm(5);
    if ( 
    $confirm ) {
        echo 
    'Sweet, your joke is confirmed!';
    } else {
        echo 
    'Eh, we can\'t seem to confirm your joke...';

    Untested, but I think it should work.

    Cheers;
    Poncho
    Perfecting the art of breaking stuff.
    Check 'em: CakePHP | TextMate

  7. #7
    SitePoint Zealot waji's Avatar
    Join Date
    Dec 2004
    Location
    United Kingdom
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you poncho,

    but how it will be if i drag that information from table1 in text fields and do some editing or without and then INSERT INTO table2? and in meanwhile the data erase/deletes from table1?

  8. #8
    Maniacally depressed robot poncho's Avatar
    Join Date
    Dec 2004
    Location
    Belfast, N.Ireland
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tend to build functions or class methods for each action that needs to take place, ie. add, confirm, update, delete. That way, you would have more code, but each function or method would only have to deal with one action. I find it easier to deal with and understand the code this way.

    Say you have confirmed a joke and you now want to edit it. Build a form with a field for the title and joke text and set the content of them to the data already in the database, I'll explain that further below.

    PHP Code:
      function getJoke ($id) {
          
    $sql "SELECT * FROM jokes WHERE ID='".$id."'";
          
    $result mysql_query($sql);
          if(!
    $result) {
              return 
    false;
          } else {
              
    $row mysql_fetch_assoc($result);
          }
      }
      
      function 
    updateJoke ($id$data) {
          if(!empty(
    $data['joke']) && !empty($data['title'])) {
              
    $sql "UPDATE jokes SET 
                      joke='"
    .mysql_real_escape_string($data['joke'])."',
                      title='"
    .mysql_real_escape_string($data['title'])."' 
                      WHERE ID='"
    .$id."'";
              
    $result mysql_query($sql);
              if(!
    $result) {
                  return 
    false;
              } else {
                  return 
    true;
              }
          }
      } 
    If you add getJoke to a variable, that variable will be available as an array of data to populate your fields like so:
    Code:
      $jokeData = getJoke($_REQUEST['id']);
      <input type="hidden" name="id" value="<?php echo $_REQUEST['id']; ?>" />
      <input type="text" name="joke" value="<?php echo $jokeData['joke']; ?>" />
    Then when you submit the form, call updateJoke to update the info you just changed in the fields, like so:

    PHP Code:
      if ($_POST) {
          
    $update updateJoke($_REQUEST['id'], $_POST);
          if(
    $update) {
              echo 
    'Sweet, you have updated your joke!';
          } else {
              echo 
    'Oh dear, looks like your joke was too bad to be updated :-(';
          }
      } 
    This is all untested code, so I can't guarantee it'll work Good luck.

    Cheers;
    Poncho
    Perfecting the art of breaking stuff.
    Check 'em: CakePHP | TextMate


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
  •