du1462 2019-07-07 02:56
浏览 61

如何修复运行两次的sql代码

I have a select query from my database which is working fine but when I run my sql insert query it is adding the insert twice into my database.

I've tried so many things that I am lost on what else I could try.

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

I am trying to add a new record into the database with the column plannumber increasing my 1

Here is the new code and it is running the code twice.

<?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");
?>

The output is a set of records with one plannumber and another set of records with plannumber + 1.

I am new to coding so what would I be looking for that may be calling the query a second time?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私