dongsuiying7773 2018-01-30 19:34
浏览 9

即使尚未创建行,也会执行数据库查询

I can't understand why mysqli_query returns true on an operation where one of the fields doesn't exist. I am trying to UPDATE a field when the field run_id matches the variable $id. However, when $id isn't set, the echo inside the if statement executes. Would really appreciate your thoughts on this phenomenon.

function sumMiles($id,$runnerRow,$weekArray,$cnn){
    //Calculate cumulative miles per week for every runner
    $weekMiles=0; //total miles per week
    foreach($weekArray as $weekday)
        {
            $weekMiles+=$runnerRow[$weekday];
        }
        $addMiles = "UPDATE run SET weekly_total = {$weekMiles} WHERE run_id=$id";//add weekly total into the database's run table 
        if (mysqli_query($cnn,$addMiles)){
            echo "Your weekly total has been updated";
            return $weekMiles;
        }
        else {echo "Your weekly total could not be updated";}
    }

I am calling this function like so:

$weekMiles = sumMiles($id,$dailyMileage,$weekArray,$cnn);
  • 写回答

1条回答 默认 最新

  • donglian8407 2018-01-30 19:39
    关注

    The fact that the line doens't exist will not generate any exception at the database.

    Verify the existence of the line first before run your update

      select count(1) from  run WHERE run_id=$id
    

    EDIT

    You need to check if the Count(1) is bigger than 0

    $query = "SELECT count(1) from run WHERE run_id=$id"; 
    if (mysqli_query($cnn,$query)>0){ 
     $addMiles = "UPDATE run SET weekly_total = {$weekMiles} WHERE run_id=$id"; 
     echo "updated"; 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)