试图在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;
}
}
}
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>';
}}
@Dan, I see you accepted an answer, but I'd like to add my bit as well.
there are a couple of problems here (see comments), rather than 'fix' (I'd use OOP over procedural any day) the code let's explain your issue. The reason why you are getting the error is because your select query (for one reason or another) is failing, that causes $result4
to be false so when you call $result4->num_rows
you get the above error.
try updating these lines:
$result4 = $con->query($sqlCheck);
if ($result4->num_rows > 0) {
with this:
if (($result4 = $con->query($sqlCheck)) === false) {
echo 'error: '.$con->error;
} else {
this should tell you what the error is in your sql statement so you can fix it yourself, and remove the need to check num_rows
(which you now don't need)
NOTE: you should always be checking the result of your sql queries and trying to handle the error in some way - don't assume that your queries always succeed!