douhai9043 2019-03-21 23:48
浏览 58
已采纳

SQL Update语句不起作用,但来自row = fetch_assoc()的信息将打印在屏幕上

I am looping through the rows of the database with fetch_assoc() and I am selecting two columns of it. I check the values of these two columns of each row. If they are between 2 values then I updated a third row as 1(TRUE) if a statement is true. I am creating two connections because there are two statements, the one that selects the information and the other that updates the column that I want to be updated. When I try to print the information in the screen it seems that the SELECT statement works but when I try to UPDATE the column that I want the UPDATE statement does not update the database. Here is my code:

$conn= mysqli_connect("localhost","root","root");        

$variablech = 1 ;   

$query = "SELECT Client, Info FROM DataTable";


if ($result = mysqli_query($conn, $query)) {

while($row=mysqli_fetch_row($result)) {

    if((($row['Client']>=54.055) && ($row['Client']<=54.117) ) && (( $row['Info']>=-4.827) && ( $row['Info']<=-4.317)))
   {
    $variablech = 0;

    $sql=  " UPDATE DataTable SET InfoData='$variablech' WHERE Client='".$row['Client']."' AND Info='".$row['Info']."'";
    $conn->query($sql);
    echo "yes";
  }

  else
  {
    $variablech = 1;
    $sql=  " UPDATE DataTable SET InfoData='$variablech' WHERE Client='".$row['Client']."' AND Info'".$row['Info']."'";
    $conn->query($sql);

   echo "no";


  }
}
}
  • 写回答

1条回答 默认 最新

  • doutan5798 2019-03-22 01:13
    关注

    This should do the trick:

    ...
    $sql=  "UPDATE DataTable SET InfoData='".$variablech."' WHERE Client='".$row['Client']."' AND Info='".$row['Info']."'";
    mysqli_query($conn, $sql)
    ...
    

    Suggestion:

    Not sure, if you have an auto_increment field in your table. If you do, then select it in select query and then use the same to update the data in update query. That will speed up the database stuff and so is your project.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题