首页 > 解决方案 > 如何修复运行两次的sql代码

问题描述

我的数据库中有一个选择查询,它工作正常,但是当我运行我的 sql 插入查询时,它会将插入两次添加到我的数据库中。

我已经尝试了很多东西,以至于我迷失了我还能尝试的东西。

<?php 
session_start();
include_once("db.php");

$loggenOnUser=$_SESSION["user_id"];

$plannumber = "SELECT * FROM goals WHERE uid = $loggenOnUser ORDER BY 
plannumber DESC LIMIT 1";
$duration = $conn->query($plannumber);
while($record = $duration->fetch_array()){
$total = $record['plannumber']+1;
}

?>

<body>
  <?php 

    $sql = "INSERT INTO goals (uid, plannumber, remainingdays)
            VALUES 
             ('$loggenOnUser', '$total', '90')";

        if (mysqli_query($conn,$sql)) {
            echo "New records created successfully";
        } else {
            echo "";
        }
    mysqli_close($conn);
 ?>

</body>
</html>

我正在尝试将新记录添加到数据库中,列 plannumber 增加我的 1

这是新代码,它正在运行代码两次。

<?php 
session_start();
include_once("db.php");

$loggedOnUser=$_SESSION["user_id"];

$plannumber = "SELECT * FROM goals WHERE uid = $loggedOnUser ORDER BY 
plannumber DESC 
LIMIT 1";
$duration = $conn->query($plannumber);
while($record = $duration->fetch_array()){
$total = $record['plannumber']+1;
}

$sql = "INSERT INTO goals (uid, plannumber, remainingdays)
    VALUES 
     ('$loggedOnUser', '$total', '90'),
     ('$loggedOnUser', '$total', '89'),
     ('$loggedOnUser', '$total', '88'),
     ('$loggedOnUser', '$total', '87'),
     ('$loggedOnUser', '$total', '86'),
     ('$loggedOnUser', '$total', '85'),
     ('$loggedOnUser', '$total', '84'),
     ('$loggedOnUser', '$total', '83'),
     ('$loggedOnUser', '$total', '82'),
     ('$loggedOnUser', '$total', '81'),
     ('$loggedOnUser', '$total', '80'),
     ('$loggedOnUser', '$total', '79'),
     ('$loggedOnUser', '$total', '78'),
     ('$loggedOnUser', '$total', '77'),
     ('$loggedOnUser', '$total', '76'),
     ('$loggedOnUser', '$total', '75'),
     ('$loggedOnUser', '$total', '74'),
     ('$loggedOnUser', '$total', '73'),
     ('$loggedOnUser', '$total', '72'),
     ('$loggedOnUser', '$total', '71'),
     ('$loggedOnUser', '$total', '70'),
     ('$loggedOnUser', '$total', '69'),
     ('$loggedOnUser', '$total', '68'),
     ('$loggedOnUser', '$total', '67'),
     ('$loggedOnUser', '$total', '66'),
     ('$loggedOnUser', '$total', '65'),
     ('$loggedOnUser', '$total', '64'),
     ('$loggedOnUser', '$total', '63'),
     ('$loggedOnUser', '$total', '62'),
     ('$loggedOnUser', '$total', '61'),
     ('$loggedOnUser', '$total', '60'),
     ('$loggedOnUser', '$total', '59'),
     ('$loggedOnUser', '$total', '58'),
     ('$loggedOnUser', '$total', '57'),
     ('$loggedOnUser', '$total', '56'),
     ('$loggedOnUser', '$total', '55'),
     ('$loggedOnUser', '$total', '54'),
     ('$loggedOnUser', '$total', '53'),
     ('$loggedOnUser', '$total', '52'),
     ('$loggedOnUser', '$total', '51'),
     ('$loggedOnUser', '$total', '50'),
     ('$loggedOnUser', '$total', '49'),
     ('$loggedOnUser', '$total', '48'),
     ('$loggedOnUser', '$total', '47'),
     ('$loggedOnUser', '$total', '46'),
     ('$loggedOnUser', '$total', '45'),
     ('$loggedOnUser', '$total', '44'),
     ('$loggedOnUser', '$total', '43'),
     ('$loggedOnUser', '$total', '42'),
     ('$loggedOnUser', '$total', '41'),
     ('$loggedOnUser', '$total', '40'),
     ('$loggedOnUser', '$total', '39'),
     ('$loggedOnUser', '$total', '38'),
     ('$loggedOnUser', '$total', '37'),
     ('$loggedOnUser', '$total', '36'),
     ('$loggedOnUser', '$total', '35'),
     ('$loggedOnUser', '$total', '34'),
     ('$loggedOnUser', '$total', '33'),
     ('$loggedOnUser', '$total', '32'),
     ('$loggedOnUser', '$total', '31'),
     ('$loggedOnUser', '$total', '30'),
     ('$loggedOnUser', '$total', '29'),
     ('$loggedOnUser', '$total', '28'),
     ('$loggedOnUser', '$total', '27'),
     ('$loggedOnUser', '$total', '26'),
     ('$loggedOnUser', '$total', '25'),
     ('$loggedOnUser', '$total', '24'),
     ('$loggedOnUser', '$total', '23'),
     ('$loggedOnUser', '$total', '22'),
     ('$loggedOnUser', '$total', '21'),
     ('$loggedOnUser', '$total', '20'),
     ('$loggedOnUser', '$total', '19'),
     ('$loggedOnUser', '$total', '18'),
     ('$loggedOnUser', '$total', '17'),
     ('$loggedOnUser', '$total', '16'),
     ('$loggedOnUser', '$total', '15'),
     ('$loggedOnUser', '$total', '14'),
     ('$loggedOnUser', '$total', '13'),
     ('$loggedOnUser', '$total', '12'),
     ('$loggedOnUser', '$total', '11'),
     ('$loggedOnUser', '$total', '10'),
     ('$loggedOnUser', '$total', '9'),
     ('$loggedOnUser', '$total', '8'),
     ('$loggedOnUser', '$total', '7'),
     ('$loggedOnUser', '$total', '6'),
     ('$loggedOnUser', '$total', '5'),
     ('$loggedOnUser', '$total', '4'),
     ('$loggedOnUser', '$total', '3'),
     ('$loggedOnUser', '$total', '2'),
     ('$loggedOnUser', '$total', '1')";

if (mysqli_query($conn,$sql)) {
    echo "New records created successfully";
} else {
    echo "";
}
mysqli_close($conn);

header("Location: http://www.example.com/goals");
?>

输出是一组带有一个 plannumber 的记录和另一组带有 plannumber + 1 的记录。

我是编码新手,所以我会寻找什么可能会再次调用查询?

标签: phpsql

解决方案


推荐阅读