3

AIM

I am attempting to update SQL.

I suspect that the issue is either with my sql query, or with my connection. Although, I could be totally wrong.

Apologies if it's messy, but I'm using console.log to attempt to debug the issue, and the console output is:

B.1
B.2
D.1
D.2
D.3
B.2.1
B.5

In relation to sql queries, amongst others, I've attempted with the following two:

  • $sql = "UPDATE Users SET description = " . '$description' . "WHERE userID = " . '$this->userID';
  • $sql = "UPDATE Users SET description = '$description' WHERE userID = '$this->userID'";

CODE

edit-profile-handler.php

<?php

if(isset($_POST['edit-profile-button'])) {
    $description = $_POST['edit-description'];

    echo '<script>console.log("B.1")</script>';

    if(isset($description)) {
        echo '<script>console.log("B.2")</script>';
        $result = $user->updateDescription($description);
        echo '<script>console.log("B.2.1")</script>';
    }

    if($result == true) {
        echo '<script>console.log("B.4")</script>';
        header("Location: profile.php");
    }

    echo '<script>console.log("B.5")</script>';
}
?>

User.php

<?php

class User {

    private $con;
    private $userID;
    private $description;

    public function __construct($con, $userID) {
        $this->con = $con;
        $this->userID = $userID;

        $sql = "SELECT * FROM Users WHERE userID='$this->userID'";
        $query = mysqli_query($this->con, $sql);
        $user = mysqli_fetch_array($query);

        $this->description = $user['description'];
    }

    public function getID() {
        return $this->userID;
    }

    public function updateDescription($description) {
        echo '<script>console.log("D.1")</script>';
        $sql = "UPDATE Users SET description = '$description' WHERE userID = '$this->userID'";
        echo '<script>console.log("D.2")</script>';
        $result = mysqli_query($this->con, $sql);
        echo '<script>console.log("D.3")</script>';
        return $result;
        echo '<script>console.log("D.4")</script>';
    }

}

?>

1 Answer 1

5

Your $result variable is not returning a BOOLEAN because it handles an UPDATE query result.

So on your updateDescription function, try to return mysqli_affected_rows() then try to check on edit-profile-handler.php if $return > 0 it means there are row/s affected by your update. You can refer here.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Roshan, I've tried as suggested and mysqli_affected_rows($result) = 0. Does that mean that the issue is with the $sql query? I've tested the connection to the db, and it's working with login, register, etc.
Connection must be the parameter just like this. $success= mysqli_affected_rows($this->con); then return $success.
Thanks @Roshan, I've done as advised. With the query as $sql = "UPDATE Users SET description = 'hello mick' WHERE userID = 2"; it works! So I suspect it's an issue with my $sql query, although, struggling to figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.