dongzhan2461 2015-05-11 14:30
浏览 108

循环遍历1个表并将选定的值添加到第2个表,并且“在循环迭代时执行更多操作[重复]

I have 2 tables: 'user2' and 'notes'

I would like to select the email entry in 'users2' where signin = 900 and then update a row in 'notes' (with the same email) with a PIN number.

<?php
require_once ('mysqli_connect.php'); //connect to database

$q = "SELECT email As email FROM users2 WHERE signin = 900";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));
$num = @mysqli_num_rows($r);
if ($num > 0) {
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){         
        $email = $row['email'];

        $pin = 1234;
        $q = "UPDATE notes SET permit = '$pin' WHERE email = '$email'";
        $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc)); 
   }
}         
?>

An error is being returned:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/fxyour/public_html/Checkin/new4.php on line 8

That is the line with 'while' on it.

I have tried various other ways but the error remains. I have the same layout in another project and it works fine.

</div>
  • 写回答

1条回答 默认 最新

  • doushuichong2589 2015-05-11 14:36
    关注

    Something like this should help:

    $pin = 1234;
    $q = "update notes set permit='$pin' WHERE email in (SELECT email FROM users2 WHERE signin = 900)";
    $r = mysqli_query ($dbc, $q);
    

    Update:

    Error in currently provided code by author - $r variable used two times for different results, and used in while loop, so it will destroy workflow of script. Change one variable name.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行