选择随机行,更新列如图所示

时间:2017-04-27 12:01:30

标签: php mysql

我希望有一行名为“'see'的列,然后此列将具有默认值no值或”yes“值。

所以..

  1. 抓住一行之前没有“看到”的地方。
  2. 更新上面的“已见列”以表示“是”。
  3. 如果所有行的值都为“yes”,则会显示通知/错误: 您已成功完成所有号码。
  4. 我已经尽了最大努力实现它,但它无法正常工作。我认为我解决这个问题的逻辑可能不正确?

        include 'DB.php';
        $con = mysqli_connect($host,$user,$pass);
        $dbs = mysqli_select_db($databaseName, $con);
    
        // Grabs one row where it hasn't been seen before
        $query = mysqli_query("SELECT number, association, image_file, skeleton, sound, colour, comments FROM num_image WHERE seen='' ORDER by rand() LIMIT 1");
    
        // Updates the above row with the 'seen' column saying 'yes''
        $query = mysqli_query("UPDATE num_image SET seen = yes");
    
        // Fetches Result
        $thestuff = mysqli_fetch_row($query);
    
    
    $seenme=$_POST['seen']; // get value of 'seen' column
    
    $result = mysqli_query("SELECT * FROM num_image where seen=$seenme");
    
    // Trying to delivery a message if the enitre 'seen' column is ALL yes.
    while($row = mysqli_fetch_row($result))
    {
        if($row['seen'] == 'yes')
        { // All numbers seen
          echo 'You have successfully completed all numbers.';
          echo json_encode($thestuff); 
        }
        else
        { // Show numbers
          echo json_encode($thestuff); 
        }
    }
    

    SELECT和UPDATE行是否也必须是if语句? 干杯

1 个答案:

答案 0 :(得分:1)

您必须转义更新语句中的值:

$query = mysqli_query("UPDATE num_image SET seen = 'yes' ");