【问题标题】:Transfer data from table to another table in same database将数据从表传输到同一数据库中的另一个表
【发布时间】:2020-07-26 11:00:51
【问题描述】:

我在单击按钮时将数据从“请求”表传输到“用户”表时遇到问题,

这是 adminApprove.php,管理员将在其中单击“传输数据”按钮:

<!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>

然后将转到approve.php,其中sql查询从表'request'移动到'user'表:

<?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();
   } 

?>

当我点击按钮时,它没有弹出任何错误,并且数据没有传输,非常感谢任何帮助,谢谢..

【问题讨论】:

标签: php html mysqli


【解决方案1】:

我很确定您也不理解您的查询。如果您的参数不存在,如何获取请求的 id。

按钮的链接应该是这样的 批准.php?id=$row["id"]

然后你在approve.php中的查询应该是 $sql = "select username, addr, phone, pwd from requests where id = '".$id."'

【讨论】:

    猜你喜欢
    • 2012-07-27
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2017-05-02
    • 2017-11-16
    • 2012-04-19
    相关资源
    最近更新 更多