从mysql中的表中选择max id

时间:2017-05-15 11:43:04

标签: php mysql

我希望代码将我的数据库中的行“image”更新为与用户选择在我的论坛上的答案中上传的图片相关的名称。

我希望查询从网址中获取question_id,并选择与该question_id相关联的最高答案ID。但是值不断说“NULL”它不会更新。

  $mq = "
  UPDATE forum_answers 
     SET image = '".$_FILES['file']['name']."'
   WHERE question_id = '$id' 
     AND a_id = '(SELECT MAX(a_id) FROM forum_answers)'
       '
   ";
   $q = mysqli_query($conn,$mq);

我的桌子

Table: forum_questions
rows: id, topic, detail, name, datetime, reply, view
Sample value: id=45, topic=hej, detail=haha, name=admin, datetime=25/04/17 04:01:15, view=7, reply=0


Table: forum_answers
rows: question_id, a_id, a_name, a_answer, a_datetime, image

此处的图片行不会更新。

2 个答案:

答案 0 :(得分:0)

请检查以下代码。它帮助

 $file_name = $_FILES['file']['name'];
 $mq = "UPDATE forum_answers SET image= '$file_name' WHERE question_id='$id' 
 AND a_id=(SELECT MAX(a_id) FROM forum_answers)";

答案 1 :(得分:-1)

$mq = "UPDATE forum_answers SET image = '".$_FILES['file']['name']."' WHERE question_id=$id AND a_id='(SELECT MAX(a_id) FROM forum_answers)''";

请尝试此问题_id是整数类型