doutuoben6908 2018-08-11 06:12
浏览 38
已采纳

elseif不能按预期在php中工作[重复]

This question already has an answer here:

<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test_db";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

//check radio button and update sql accordingly

if (isset($_GET['calc'])) {

$calc = $_GET['calc'];
}

if ($calc='average'){
$sql1 = "UPDATE `config` SET `calc_category`=1 WHERE 1";
$conn->query($sql1);
} elseif ($calc='high'){
$sql1 = "UPDATE `config` SET `calc_category`=2 WHERE 1";
$conn->query($sql1);
} else {
$sql1 = "UPDATE `config` SET `calc_category`=3 WHERE 1";
$conn->query($sql1);
}


?>

In the above code even if the $calc is equal to 'high' or something other than 'average', it still updates the database with 'calc_category'=1 which is only supposed to happen when first 'if' condition is true.

I have echoed the value of $calc and it changes based on which radio button is selected but I am not sure why always first 'if' condition code is getting executed.

I am new to PHP,SQL please help me where am I going wrong.

</div>
  • 写回答

3条回答 默认 最新

  • dt2002 2018-08-11 06:16
    关注

    double equal to needed...

     if ($calc=='average'){ /****// == instead of = //*****/
         $sql1 = "UPDATE `config` SET `calc_category`=1 WHERE 1";
         $conn->query($sql1);
     } elseif ($calc=='high'){ /****// == instead of = //***/
         $sql1 = "UPDATE `config` SET `calc_category`=2 WHERE 1";
         $conn->query($sql1);
     } else {
         $sql1 = "UPDATE `config` SET `calc_category`=3 WHERE 1";
         $conn->query($sql1);
     }
    

    also check your query near Where clause.... it is where 1 ?????

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画