How to expty a MySWL table in PHP

Discussion in 'PHP' started by CuBz, Jul 16, 2009.

  1. #1
    Sorry i spelt the title wrong lol

    I have a MySQL table shown below:

    CREATE TABLE IF NOT EXISTS `garage` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `owner` varchar(40) NOT NULL DEFAULT '',
      `car` varchar(100) NOT NULL DEFAULT '',
      `damage` varchar(100) NOT NULL DEFAULT '',
      `origion` varchar(100) NOT NULL DEFAULT '',
      `location` varchar(100) NOT NULL DEFAULT '',
      `upgrades` varchar(100) NOT NULL DEFAULT '0-0-0-0-0-0-0-0',
      `status` enum('0','1','2','3','4') NOT NULL DEFAULT '0',
      `worth` int(32) NOT NULL DEFAULT '0',
      `shiptime` varchar(100) NOT NULL DEFAULT '',
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=134 ;
    
    PHP:

    I want to make a php page that will empty the table but will add the amount in 'worth' to another table called 'users', i want it to add 'worth' to each 'owner'

    i dont know how to explain it.

    basically i have a game, and i want to clear the garage but add the car money (worth) to the users money ('money' column in 'users' table) :)

    Please help, thanks
     
    CuBz, Jul 16, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Can you post the structure of the other table, and explain if there is any relationship between the two (EX: user_id is the same etc.)?
     
    jestep, Jul 16, 2009 IP
  3. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the structure of the other table is way too big to post, all i need to tell you that is in the table is, it is named 'users' and it has username and money column.

    They are the only columns that would need to connect to the garage

    The ID's are not the same

    The only relationship they have is in the 'garage' table 'username' would be the same to the 'owner' in the 'garage' table :)

    Thanks for your help
     
    CuBz, Jul 16, 2009 IP
  4. arunn

    arunn Active Member

    Messages:
    347
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #4
    yea i understand your requirement. must be a simple thing to do as well.

    you want to move the worth data from garage table to the respective usernames in users table and then clean the garage table.

    let me know if you want a coder as well.
     
    arunn, Jul 16, 2009 IP
  5. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i want to move the 'worth' data from the "garage" table to 'money' in "users" table

    but i want it to add onto it (worth+money)

    then EMPTY the garage table, not drop it

    and i can code, i am actually good at it, but i havnt coded in a long while and long story short i need to refresh my memory fully lol
     
    CuBz, Jul 16, 2009 IP
  6. arunn

    arunn Active Member

    Messages:
    347
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #6
    yea. that can be done as well. if you only need input tell me where you got struck.. if you need a coder as well check your PM. i can do this for you..
     
    arunn, Jul 16, 2009 IP
  7. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    can anybody else help me with this please
     
    CuBz, Jul 16, 2009 IP
  8. vpdesigns

    vpdesigns Peon

    Messages:
    353
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    $result = mysql_query("SELECT * FROM garage");  
    while($r=mysql_fetch_array($result))
    {     
    	$ownerq = mysql_query("SELECT * FROM `users` WHERE `username` = '".$r[owner]."'");
    	$o = mysql_fetch_array($ownerq);
    	$update = mysql_query("UPDATE `users` SET `money` = '".$o[money]+$r[worth]."' WHERE `username` = '".$r[owner]."'"); 
    	$delete = mysql_query("DELETE FROM garage WHERE `id`=".$r[id]); 
    } 
    PHP:
    I think that should do what your looking for it was only a 2 min job so i could be wrong.
     
    vpdesigns, Jul 17, 2009 IP
  9. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #9
    If the username is already inserted into each table you can reduce this to 2 queries. Let me know if there are possibly members in the users table that would need to be inserted into the garage table.

    Query 1:

    UPDATE `users` INNER JOIN garage ON `users`.username = garage.owner
    SET `users`.money = (`users`.money + garage.worth);

    Query 2:

    DELETE FROM `garage`;
     
    jestep, Jul 17, 2009 IP
  10. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10



    I tried that, but it didnt do anything at all, didn't delete anything and didnt add the money

    here is my code:

    <?php
    if (strip_tags($_POST['submit'])){
    
    $result = mysql_query("SELECT * FROM garage"); 
    while($r=mysql_fetch_array($result))
    {     
        $ownerq = mysql_query("SELECT * FROM `users` WHERE `username` = '".$r[owner]."'");
        $o = mysql_fetch_array($ownerq);
        mysql_query("UPDATE `users` SET `money` = '".$o[money]+$r[worth]."' WHERE `username` = '".$r[owner]."'");
        mysql_query("DELETE FROM garage WHERE `id`=".$r[id]);
        die("All Garages Cleared and money Refunded!");
    }}
    
    
    ?>
    
    <center><br><Br>
    
    <form method="post" name="form8" action="">
    
    <table height=100 width="600" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="#000000"><tr><td align=center background=includes/grad.jpg height=30>
    Empty Garages</td></tr><tr>
    
    
    <td align=center>Clear Garages and give everyone their money for the cars</td></tr>
    <td align=center><input type="submit" value="submit" name="submit" class="custombutton"></td></tr>
    
    </form></table>
    PHP:
     
    CuBz, Jul 17, 2009 IP
  11. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    can anyone help me with this please?
     
    CuBz, Jul 18, 2009 IP
  12. vpdesigns

    vpdesigns Peon

    Messages:
    353
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Is there an error log created when you ran the script ? or do any errors appear on the site. You also have to make sure that you include your configuration file or something which connects to the actual database.
     
    vpdesigns, Jul 18, 2009 IP
  13. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    No errors were shown, it just goes through the script and shows "All Garages Cleared and money Refunded!"

    but it does nothing to the garage or users money

    it is deffinately connected to the correct database and tables so i dno whats wrong
     
    CuBz, Jul 19, 2009 IP
  14. Martinoes

    Martinoes Peon

    Messages:
    110
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I didn't test this:) But you should try.
    
    
    UPDATE tb_users users SET users.money = users.money + (select garage.worth from tb_garage garage where garage.owner = users.username)
    
    DELETE FROM garage;
    
    
    PHP:
     
    Martinoes, Jul 22, 2009 IP