duanbenzan4050 2015-07-16 06:09
浏览 59
已采纳

试图在php中获取非对象的属性

I'm getting an error

"Trying to get property of non object" on : if ($result4->num_rows > 0) .

Why this is occurring?And how to prevent it happening again. I'm just starting with php... Can it be solved? Thank you in advance.

if (isset($_POST['submit'])) 
{
    if (isset($_POST['ID'])) {
    $sql = "SHOW COLUMNS FROM Employees";
    $result = mysqli_query($con,$sql);
     while($row = mysqli_fetch_array($result)){
                $tempname = $row['Field'];

                // Changes Function
                $sqlCheck = "SELECT * FROM Employees WHERE ID='".$_GET["id"]."' AND (".$row['Field']." NOT LIKE '".$_POST[$tempname]."')";
                $result3 = $con->query($sqlCheck);
                if ($result3->num_rows > 0) {
                // output data of each row
                while($row3 = $result3->fetch_assoc()) {
                    $sql3 = "INSERT INTO `Changes` (`Table`, `ID`, `Attribute`, `DateChanged`, `HRUser`, `OldValue`, `NewValue`)

VALUES ('Employees', '".$_GET["id"]."', '".$row["Field"]."', '".date("d/m/Y h:i:sa")."', '$login_session', '$row3[$tempname]', '$_POST[$tempname]')";
                    if ($con->query($sql3) === TRUE) {
                    } else {
                    echo "Error: " . $sql3 . "<br>" . $con->error;
                    }
                }
                }
  • 写回答

2条回答 默认 最新

  • douhoulei4706 2015-07-16 06:29
    关注

    Do not mix both mysqli fuctions and OOP Concept Try this Code

    if (isset($_POST['submit'])) 
    {
        if (isset($_POST['AFNumber'])) {
        $sql = "SHOW COLUMNS FROM Employees";
        $result = mysqli_query($con,$sql);
         while($row = mysqli_fetch_array($result)){
                    $tempname = $row['Field'];
    
                    // Changes Function
                    $sqlCheck = "SELECT * FROM Employees WHERE AFNumber='".$_GET["af"]."' AND (".$row['Field']." NOT LIKE '".$_POST[$tempname]."')";
                    $result3 = mysqli_query($con,$sqlCheck);
                    if (mysqli_num_rows($result3) > 0) {
                    // output data of each row
                    while($row3 = mysqli_fetch_array($result3)) {
                        $sql3 = "INSERT INTO `Changes` (`Table`, `AFNumber`, `Attribute`, `DateChanged`, `HRUser`, `OldValue`, `NewValue`)
    
    VALUES ('Employees', '".$_GET["af"]."', '".$row["Field"]."', '".date("d/m/Y h:i:sa")."', '$login_session', '$row3[$tempname]', '$_POST[$tempname]')";
                        if (mysqli_query($con,$sql3) === TRUE) {
                        } else {
                        echo "Error: " . $sql3 . "<br>" . mysqli_error($con);
                        }
                    }
                    }
                    //End Changes Function
    
    
                    $sql2 = "UPDATE Employees SET ".$row['Field']."= '$_POST[$tempname]' WHERE AFNumber='".$_GET["af"]."'";
                    $result2 = mysqli_query($con,$sql2);
                    if (mysqli_query($con,$sql2) === TRUE) {
                    } else {
                        echo "Error: " . $sql2 . "<br>" . mysqli_error($con);
                        echo '<script>swal("Error", "Something went wrong '.mysqli_error($con).'", "error");</script>';
                    }
    
    }       
    
    
    
    
        echo '<script>swal("Success", "Changes have been saved", "success");</script>';
    } //End If POST Submit True
        else{
    
         $sql = "SHOW COLUMNS FROM Candidates";
        $result = mysqli_query($con,$sql);
         while($row = mysqli_fetch_array($result)){
                    $tempname = $row['Field'];
    
    
                // Changes Function
                    $sqlCheck = "SELECT * FROM Candidates WHERE CID='".$_GET["cid"]."' AND (".$row['Field']." NOT LIKE '%".$_POST[$tempname]."%')";
                    $result4 = mysqli_query($con,$sqlCheck);
                    if (mysqli_num_rows($result4) > 0) {
                    // output data of each row
                    while($row4 = mysqli_fetch_array($result4)) {
                        $sql4 = "INSERT INTO `Changes` (`Table`, `AFNumber`, `Attribute`, `DateChanged`, `HRUser`, `OldValue`, `NewValue`)
    
    VALUES ('Candidates', '".$_GET["cid"]."', '".$row["Field"]."', '".date("d/m/Y h:i:sa")."', '$login_session', '$row4[$tempname]', '$_POST[$tempname]')";
                        if (mysqli_query($con,$sql4) === TRUE) {
                        } else {
                        echo "Error: " . $sql4 . "<br>" . mysqli_error($con);
                        }
                    }
                    }
    
    
    
        }
    
        echo '<script>swal("Success", "Changes have been saved", "success");</script>';
    
    
        }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含