正在跳过DELETE ROW命令而没有任何错误

时间:2017-01-18 05:30:44

标签: php

我有两个表,table1有多行数据,而table2是空的。

我试图在php中执行一个简单的INSET和DELETE命令,将所选行从table1添加到table2,同时从table1中删除所选行

但是在这里,它会被插入到表2中并且不会从表1中删除但是返回主页而没有任何错误。

我的代码如下:

从table1调用行内容到用户页面:

<?php 
$sql2 = "SELECT * FROM apromocode ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {                               
    while($row2 = $result2->fetch_assoc()) {
    ?>          
        <tr >
            <form id="promocodesub" action="apromocodesubdelete.php" method="post">
                <td>
                    <?php echo ($row2["id"]); ?>
                </td>
                <td>
                    <?php echo ($row2["code"]); ?>
                </td>
                <input type="hidden" form="promocodesub" name="code" value="<?php echo ($row2["code"]); ?>"/>
                <td>
                    <?php echo ($row2["occation"]); ?>
                </td>
                <input type="hidden" form="promocodesub" name="occation" value="<?php echo ($row2["occation"]); ?>"/>
                <td>
                    <?php echo ($row2["about"]); ?>
                </td>
                <input type="hidden" form="promocodesub" name="about" value="<?php echo ($row2["about"]); ?>"/>
                <td>
                    <?php echo ($row2["discount"]); ?>
                </td>
                <input type="hidden" form="promocodesub" name="discount" value="<?php echo ($row2["discount"]); ?>"/>
                <td>
                    <?php echo ($row2["date"]); ?>
                </td>
                <td>
                    <button form="promocodesub" type="submit" name="deleteId" value="<?php echo ($row2["id"])?>"class="promocodedelete">Delete</button>
                </td>
            </form>
        </tr>  
    <?php
    }
} else {
    echo "0 results";
}
?>

因此,当用户打开页面时,他会看到所有行内容按顺序排列,并且&#34;删除&#34;每个按钮。当用户按下该特定行的删除按钮时,它将插入到table2中,并应使用以下脚本从table1中删除:

<?php include('amerchantassign.php'); ?>
<?php
$servername  = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "";
$code = $_POST['code'];
$occation = $_POST['occation'];
$about = $_POST['about'];
$discount = $_POST['discount'];
$date = date_default_timezone_set('Asia/Kolkata');
$date = date('M-d,Y H:i:s');
enter code here

$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$deleteId= $_POST['deleteId'];

$sql="INSERT INTO apromocodehistory (code, occation, about, discount, date)
SELECT code, occation, about, discount, date
FROM   apromocode 
WHERE  id=  $deleteId";

if ($conn->query($sql) === TRUE) {
    $sql4 = "DELETE FROM feedback WHERE id = '$deleteId' ";

    if ($conn->query($sql4) === TRUE) {
        echo '<a href="amerchantassign.php"></a>';
    } else {
        echo "ERROR" . $sql4;
    }
} else {
    echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

任何建议都表示赞赏......

2 个答案:

答案 0 :(得分:1)

使用

<td><button form="promocodesub" type="submit" name="deleteId" value="<?php echo ($row2["id"]) ?>" class="promocodedelete">Delete</button></td>

提交后

$deleteId= $_POST['deleteId']; 

在上面的Id上应用选择,然后应用删除操作。

$sql="INSERT INTO apromocodehistory (code, occation, about, discount, date)
      SELECT code, occation, about, discount, date
      FROM   apromocode 
      WHERE  id=  $deleteId";
 if ($conn->query($sql) === TRUE) {
 echo '<a href="amerchantassign.php"></a>';
 }
 else {
 echo "ERROR" . $sql . "<br>" . $conn->error;
 }

答案 1 :(得分:0)

这里的问题是表单id属性。您使用相同的ID promocodesub循环表单并在<button form='promocodesub'>内使用相同的内容。

因此所有表单都具有相同的id,而button表单属性指定了它所属的表单。所以在这种情况下,它指的是最后一种形式。