简体   繁体   中英

Transfer data from table to another table in same database

i have problem to transfer data from 'requests' table to 'user' table on button click,

This is the adminApprove.php, where admin will click the 'Transfer data' button:

<!DOCTYPE html>
<html>
</html>
<head>
</head>
<body>

    <h1 align="center">User List To be Approved</h1>

    <br>
    <table border='1' align="center">
        <thead>
    <tr>
        <th align="center">Name</th>
        <th>Address</th>
        <th>Phone</th>
        <th>Action</th>
    </tr>
</thead>

<?php
    // connect to the database
    include('dbconfig.php');

    // get record from db
    $sql = "SELECT id, username, addr, phone from requests";
    $result = $mysqli-> query($sql);

        if ($result->num_rows > 0){

                while ($row = $result-> fetch_assoc()){

                        echo "<tr><td>". $row["username"] ."</td>
                        <td>". $row["addr"] ."</td>
                        <td>". $row["phone"]. "</td>

                        <td>". "<a href='approve.php'" . $row["id"] . "'>Transfer Data</a>". 
                        "</td></tr>";
                }

                echo "</table>";
        }
            else {
                echo "No data to display";
            }

        $mysqli->close();
?>
</table>
</body>
</html>

and will go to the approve.php, where the sql queries for moving from table 'request' to 'user' table:

<?php
include('dbconfig.php');


if (isset($_GET['id']) && is_numeric($_GET['id']))
{

//$id = $_GET['id'];


$sql = "SELECT username, addr, phone, pwd from requests'";

$query = $mysqli->query($sql);

        if(mysqli_num_rows($query) >= 1){

            while ($row = $result-> fetch_assoc()){

            $username = $row['username'];
            $addr = $row['addr'];
            $phone = $row['phone'];
            $pwd = $row['pwd'];

            $sql = "INSERT INTO user (username, addr, phone, pwd) VALUES ('$username', '$addr', '$phone','$pwd'";

            echo "<script type='text/javascript'>alert('success');
            window.location = '';</script>";

        }

            
        }else{
            
          echo "<script type='text/javascript'>alert('Failed');
            window.location = 'home.php';</script>";
        }

        $mysqli->close();
   } 

?>

as i click the button, It doesnt popout any error, and the data not transfered, Any helps really appreciated, thank you..

Im pretty sure that you also doesn't understand your query. How can you get the id of the request if your parameter is doesn't exist.

The link for your button should be like this approve.php?id=$row["id"]

And then your query in approve.php should be $sql = "select username, addr, phone, pwd from requests where id = '".$id."'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM