doumao1047 2019-07-30 14:02
浏览 116
已采纳

运行此代码后,为什么我的SQL表数据会被删除?

I have this code that I want to run and it's connected to my database and it uses to show the values on the table but every time I do an UPDATE query.

It updates the rest of the row with zeros except the one I've chosen. I want to keep my numbers on the row but only update the one that i chose but I can't figure it out how to!

I initialized the table with a row with Zeros (0s) and then with my web page application I want to change those values.

But if I have another value on the rest of the columns and I want to change this one, the rest of the values go to zero (the initial state of the table) (I don't know how to explain it better I think)

<?php 
                      $showData = ("SELECT * FROM horario");
                      if ($new_user_activity['data'] == 'Seg' ) {
                          $segHora = $new_user_activity['horas'];
                      }
                          $sql = ("UPDATE horario SET seg=$segHora WHERE id=1");
                          if(!mysqli_query($conn,$sql)) {
                            echo("ERRO NA QUERY" );
                          }
                          else {
                            if(mysqli_query($conn,$sql)){
                              $sqlData = mysqli_query($conn,$showData);
                              while ($row = mysqli_fetch_array($sqlData,MYSQLI_ASSOC)){
                              echo($row['seg']);
                              }     

                        }

                        }

            ?>

Its expected to keep my values from the other columns on the same row!

PICTURE OF WHAT IS HAPPENING TO THE DATABASE

  • 写回答

2条回答 默认 最新

  • dongyi7901 2019-07-30 14:32
    关注

    This code you've shown is not responsible for resetting the row. Something else must be doing it. There is another update somewhere, possibly a trigger on the database, but it's not coming from the code sample you've posted.

    Regarding your code, there are several things wrong, which others have already pointed out. I would like to add a couple more

    1. Ensure you SANITIZE your data before inserting it in MySQL. Check out PDO and bindparam, for starters. Short of that, use (int) to cast the variable to an integer.
    2. The update should only happen if there is new user activity.

    Code:

     if ($new_user_activity['data'] == 'Seg') {
         $segHora = (int)$new_user_activity['horas'];
         $sql = ("UPDATE horario SET seg=$segHora WHERE id=1");
    
         if (!mysqli_query($conn, $sql)) {
             echo ("ERRO NA QUERY");
             return; // or exit, if this is not a function
         }
     }
    
     $showData = ("SELECT * FROM horario");
     $sqlData = mysqli_query($conn, $showData);
    
     while ($row = mysqli_fetch_array($sqlData, MYSQLI_ASSOC)) {
         echo ($row['seg']);
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值