简体   繁体   中英

PHP SQL update query

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>';
    }

}

?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM