Results 1 to 2 of 2

Thread: moving data from one table to another

  1. #1
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default moving data from one table to another

    I am new to php, mysql, I have 2 tables (table1 and table2) and wish to move data from table1 to table2 such that when a user filled in a reg_number in the form field and clicks the submit button it executes the code and opens a fresh page named "register.php.

    I am having problem with this code as it fails to execute and returning blank page. Please help me in resolving this problem.
    here is the form:


    <form action="discharge.php" method="post">
    <p>INSERT STUDENT'S REGISTRATION NUMBER
    <input type="text" name="reg_number" value="">
    </p>
    <p>
    <input type="submit" value="Submit">
    </p>
    </form>

    and
    here is my discharge.php code:

    <?php

    include('connect.php');

    // username and password sent from form
    $reg_number=$_POST["reg_number"];

    $sql="INSERT INTO table2 (reg_number, surname,first_name, othername, gender)
    SELECT reg_number, surname,first_name, othername, gender FROM table1 WHERE reg_number='$reg_number'";

    DELETE * FROM table1 WHERE reg_number='$reg_number'";
    $result=mysql_query($sql);
    if(!$result)
    {
    header ("location:register.php");
    }


    ?>

  2. #2
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I wouldnt nest the select query as you cant really error trap that I would rather do the query and if there is a result do the insert, if the insert happens do the delete, all the way through you can have three error traps

    if select {great} else {error}
    if insert {great} else {error}
    if delete {great} else {error}

    this will allow you to see at what stage your script is failing and stop the blank screen of unknown you have

    I also say that you should never pass a variable into an SQL query directly without sanitizing it first:

    $reg_number=$_POST["reg_number"]; //This is Dirty and vulnerable

    //You can imagine someone putting a nested mysql query into your form field and dropping or truncating your tables, disaster.

    $reg_number=mysql_real_escape_string($_POST["reg_number"]); //Slightly better...

    //if the reg_number is always going to be a number you could validate using is_numeric(); this would help stop nested queries

    if (is_numeric($reg_number)) {
    //Do the mysql stuff because it is safe to do so
    }
    else
    {
    //redirect back to try again or just echo error message and then exit;
    }


    Hope this helps and does not confuse things too much

Similar Threads

  1. Replies: 0
    Last Post: 05-26-2014, 03:38 AM
  2. How to import data from one table to other
    By gurmeet in forum MySQL and other databases
    Replies: 4
    Last Post: 04-21-2011, 02:51 AM
  3. Resolved Moving records from one table to another
    By Schmoopy in forum PHP
    Replies: 3
    Last Post: 04-10-2009, 01:38 AM
  4. image/table moving along with scrolling page
    By questions in forum Looking for such a script or service
    Replies: 7
    Last Post: 02-16-2008, 04:45 AM
  5. moving table
    By spyder in forum JavaScript
    Replies: 0
    Last Post: 07-01-2005, 06:35 PM

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
  •