Transferring data from one table to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • computerfox
    Contributor
    • Mar 2010
    • 276

    Transferring data from one table to another

    I'm writing a work order system using PHP and MySQL and I'm stuck on two things-transferring data and updating the data.

    Here is my code:

    i'm using checklogin.php to do most of everything so it's most likely something I'm missing here.

    checklogin.php

    Code:
    <?php
    $host="example.com"; // Host name 
    $username="gancsosa"; // Mysql username 
    $password="******"; // Mysql password 
    $db_name="gancsosa_members"; // Database name 
    $tbl_name="logon"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db($db_name);
    // username and password sent from form 
    $Username=$_POST['Username']; 
    $Password=$_POST['Password'];
    
    // To protect MySQL injection (more detail about MySQL injection)
    $Username = stripslashes($Username);
    $Password = stripslashes($Password);
    $Username = mysql_real_escape_string($Username);
    $Password = mysql_real_escape_string($Password);
    
    $sql="SELECT * FROM $tbl_name WHERE username='$Username' and password='$Password'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("Username");
    session_register("Password"); 
    
    
    mysql_select_db(gancsosa_crystalworks);
    
    echo "\n";
    
    $link = mysql_connect('example.com', 'gancsosa', '*****');
    mysql_select_db("gancsosa_crystalworks") or die(mysql_error()); 
    $data = mysql_query("SELECT * FROM inprogress")
    or die(mysql_error()); 
    
    
    //Transfer data
    
    
    
    Print "<br><br>";
    
    Print "Inprogress<br><br>";
    
    Print "<table border cellpadding=3>"; 
    while($info = mysql_fetch_array( $data )) 
    { 
    @mysql_query( "UPDATE INTO complete SELECT  FROM inprogress  WHERE Status='1'  ");
    
    Print "<th>ID: ".$info['ID'] . "</th> "; 
    Print "<th>Time: " .$info['Time'] . "</th>";
    Print "<th>Name: ".$info['Client'] . " </th>"; 
    Print "<th>Number: " .$info['Number'] . "</th>";
    Print "<th>Address: " .$info['Address'] . "</th>";
    Print "<th>Issue: " .$info['Issue'] . "</th>";
    Print "<th>Notes: " . $info['Notes'] . "</th>";
    Print "<th>Status: " .$info['Status'] . "</th>";
    Print "<th>Charge: $" .$info['Charge'] . "</th>";
    Print '<th><a href="http://www.1fixcomputermedic.com/update.php" target="_blank">Edit?</a><th>';
    Print "<br><br>";
    } 
    Print "</table>"; 
    
    Print "<br>";
    Print "<br>";
    
    
    
    
    Print "<br><br><br>Complete<br><br>";
    
    $data = mysql_query("SELECT * FROM complete") 
    or die(mysql_error()); 
    
    
    Print "<br><br>";
    
    Print "<table border cellpadding=6>"; 
    while($info = mysql_fetch_array( $data )) 
    { 
    Print "<tr>"; 
    Print "<th>ID:</th> <td>".$info['ID'] . "</td> "; 
    Print "<th>Time:</th> <td>" .$info['Time'] . "</td>";
    Print "<th>Name:</th> <td>".$info['Client'] . " </td>"; 
    Print "<th>Number:</th> <td>" .$info['Number'] . "</td>";
    Print "<th>Address:</th> <td>" .$info['Address'] . "</td>";
    Print "<th>Issue:</th> <td>" .$info['Issue'] . "</td>";
    Print "<th>Notes:</th><td>" . $info['Notes'] . "</td>";
    Print "<th>Notes:</th> <td>" .$info['Notes2'] . "</td>";
    Print "<th>Status:</th> <td>" .$info['Status'] . "</td>";
    Print "<th>Charge:</th> <td>$" .$info['Charge'] . "</td></tr>";
    } 
    Print "</table>"; 
    }
    
    else {
    echo "Wrong Username or Password\r\n";
    
    echo "Please try again....<br><br>";
    echo '<a href="http://www.1fixcomputermedic.com/login.php">CLICK</a>';
    
    }
    ?>
    Now for my second problem.

    update.php

    Code:
    <html>
    <?
    
    $ID=$_GET['ID'];
    $username="gancsosa";
    $password="*****";
    $database="gancsosa_crystalworks";
    mysql_connect("example.com",$username,$password);
    @mysql_select_db($database);
    $query=" SELECT * FROM inprogress WHERE ID='$ID";
    $result=mysql_query($query);
    mysql_close();
    
    
    //Print output
    
    $Client=mysql_result($result,"Client");
    $Number=mysql_result($result,"Number");
    $Address=mysql_result($result,"Address");
    $Issue=mysql_result($result,"Issue");
    $Notes=mysql_result($result,"Notes");
    $Status=mysql_result($result,"Status");
    $Charge=mysql_result($result,"Charge");
    
    ?>
    
    <form action="updated.php" method="post">
    <input type="hidden" name="ud_ID" value="<? echo $ID; ?>">
    Client: <input type="varchar(56)" name="ud_Client" value="<? echo $Client; ?>"><br>
    Address: <input type="text" name="ud_Address" value="<? echo $Address; ?>"><br>
    Number: <input type="bigint(20)" name="ud_Number" value="<? echo $Number; ?>"><br>
    Issue: <input type="longtext" name="ud_Issue" value="<? echo $Issue; ?>"><br>
    Notes: <input type="longtext" name="ud_Notes" value="<? echo $Notes; ?>"><br>
    Status: <input type="tinyint(1)" name="ud_Status" value="<? echo $Status; ?>"><br>
    <input type="Submit" value="Update">
    </form>
    
    <?
    //Update
    $ud_Address=$_POST['ud_Address'];
    $ud_Number=$_POST['ud_Number'];
    $ud_Issue=$_POST['ud_Issue'];
    $ud_Notes=$_POST['ud_Notes'];
    $ud_Status=$_POST['ud_Status'];
    
    
    $username="gancsosa";
    $password="*****";
    $database="gancsosa_crystalworks";
    mysql_connect("example.com",$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    $query="UPDATE inprogress SET Address='$ud_Address', Number='$ud_Number', Issue='$ud_Issue', Notes='$ud_Notes2', Status='$ud_Status' WHERE ID='ud_ID' ";
    mysql_query($query);
    mysql_close();
    
    ?>
    
    
    </html>
    Any help would be GREATLY appreciated.
    Last edited by Atli; Mar 12 '10, 03:08 AM. Reason: Replaced the real SQL login info with example values.
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    Anyone? Any ideas? ..............

    Comment

    • computerfox
      Contributor
      • Mar 2010
      • 276

      #3
      Thank you for replacing my actual information. LOL I was quite worried about everyone seeing it.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hey.

        What are you having problems with, exactly?
        What are you expecting the code to do? What is it actually doing?
        Are you getting any error messages? - We LOVE error messages :)
        (Keep in mind that we can't actually run the code on our own test machines.)

        A few things I notice, though, that may help.
        • In both of your code examples, you connect to MySQL twice. This is unnecessary, and a waste of resources. A single call to the mysql_connect function is enough, unless you have a specific reason for needing to call it more often.
        • On lines #31 and #32 you use the session_registe r function. This function is deprecated and should not be used anymore. - Instead, you should register session variables like so:
          [code=php]$_SESSION['username'] = $username;[/code]
        • Indentation - You should always write your code as if the guy who will be maintaining it is a homicidal maniac that knows where you live. It will make your own work on the code easier, and it makes it easier for people like myself - who were not involved in writing it - to work on it.

        Comment

        • computerfox
          Contributor
          • Mar 2010
          • 276

          #5
          Thank you for the response and advise.

          No error messages. It's just not transferring the information from one table to another.

          When I go into the data page, I want to make sure that the data is in the right tables.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Ok, so after going a bit more thoroughly over it, I think I've found what is causing your main problem.

            #1
            In your second script, "update.php ", you have a SQL query at the very end of it, line #54. - The problem is the WHERE clause. Instead of using the "ud_ID" from the form (the $_POST array), you are using the name as a static string. That will cause MySQL to either fail with an error (which you don't seem to even check for), or always update the first row. - Either way, you need to replace that with the value passed from the form. (Like you do with the other values in that query.)

            #2
            Earlier in that script, you do this:
            [code=php]$Client=mysql_r esult($result," Client");
            $Number=mysql_r esult($result," Number");
            $Address=mysql_ result($result, "Address");
            // etc...[/code]
            This is incorrect usage of the mysql_result function. Instead, you should be fetching the entire row using the mysql_fetch_ass oc function and using the return array you get from that.
            [code=php]$row = mysql_fetch_ass oc($result);
            $Client=$row['Client'];
            $Number=$row['Number'];
            $Address=$row['Address'];
            // etc...[/code]

            #3
            On line #13 of the second example, you have a SQL query. It is missing a quotation mark at the end. The single-quote after the $ID is missing. It needs to be added. - Also, there is an extra space in front of the SELECT that should be removed.

            #4
            I don't see anything in the first code that would "break" it. It should work, even though there are a few things that could use some "tuning". - However, there is one thing that should be removed or fixed. On line #56 you have a SQL query that is just... wrong. - There is no such thing as a UPDATE INTO query. -Not sure exactly what you were trying to do there, but I would simply remove that line.

            Hope that helps any.

            Comment

            • computerfox
              Contributor
              • Mar 2010
              • 276

              #7
              LOL!!!! Yeah, I had INSERT INTO and it wouldn't transfer the data so I tried out the UPDATE. LOL!!!! I'm not THAT much of a PHP noob. Am I?

              Comment

              • computerfox
                Contributor
                • Mar 2010
                • 276

                #8
                I did what you told me to, but unfortunately it still doesn't update the data. :(

                Comment

                • computerfox
                  Contributor
                  • Mar 2010
                  • 276

                  #9
                  I got it to transfer from table to table, but it still doesn't update for some REALLY annoying reason. I'm either being really stupid or something's dumb with the code....

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    How does your update script look now?

                    Comment

                    • computerfox
                      Contributor
                      • Mar 2010
                      • 276

                      #11
                      Code:
                      <html>
                      <?
                      
                      $ID=$_GET['ID'];
                      $username="gancsosa";
                      $password="************";
                      $database="gancsosa_crystalworks";
                      mysql_connect("njcomputermedic.fatcowmysql.com",$username,$password);
                      mysql_select_db($database);
                      $query="SELECT*  FROM gancsosa_crystalworks.inprogress WHERE ID='$ud_ID' ";
                      $result=mysql_query($query);
                      
                      
                      //Print output
                      $row=mysql_fetch_assoc($result);
                      
                      $ID=$row['ID'];
                      $Client=$row['Client'];
                      $Number=$row['Number'];
                      $Address=$row['Address'];
                      $Issue=$row['Issue'];
                      $Notes=$row['Notes'];
                      $Status=$row['Status'];
                      $Charge=$row['Charge'];
                      
                      ?>
                      
                      <form action="updated.php" method="post">
                      <input type="hidden" name="ud_ID" value="<? echo $ID; ?>">
                      Issue: <input type="longtext" name="ud_Issue" value="<? echo $Issue; ?>"><br>
                      Notes: <input type="longtext" name="ud_Notes" value="<? echo $Notes; ?>"><br>
                      Status: <input type="tinyint(1)" name="ud_Status" value="<? echo $Status; ?>"><br>
                      <input type="Submit" value="Update">
                      </form>
                      
                      <?
                      //Update
                      $ud_ID=$_POST['ud_ID'];
                      $ud_Issue=$_POST['ud_Issue'];
                      $ud_Notes=$_POST['ud_Notes'];
                      $ud_Status=$_POST['ud_Status'];
                      
                      
                      $username="gancsosa";
                      $password="***********";
                      $database="gancsosa_crystalworks";
                      mysql_connect("*******************",$username,$password);
                      
                      $query="UPDATE  gancsosa_crystalworks.inprogress SET  Issue='$ud_Issue', Notes='$ud_Notes2', Status='$ud_Status' WHERE ID='$ud_ID' ";
                      
                      mysql_query($query);
                      
                      ?>
                      
                      
                      </html>

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #12
                        Line #11. The WHERE clause is using the wrong ID variable. It is using $ud_ID where it should be using $ID. (The $ud_ID variable is created later in the code.)

                        Comment

                        • computerfox
                          Contributor
                          • Mar 2010
                          • 276

                          #13
                          I changed that and I even tested the value of ID that the _GET function was pulling in and it printed nothing. So I'm guessing it's not updating because it's not pulling in the information because it's not pulling in the ID :(

                          Comment

                          • Atli
                            Recognized Expert Expert
                            • Nov 2006
                            • 5062

                            #14
                            How does the URL you are requesting look like?
                            The "$ID=$_GET['ID'];" line is pulling the ID from the URL, so it should look something like:

                            Where "xx" is the ID of the record you are trying to update.

                            Comment

                            • computerfox
                              Contributor
                              • Mar 2010
                              • 276

                              #15
                              Okay, so i made it http://localhost/update.php?ID=01.

                              It takes in the information, but doesn't update the table with new information after submit has been clicked. I'm guessing it's something with my query....

                              Comment

                              Working...