如何从一个表中移动一行到mysql中的另一个表?

时间:2017-02-04 15:35:41

标签: php html mysql mysql-error-1064

帮助。我怎么能从一个表移动或传输一行到mysql中的另一个表?单击"批准"后,所选行将转移到数据库中的另一个表。请检查我的代码。谢谢!

message.php

<?php


include('connect.php');
$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM message");
while($row = mysqli_fetch_array($result))
{
    echo '<tr class="record">';
    echo '<td style="background-color: #F8F8F8 ;font-size: 13px; color: maroon; 
        font-weight: bold; border-left: 1px solid #C1DAD7;">'.$row['name'].'</td>';
    echo '<td><div style="font-size: 13px;" align="left">'.$row['address'].'</div></td>';
    echo '<td><div style="color:maroon; font-size: 13px;"align="left">Email:<br></div>
        <div style="font-size: 13px;"align="left">'.$row['email'].'</div><br>
        <div style="font-size: 13px; color:maroon;"align="left">Contact No.<br></div>
        <div style="font-size: 13px;"align="left">'.$row['number'].'</div></td>';
    echo '<td><div style="font-size: 13px;"align="left">'.$row['services'].'</div></td>';
    echo '<td><div style="color:maroon; font-size: 13px;"align="left">Desired Date:<br></div>
        <div style="font-size: 13px;"align="left">'.$row['shootdate'].'</div><br>
        <div style="font-size: 13px; color:maroon;"align="left">Desired Time: <br></div>
        <div style="font-size: 13px;"align="left">'.$row['shoottime'].'</div></td>';
    echo '<td><div style="font-size: 13px;"align="left">'.$row['place'].'</div></td>';
    echo '<td><div style="font-size: 13px;"align="left">'.$row['message'].'</div></td>';
    echo '<td><div align="center">
        <a href="acceptmessage.php" id="'.$row['message_id'].'" class="acceptbutton" 
            title="Click To Approve">Approve</a><br><br>
        <a href="deletemessage.php" id="'.$row['message_id'].'" class="delbutton" 
            title="Click To Decline" style="color:red;">Decline</a></div></td>';
    echo '</tr>';
    }
?> 

acceptmessage.php

<?php
    include('../db.php');
    if($_GET['id']){
    $id = (isset($_GET['id']) ? $_GET['id'] : null); 
    $sql = "INSERT INTO accept (name, address, email, number, services, shootdate, shoottime, place) SELECT name, address, email, number, services, shootdate, shoottime, place FROM message WHERE message_id='$id'";
    mysqli_query($GLOBALS["___mysqli_ston"],  $sql);}
?>

1 个答案:

答案 0 :(得分:1)

如果列message_id是一个int我认为你应该从$ id中删除单引号,如下所示:

$sql = "INSERT INTO accept (name, address, email, number, services, shootdate, shoottime, place) SELECT name, address, email, number, services, shootdate, shoottime, place FROM message WHERE message_id=$id";

P.S。您应该在查询中使用预准备语句,而不是直接插入变量,这样可以避免SQL注入。

这样的事情:

$sql = "INSERT INTO accept (name, address, email, number, services, shootdate, shoottime, place) SELECT name, address, email, number, services, shootdate, shoottime, place FROM message WHERE message_id=?";
$stmt = $connection->prepare($sql);
$stmt->bindValue(1, $id);
$stmt->execute();