我想在表格中选择最高值:
$max = "SELECT MAX(pid) FROM pic";
然后将该值传递给PHP变量:
$results_max = $conn->query($max);
$highest_val = $results_max->fetch_assoc();
然后在SQL插入语句中再次使用:
$sql_update = "UPDATE users
SET username = '$username', pid = '$highest_val'
WHERE username = '$username'";
然而,我测试了我从第一个选择语句($highest_val
)得到的值,它返回"Array"
。有谁知道我做错了什么?
编辑:
$sql_update = "UPDATE users
SET username = '$username', pic_id = '$highest_val[pid]'
WHERE username = '$username'" ;
答案 0 :(得分:0)
您需要创建MAX(pid);
$max = "SELECT MAX(pid) as pid FROM pic";
现在使用
获取max pid$results_max = $conn->query($max);
$highest = $results_max->fetch_assoc();
$highest_val =$highest['pid'];// pass column name here
您的更新查询将是
$sql_update = "UPDATE users
SET username = '".$username."', pid = '".$highest_val."'
WHERE username = '".$username."'";