【问题标题】:Select random row, update column as seen选择随机行,如所见更新列
【发布时间】:2017-04-27 12:01:29
【问题描述】:

我想要一行包含一个名为“seen”的列,那么该列将具有默认值无值或“是”值。

所以..

  1. 在之前没有“见过”的地方抓取一排。
  2. 将上面的“seen column”行更新为“yes”。
  3. 如果所有行的值为“yes”,则会显示通知/错误: 您已成功完成所有数字。

我已尽我所能做到这一点,但它不起作用。我认为我处理此问题的逻辑可能不正确?

    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 语句? 干杯

【问题讨论】:

  • $query = mysqli_query("UPDATE num_image SET seen = yes");你知道这会更新表格的所有行吗?
  • ...如果它到达那里。
  • @AgamBanga 我如何只更新选择的 SELECT 记录?有没有办法将两者联系在一起?
  • 您似乎一直在尝试将您的代码从 mysql_ 转换为 mysqli_;它在许多层面上都失败了。您应该先阅读手册。
  • @aussiedan 您需要从Select 查询中获取一些主键(may be database increment id in your case),然后将其传递给Where Update 中的条件

标签: php mysql


【解决方案1】:

你必须对Update语句中的值进行转义:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2012-06-20
    • 1970-01-01
    • 2011-06-30
    • 2021-11-27
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多