douhui1957 2015-12-29 21:45
浏览 21
已采纳

关于检索和处理的通知

This is my table:

enter image description here

I want a query that will retrieve the maximum QID from the table where I specify a Pid. Then I want to take the selected value and add 1 to it. I want this to work even if there is no value on qid where Pid is for example 1 or 2.

For example if the max qid is 1 where pid is 1, I want a code to retrieve the value qid=1 and add 1 to it. Then the new value, "2" in this case, should be stored in a hidden input field. If I, for example, put "WHERE pid=4" in my query and there is no pid=4 in my table, which retrieves a null value from qid, that should also work and the new value should be 1(null + 1 = 1).

I have this but I don't know if it is right. I'm maybe doing something wrong in the query.

<?php
$qu = "SELECT coalesce(max(qid),1) AS id FROM answer_det WHERE pid = '1' ";
 $result = $mysqli->query($qu);

if ($result->num_rows > 0) {
    while($row = $result->fetch_array()) {   
        
        echo "
            <form action='insertdilemma.php' method='post'>
            <input type='text' name='ans1'>
            <input type='hidden' name='qidsend' value='". $row["id"]. "'>
            <input type='submit' name='submit' value='Send'>   
            </form>";
}}
  else {
    echo "0 results";
}

mysqli_close($mysqli);
          
?>

I get this error message: Notice: Trying to get property of non-object in C:\xampp\htdocs\wildfire\dilemman.php on line 132

This is on line 132: if ($result->num_rows > 0) {

</div>
  • 写回答

1条回答 默认 最新

  • doudun1934 2015-12-29 21:52
    关注

    simply remove the second where

       $qu = "SELECT ifnull(max(qid)+1,1) AS id FROM answer_det  WHERE pid = '1' ";
    

    and you should submit the new qid but also the id .

        echo "
            <form action='insertdilemma.php' method='post'>
            <input type='text' name='ans1'>
            <input type='hidden' name='pidsend' value='". $row["pid"]. "'>
            <input type='hidden' name='qidsend' value='". $row["id"]. "'>
            <input type='submit' name='submit' value='Send'>   
            </form>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并