move data from 1 column to another in same table

Discussion in 'Databases' started by devo10, Feb 24, 2010.

  1. #1
    Hi

    Could somebody help please?

    I have a mysql table. When I update a column I want the past entry to be posted to the next column.

    The table collects quiz scores, so when the user gets a new score I want the old score to be placed into the ‘pastscore’ column.

    I’m pretty new to php and mysql, so could you possibly show me the language. I’ve been trying for hours.

    Cheers
     
    devo10, Feb 24, 2010 IP
  2. ServerUnion

    ServerUnion Peon

    Messages:
    3,611
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    0
    #2
    how about adding a new record for each text taken by the user including the datetime. You will then have a archived record of all the tests they have take.

    This would work too:

    UPDATE test
    SET pastscore=score
    ,score='newscore'
    WHERE .....
     
    ServerUnion, Feb 24, 2010 IP
  3. devo10

    devo10 Active Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    Thanks

    I'll try it now and get back to you asap. I'm just on my way out at the moment. it maybe tomorrow now.

    Many thanks in advance
     
    devo10, Feb 24, 2010 IP
  4. devo10

    devo10 Active Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #4
    Hi

    I’m having this problem. Here is my table:

    username quizid score date pastscore pastdate



    I want my quiz to work like this. After the student takes the quiz for a second time, the score is updated, and the previous score and date are moved to ‘pastscore’ and ‘pastdate’ respectively. Below is the whole code I am using, but it posts ‘score’ and ‘date’ into the database fields when I run the file. I can’t get it to ‘capture’ the actual scores and dates.

    ServerUnion, thanks for your help, but I couldn’t get your recommendation to work. Any ideas?







    <?php


    $username=$_REQUEST["username"];

    $quizid=$_REQUEST["quizid"];

    $score=$_REQUEST["score"];

    $dates=$_REQUEST["dates"];



    $con = mysql_connect("localhost","name","password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("scores", $con);


    mysql_query("UPDATE scoredate SET score = '$score'
    WHERE username = 'name' AND quizid = 't4'");


    mysql_query("UPDATE scoredate SET date = '$dates'
    WHERE username = 'name' AND quizid = 't4'");



    mysql_query("UPDATE scoredate SET pscore = 'score'
    WHERE username = 'name' AND quizid = 't4'");


    mysql_query("UPDATE scoredate SET pdate = 'date'
    WHERE username = 'name' AND quizid = 't4'");





    mysql_close($con);
    ?>
     
    devo10, Feb 24, 2010 IP
  5. ServerUnion

    ServerUnion Peon

    Messages:
    3,611
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try something like this, don't do much PHP,but you get the jist of it.

    
    
    mysql_query("UPDATE scoredate SET pscore = score, pdate=date,score = '$score', date = '$dates' 
    WHERE username = 'name' AND quizid = 't4'");
    
    
    Code (markup):
     
    ServerUnion, Feb 25, 2010 IP
  6. devo10

    devo10 Active Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #6
    Hi ServerU

    It’s getting closer, but now when I score the quiz, the new score and date is getting put into both the new score and date, ‘and’ the past score and date.

    I can’t see how this is happening.
     
    devo10, Feb 25, 2010 IP
  7. ServerUnion

    ServerUnion Peon

    Messages:
    3,611
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    0
    #7
    try doing 2 queries then.
     
    ServerUnion, Feb 25, 2010 IP
  8. ServerUnion

    ServerUnion Peon

    Messages:
    3,611
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    mysql_query("UPDATE scoredate SET pscore = score, pdate=date WHERE username = 'name' AND quizid = 't4'");
    
    mysql_query("UPDATE scoredate SET score = '$score', date = '$dates' 
    WHERE username = 'name' AND quizid = 't4'");
    
    
    
    Code (markup):
     
    ServerUnion, Feb 25, 2010 IP
  9. devo10

    devo10 Active Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #9
    Hi ServerU

    Sorry, now the code is just updating the score and date and the pscore and pdate are empty.
     
    devo10, Feb 25, 2010 IP
  10. devo10

    devo10 Active Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #10
    ServerU

    Is this correct? When the student does the quiz the first time, the score and date are stored in the ‘score’ and ‘date’ respectively. This is happening.

    When the student does the quiz again, the values of the ‘score’ and ‘date’ are captured and moved to the ‘pscore’ and ‘pdate’. This happens.

    From what I know (very little), the code you have given me ‘should’ capture the values of the ‘score’ and ‘date’. I think your code is correct, but this isn’t happening.

    Is this correct?
     
    devo10, Feb 25, 2010 IP
  11. devo10

    devo10 Active Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #11
    If I run the following code on its own, it updates the score and date perfectly. However, I want this to run as the student completes the quiz a second time. There appears to be a problem when I try to load the ‘new’ score in the same code.



    <?php


    $username=$_REQUEST["username"];

    $quizide=$_REQUEST["quizid"];



    $con = mysql_connect("localhost","name","pass");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("scores", $con);


    mysql_query("UPDATE scoredate SET pscore = score, pdate = date WHERE username = 'name' AND quizid = 't4'");


    mysql_close($con);
    ?>
     
    devo10, Feb 25, 2010 IP